1 /*****************************************************************************
2  * Copyright (c) 2019 FrontISTR Commons
3  * This software is released under the MIT License, see LICENSE.txt
4  *****************************************************************************/
5 
6 #include "hecmw_visualizer.h"
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include "hecmw_vis_mem_util.h"
11 #include "hecmw_vis_read_control.h"
12 #include "hecmw_vis_surface_main.h"
13 #include "hecmw_vis_pvr_main.h"
14 #include "hecmw_malloc.h"
15 
16 PSF_link *psf;
17 PVR_link *pvr;
18 
HECMW_visualize_init(void)19 int HECMW_visualize_init(void) {
20   return HECMW_visualize_init_by_comm(HECMW_comm_get_comm());
21 }
22 
HECMW_visualize_init_by_comm(HECMW_Comm VIS_COMM)23 int HECMW_visualize_init_by_comm(HECMW_Comm VIS_COMM) {
24   FILE *contfp;
25   int pesize, mynode;
26   char *contfile, buf[HECMW_FILENAME_LEN];
27 
28   HECMW_Comm_size(VIS_COMM, &pesize);
29   HECMW_Comm_rank(VIS_COMM, &mynode);
30 
31   if ((contfp = fopen("hecmw_vis.ini", "r")) == NULL) {
32     contfile = HECMW_ctrl_get_control_file("vis_ctrl");
33     if ((contfp = fopen(contfile, "r")) == NULL)
34       HECMW_vis_print_exit("ERROR: HEC-MW-VIS-E0011: Cannot open control file");
35   }
36 
37   psf = (PSF_link *)HECMW_malloc(sizeof(PSF_link));
38   if (psf == NULL) HECMW_vis_memory_exit("psf");
39   psf->next_psf   = NULL;
40   psf->num_of_psf = 0;
41   pvr             = (PVR_link *)HECMW_malloc(sizeof(PVR_link));
42   if (pvr == NULL) HECMW_vis_memory_exit("pvr");
43   pvr->next_pvr   = NULL;
44   pvr->num_of_pvr = 0;
45 
46   HECMW_vis_read_control(contfp, pesize, mynode, psf, pvr);
47   fclose(contfp);
48 
49   return 0;
50 }
51 
HECMW_visualize(struct hecmwST_local_mesh * mesh,struct hecmwST_result_data * data,int timestep)52 int HECMW_visualize(struct hecmwST_local_mesh *mesh,
53                     struct hecmwST_result_data *data, int timestep ) {
54   int ii;
55   char *outfile, *buf1, outfile1[HECMW_FILENAME_LEN];
56   char body[HECMW_FILENAME_LEN];
57   PSF_link *tp1;
58   PVR_link *tv1;
59   int visual_id, init_flag, fg_text;
60   Parameter_rendering *sr;
61   struct surface_module *sf;
62   Parameter_vr *vr;
63   int stat_para_sf[NUM_CONTROL_PSF], stat_para_vr[NUM_CONTROL_PVR];
64   HECMW_Comm VIS_COMM;
65   int pesize, mynode;
66 
67   HECMW_Comm_dup(mesh->HECMW_COMM, &VIS_COMM);
68   HECMW_Comm_size(VIS_COMM, &pesize);
69   HECMW_Comm_rank(VIS_COMM, &mynode);
70 
71   outfile = HECMW_ctrl_get_result_fileheader("vis_out", timestep, &fg_text);
72   buf1 = HECMW_ctrl_get_result_filebody("vis_out");
73 
74   if (HECMW_ctrl_is_subdir()) {
75     if (HECMW_ctrl_make_subdir(outfile)) {
76       HECMW_vis_print_exit(
77           "ERROR: HEC-MW-VIS-E0009: Cannot open output directory");
78     }
79   }
80   if (psf->num_of_psf > 0) {
81     init_flag = 1;
82     tp1       = psf->next_psf;
83     for (visual_id = 0; visual_id < psf->num_of_psf; visual_id++) {
84       if (mynode == 0)
85         fprintf(stderr, " Start visualize PSF %d at timestep %d\n",
86                 visual_id + 1, timestep);
87       sf = tp1->sf;
88       sr = tp1->sr;
89       for (ii            = 0; ii < NUM_CONTROL_PSF; ii++)
90         stat_para_sf[ii] = tp1->stat_para[ii];
91       tp1                = tp1->next_psf;
92       if (psf->num_of_psf > 1) {
93         if (timestep >= 1000) {
94           sprintf(outfile1, "%s_psf%d.%d", outfile, visual_id + 1, timestep);
95           sprintf(body, "%s_psf%d.%d", buf1, visual_id + 1, timestep);
96         } else if ((timestep >= 100) && (timestep <= 999)) {
97           sprintf(outfile1, "%s_psf%d.0%d", outfile, visual_id + 1, timestep);
98           sprintf(body, "%s_psf%d.0%d", buf1, visual_id + 1, timestep);
99         } else if ((timestep >= 10) && (timestep <= 99)) {
100           sprintf(outfile1, "%s_psf%d.00%d", outfile, visual_id + 1, timestep);
101           sprintf(body, "%s_psf%d.00%d", buf1, visual_id + 1, timestep);
102         } else if (timestep <= 9) {
103           sprintf(outfile1, "%s_psf%d.000%d", outfile, visual_id + 1, timestep);
104           sprintf(body, "%s_psf%d.000%d", buf1, visual_id + 1, timestep);
105         }
106       } else {
107         if (timestep >= 1000) {
108           sprintf(outfile1, "%s_psf.%d", outfile, timestep);
109           sprintf(body, "%s_psf.%d", buf1, timestep);
110         } else if ((timestep >= 100) && (timestep <= 999)) {
111           sprintf(outfile1, "%s_psf.0%d", outfile, timestep);
112           sprintf(body, "%s_psf.0%d", buf1, timestep);
113         } else if ((timestep >= 10) && (timestep <= 99)) {
114           sprintf(outfile1, "%s_psf.00%d", outfile, timestep);
115           sprintf(body, "%s_psf.00%d", buf1, timestep);
116         } else if (timestep <= 9) {
117           sprintf(outfile1, "%s_psf.000%d", outfile, timestep);
118           sprintf(body, "%s_psf.000%d", buf1, timestep);
119         }
120       }
121       HECMW_vis_psf_rendering(mesh, data, &timestep, sf, sr, stat_para_sf,
122                               outfile1, body, VIS_COMM);
123       init_flag = 0;
124     }
125   }
126   if (pvr->num_of_pvr > 0) {
127     tv1       = pvr->next_pvr;
128     init_flag = 1;
129     for (visual_id = 0; visual_id < pvr->num_of_pvr; visual_id++) {
130       if (mynode == 0)
131         fprintf(stderr, " Start visualize PVR %d at timestep %d\n",
132                 visual_id + 1, timestep);
133       vr = tv1->vr;
134       for (ii            = 0; ii < NUM_CONTROL_PVR; ii++)
135         stat_para_vr[ii] = tv1->stat_para[ii];
136       tv1                = tv1->next_pvr;
137       if (pvr->num_of_pvr > 1) {
138         if (timestep >= 1000)
139           sprintf(outfile1, "%s_pvr%d.%d", outfile, visual_id + 1, timestep);
140         else if ((timestep >= 100) && (timestep <= 999))
141           sprintf(outfile1, "%s_pvr%d.0%d", outfile, visual_id + 1, timestep);
142         else if ((timestep >= 10) && (timestep <= 99))
143           sprintf(outfile1, "%s_pvr%d.00%d", outfile, visual_id + 1,
144                   timestep);
145         else if (timestep <= 9)
146           sprintf(outfile1, "%s_pvr%d.000%d", outfile, visual_id + 1,
147                   timestep);
148       } else {
149         if (timestep >= 1000)
150           sprintf(outfile1, "%s_pvr.%d", outfile, timestep);
151         else if ((timestep >= 100) && (timestep <= 999))
152           sprintf(outfile1, "%s_pvr.0%d", outfile, timestep);
153         else if ((timestep >= 10) && (timestep <= 99))
154           sprintf(outfile1, "%s_pvr.00%d", outfile, timestep);
155         else if (timestep <= 9)
156           sprintf(outfile1, "%s_pvr.000%d", outfile, timestep);
157       }
158       HECMW_vis_pvr_rendering(mesh, data, &timestep, &init_flag,
159                               pvr->num_of_pvr, vr, stat_para_vr, outfile1,
160                               VIS_COMM);
161       init_flag = 0;
162     }
163   }
164   HECMW_free(buf1);
165   HECMW_free(outfile);
166   HECMW_Comm_free(&VIS_COMM);
167 
168   return 0;
169 }
170 
HECMW_visualize_finalize(void)171 int HECMW_visualize_finalize(void) {
172   PSF_link *tp1, *tp2;
173   PVR_link *tv1, *tv2;
174   int i;
175 
176   if (psf->num_of_psf > 0) {
177     tp1 = psf->next_psf;
178     for (i = 0; i < psf->num_of_psf; i++) {
179       tp2 = tp1;
180       tp1 = tp1->next_psf;
181       HECMW_free(tp2->sf);
182       if (tp2->sr->light_point) HECMW_free(tp2->sr->light_point);
183       HECMW_free(tp2->sr);
184       HECMW_free(tp2);
185     }
186   }
187   HECMW_free(psf);
188   if (pvr->num_of_pvr > 0) {
189     tv1 = pvr->next_pvr;
190     for (i = 0; i < pvr->num_of_pvr; i++) {
191       tv2 = tv1;
192       tv1 = tv1->next_pvr;
193       HECMW_free(tv2->vr);
194       HECMW_free(tv2);
195     }
196   }
197   HECMW_free(pvr);
198 
199   return 0;
200 }
201