FrontISTR  6.0.0
Large-scale structural analysis program with finit element method
hecmw_vis_generate_histogram_sf.c
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright (c) 2019 FrontISTR Commons
3  * This software is released under the MIT License, see LICENSE.txt
4  *****************************************************************************/
5 
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <math.h>
11 #include "hecmw_vis_bmp.h"
12 #include "hecmw_vis_font_texture.h"
13 #include "hecmw_vis_mem_util.h"
14 #include "hecmw_malloc.h"
15 
16 void generate_histogram_graph_sf(struct surface_module *sf, int *color_list,
17  struct hecmwST_result_data *data,
18  double *mivalue, double *mavalue,
19  Result *result, int mynode, int pesize,
20  HECMW_Comm VIS_COMM, int color_system_type) {
21  int i, j, k, m, ii;
22  double delta, value, color[3];
23  int count[500], t_count[500], max_number, max_length, start_x, end_x, start_y;
24  FILE *fp;
25  double *graph;
26  BITMAPFILEHEADER header; /* File header */
27 
28  unsigned char r, g, b;
29  int ri, gi, bi;
30  BITMAPINFOHEADER info;
31 
32  int start_xs, start_ys;
33  char buf[128];
34  int output7[7][7];
35  int color_id;
36 
37  for (i = 0; i < data->nn_component; i++) {
38  if (color_list[i] == 1) {
39  color_id = i;
40  delta = (mavalue[i] - mivalue[i]) / 500.0;
41  }
42  }
43 
44  for (i = 0; i < 500; i++) {
45  count[i] = 0;
46  t_count[i] = 0;
47  }
48 
49  for (ii = 1; ii <= sf[0].surface_style; ii++) {
50  if (sf[ii].display_method != 4) {
51  for (i = 0; i < result[ii - 1].n_vertex; i++) {
52  j = (int)((result[ii - 1].color[i] - mivalue[color_id]) / delta);
53  if (j < 0) j = 0;
54  if (j > 499) j = 499;
55  count[j]++;
56  }
57  }
58  }
59  if (pesize > 1)
60  HECMW_Allreduce(count, t_count, 500, HECMW_INT, HECMW_SUM, VIS_COMM);
61  else {
62  for (i = 0; i < 500; i++) t_count[i] = count[i];
63  }
64  /* fprintf(stderr, "count[2]=%d t_count[2]=%d\n", count[2], t_count[2]);
65  */
66  if (mynode == 0) {
67  fp = fopen("histogram.bmp", "wb");
68  if (fp == NULL)
69  HECMW_vis_print_exit("Cannot generate the histogram output file");
70  graph = (double *)HECMW_calloc(400 * 530 * 3, sizeof(double));
71  if (graph == NULL) HECMW_vis_memory_exit("graph");
72  for (i = 0; i < 400 * 530 * 3; i++) graph[i] = 0.0;
73  max_number = 0;
74  for (i = 0; i < 500; i++) {
75  if (t_count[i] > max_number) max_number = t_count[i];
76  }
77  if (max_number == 0)
79  "ERROR: HEC-MW-VIS-E2003:Cannot generate histogram graph, the number "
80  "of voxels is 0");
81  max_length = (int)(400 - 30 - 5 - 45 * 1.5);
82  start_x = (int)(5 + 45 * 1.5 + 15);
83  start_y = 15;
84  for (j = 0; j < 500; j++) {
85  end_x =
86  (int)((double)t_count[j] * (double)max_length / (double)max_number +
87  start_x) +
88  2;
89  value = j / 500.0;
90  value2_to_rgb(value, color, color_system_type);
91  for (i = start_x; i < end_x; i++) {
92  graph[((j + 15) * 400 + i) * 3] = color[0];
93  graph[((j + 15) * 400 + i) * 3 + 1] = color[1];
94  graph[((j + 15) * 400 + i) * 3 + 2] = color[2];
95  }
96  }
97  /*start mark scales */
98  start_y = 15;
99  for (k = 0; k < 11; k++) {
100  value = mivalue[color_id] +
101  (mavalue[color_id] - mivalue[color_id]) / 10.0 * k;
102  start_ys = start_y + (int)((double)500.0 / 10 * k) - (int)7 / 2;
103  start_xs = 15;
104  snprintf(buf, sizeof(buf), "%3.2E", value);
105  for (m = 0; m < 9; m++) {
106  font7_generate(buf[8 - m], output7);
107  for (j = 0; j < 7; j++)
108  for (i = 0; i < 7; i++) {
109  graph[((start_ys + j) * 400 + start_xs - i) * 3] =
110  (double)output7[6 - j][i];
111  graph[((start_ys + j) * 400 + start_xs - i) * 3 + 1] =
112  (double)output7[6 - j][i];
113  graph[((start_ys + j) * 400 + start_xs - i) * 3 + 2] =
114  (double)output7[6 - j][i];
115  }
116  start_xs += 7;
117  if ((value >= 0) && (m == 0)) start_xs -= 7;
118  }
119  if ((k != 0) && (k != 10)) {
120  start_ys += (int)7 / 2;
121 
122  for (i = start_x; i < start_x + 5; i++)
123  for (j = 0; j < 3; j++) graph[(start_ys * 400 + i) * 3 + j] = 1.0;
124  }
125  }
126  header.bfSize = 54 + 3 * 400 * 530;
127 #ifdef CONVERSE_ORDER
128  header.bfSize = change_unsigned_int_order(54 + 3 * 400 * 530);
129 #endif
130  header.bfReserved1 = 0;
131 #ifdef CONVERSE_ORDER
133 #endif
134 
135  header.bfReserved2 = 0;
136 #ifdef CONVERSE_ORDER
138 #endif
139 
140  header.bfOffBits = 54;
141 #ifdef CONVERSE_ORDER
143 #endif
144 
145  info.biBitCount = 24;
146 #ifdef CONVERSE_ORDER
148 #endif
149 
150  info.biSize = 40;
151 #ifdef CONVERSE_ORDER
153 #endif
154 
155  info.biWidth = 400;
156 #ifdef CONVERSE_ORDER
157  info.biWidth = change_int_order(400);
158 #endif
159 
160  info.biHeight = 530;
161 #ifdef CONVERSE_ORDER
162  info.biHeight = change_int_order(530);
163 #endif
164 
165  info.biSizeImage = 3 * 400 * 530;
166 #ifdef CONVERSE_ORDER
167  info.biSizeImage = change_unsigned_int_order(3 * 400 * 530);
168 #endif
169 
170  info.biClrImportant = 0;
171 #ifdef CONVERSE_ORDER
173 #endif
174 
175  info.biClrUsed = 0;
176 #ifdef CONVERSE_ORDER
178 #endif
179 
180  info.biCompression = 0;
181 #ifdef CONVERSE_ORDER
183 #endif
184 
185  info.biPlanes = 1;
186 #ifdef CONVERSE_ORDER
188 #endif
189 
190  info.biXPelsPerMeter = 3780;
191 #ifdef CONVERSE_ORDER
192  info.biXPelsPerMeter = change_int_order(3780);
193 #endif
194 
195  info.biYPelsPerMeter = 3780;
196 #ifdef CONVERSE_ORDER
197  info.biYPelsPerMeter = change_int_order(3780);
198 #endif
199 
200  putc('B', fp);
201  putc('M', fp);
202  fwrite(&(header.bfSize), sizeof(unsigned int), 1, fp);
203  fwrite(&header.bfReserved1, sizeof(unsigned short int), 1, fp);
204  fwrite(&header.bfReserved2, sizeof(unsigned short int), 1, fp);
205  fwrite(&header.bfOffBits, sizeof(unsigned int), 1, fp);
206  fwrite(&info, 40, 1, fp);
207  for (j = 0; j < 530; j++)
208  for (i = 400 - 1; i >= 0; i--) {
209  ri = (int)(graph[(j * 400 + i) * 3] * 256);
210  gi = (int)(graph[(j * 400 + i) * 3 + 1] * 256);
211  bi = (int)(graph[(j * 400 + i) * 3 + 2] * 256);
212  if (ri < 0) ri = 0;
213  if (ri > 255) ri = 255;
214  if (gi < 0) gi = 0;
215  if (gi > 255) gi = 255;
216  if (bi < 0) bi = 0;
217  if (bi > 255) bi = 255;
218  r = ri;
219  g = gi;
220  b = bi;
221  putc(b, fp);
222  putc(g, fp);
223  putc(r, fp);
224  }
225 
226  fclose(fp);
227  HECMW_free(graph);
228  }
229  return;
230 }
231 
232 void generate_interval_point_sf(struct surface_module *sf, int *color_list,
233  struct hecmwST_result_data *data,
234  double *mivalue, double *mavalue,
235  Result *result, int mynode, int pesize,
236  HECMW_Comm VIS_COMM, double *interval_point) {
237  int i, j, ii;
238  double delta;
239  int count[500], t_count[500], tmp_count[500], sum_count, interv_count, sum,
240  current_j;
241  int color_id;
242 
243  for (i = 0; i < data->nn_component; i++) {
244  if (color_list[i] == 1) {
245  color_id = i;
246  delta = (mavalue[i] - mivalue[i]) / 500.0;
247  }
248  }
249 
250  for (i = 0; i < 500; i++) {
251  count[i] = 0;
252  t_count[i] = 0;
253  }
254 
255  for (ii = 1; ii <= sf[0].surface_style; ii++) {
256  if (sf[ii].display_method != 4) {
257  for (i = 0; i < result[ii - 1].n_vertex; i++) {
258  j = (int)((result[ii - 1].color[i] - mivalue[color_id]) / delta);
259  if (j < 0) j = 0;
260  if (j > 499) j = 499;
261  count[j]++;
262  }
263  }
264  }
265  if (pesize > 1)
266  HECMW_Allreduce(count, t_count, 500, HECMW_INT, HECMW_SUM, VIS_COMM);
267  else {
268  for (i = 0; i < 500; i++) t_count[i] = count[i];
269  }
270  sum_count = 0;
271  for (i = 0; i < 500; i++) {
272  sum_count += t_count[i];
273  tmp_count[i] = t_count[i];
274  }
275  interv_count = (int)sum_count / 10;
276  current_j = 0;
277  interval_point[0] = mivalue[color_id];
278  interval_point[1] = 0.0;
279  for (i = 1; i < 10; i++) {
280  interval_point[i * 2 + 1] = (double)i / 10.0;
281  sum = 0;
282  j = current_j;
283  while ((j < 500) && (sum < interv_count)) {
284  sum += t_count[j];
285  j++;
286  }
287  j--;
288  interval_point[i * 2] =
289  ((double)j / 500.0 +
290  1.0 / 500.0 *
291  (1.0 - (double)(sum - interv_count) / (double)tmp_count[j])) *
292  (mavalue[color_id] - mivalue[color_id]) +
293  mivalue[color_id];
294  t_count[j] = sum - interv_count;
295  current_j = j;
296  }
297  interval_point[20] = mavalue[color_id];
298  interval_point[21] = 1.0;
299  fprintf(stderr, "The automatic color mapping set is :\n");
300  for (i = 0; i < 11; i++)
301  fprintf(stderr, "%lf %lf \n", interval_point[i * 2],
302  interval_point[i * 2 + 1]);
303  return;
304 }
305 
306 void output_histogram_sf(struct surface_module *sf, int *color_list,
307  struct hecmwST_result_data *data, double *mivalue,
308  double *mavalue, Result *result, int mynode,
309  int pesize, HECMW_Comm VIS_COMM) {
310  int i, j, ii;
311  double delta;
312  int count[100], t_count[100];
313  FILE *fp;
314  int color_id;
315  for (i = 0; i < data->nn_component; i++) {
316  if (color_list[i] == 1) {
317  color_id = i;
318  delta = (mavalue[i] - mivalue[i]) / 100.0;
319  }
320  }
321 
322  for (i = 0; i < 100; i++) {
323  count[i] = 0;
324  t_count[i] = 0;
325  }
326 
327  for (ii = 1; ii <= sf[0].surface_style; ii++) {
328  if (sf[ii].display_method != 4) {
329  for (i = 0; i < result[ii - 1].n_vertex; i++) {
330  j = (int)((result[ii - 1].color[i] - mivalue[color_id]) / delta);
331  if (j < 0) j = 0;
332  if (j > 99) j = 99;
333  count[j]++;
334  }
335  }
336  }
337  if (pesize > 1)
338  HECMW_Allreduce(count, t_count, 100, HECMW_INT, HECMW_SUM, VIS_COMM);
339  else {
340  for (i = 0; i < 100; i++) t_count[i] = count[i];
341  }
342 
343  if (mynode == 0) {
344  fp = fopen("histogram.file", "w");
345  if (fp == NULL)
346  HECMW_vis_print_exit("Cannot generate the histogram output file");
347  for (i = 0; i < 100; i++)
348  fprintf(fp, "%d %d -----(%lf --- %lf)\n", i, t_count[i],
349  mivalue[color_id] + i * delta,
350  mivalue[color_id] + (i + 1) * delta);
351  fclose(fp);
352  }
353  return;
354 }
355 
356 void find_minmax_sf(struct hecmwST_local_mesh *mesh, int mynode,
357  double range[6]) {
358  int i;
359  for (i = 0; i < 3; i++) {
360  range[i * 2] = 1.0E17;
361  range[i * 2 + 1] = -1.0E17;
362  }
363  for (i = 0; i < mesh->n_node; i++) {
364  if (mesh->node[i * 3] < range[0]) range[0] = mesh->node[i * 3];
365  if (mesh->node[i * 3] > range[1]) range[1] = mesh->node[i * 3];
366  if (mesh->node[i * 3 + 1] < range[2]) range[2] = mesh->node[i * 3 + 1];
367  if (mesh->node[i * 3 + 1] > range[3]) range[3] = mesh->node[i * 3 + 1];
368  if (mesh->node[i * 3 + 2] < range[4]) range[4] = mesh->node[i * 3 + 2];
369  if (mesh->node[i * 3 + 2] > range[5]) range[5] = mesh->node[i * 3 + 2];
370  }
371 
372  return;
373 }
374 
375 void find_patch_minmax_sf(Result *result, struct surface_module *sf,
376  double range[6]) {
377  int i, ii, j;
378 
379  for (i = 0; i < 3; i++) {
380  range[i * 2] = 1.0E17;
381  range[i * 2 + 1] = -1.0E17;
382  }
383 
384  for (ii = 0; ii < sf[0].surface_style; ii++) {
385  for (i = 0; i < result[ii].n_vertex; i++) {
386  for (j = 0; j < 3; j++) {
387  if (result[ii].vertex[i * 3 + j] < range[j * 2])
388  range[j * 2] = result[ii].vertex[i * 3 + j];
389  if (result[ii].vertex[i * 3 + j] > range[j * 2 + 1])
390  range[j * 2 + 1] = result[ii].vertex[i * 3 + j];
391  }
392  }
393  }
394  return;
395 }
396 
398  double range[6]) {
399  int i, ii, j;
400  double new_v;
401 
402  for (ii = 0; ii < sf[0].surface_style; ii++) {
403  if (sf[ii + 1].deform_display_on == 1) {
404  for (i = 0; i < result[ii].n_vertex; i++) {
405  for (j = 0; j < 3; j++) {
406  new_v = result[ii].vertex[i * 3 + j] +
407  result[ii].disp[i * 3 + j] * sf[ii + 1].disp_scale;
408  if (new_v < range[j * 2]) range[j * 2] = new_v;
409  if (new_v > range[j * 2 + 1]) range[j * 2 + 1] = new_v;
410  }
411  }
412  }
413  }
414  return;
415 }
416 
417 void get_deform_scale(struct surface_module *sf, int ii, double range_x,
418  double range_y, double range_z,
419  struct hecmwST_local_mesh *mesh,
420  struct hecmwST_result_data *data, int pesize,
421  HECMW_Comm VIS_COMM) {
422  double max_disp, t_max_disp, tmp[3], disp, max_range, s_scale;
423  int i, j;
424  int tn_component, d_base;
425  int mynode;
426 
427  tn_component = 0;
428  for (i = 0; i < data->nn_component; i++) tn_component += data->nn_dof[i];
429  d_base = 0;
430  for (i = 0; i < sf[ii].disp_comp; i++) d_base += data->nn_dof[i];
431 
432  max_disp = 0.0;
433 
434  for (i = 0; i < mesh->n_node; i++) {
435  for (j = 0; j < 3; j++)
436  tmp[j] = data->node_val_item[i * tn_component + d_base + j];
437  disp = sqrt(tmp[0] * tmp[0] + tmp[1] * tmp[1] + tmp[2] * tmp[2]);
438  if (disp > max_disp) max_disp = disp;
439  }
440  HECMW_Comm_rank(VIS_COMM, &mynode);
441  if (pesize > 1)
442  HECMW_Allreduce(&max_disp, &t_max_disp, 1, HECMW_DOUBLE, HECMW_MAX,
443  VIS_COMM);
444  else
445  t_max_disp = max_disp;
446 
447  max_range = sqrt(range_x * range_x + range_y * range_y + range_z * range_z);
448  if (fabs(t_max_disp) < EPSILON * 0.0001)
449  HECMW_vis_print_exit("No deformation in this dataset");
450  s_scale = max_range * 0.1 / t_max_disp;
451  if (sf[ii].disp_scale < 0.0)
452  sf[ii].disp_scale = s_scale;
453  else
454  sf[ii].disp_scale *= s_scale;
455  if (mynode == 0)
456  fprintf(stderr, "The real deformation scale = %lf\n", sf[ii].disp_scale);
457  return;
458 }
int HECMW_Comm_rank(HECMW_Comm comm, int *rank)
Definition: hecmw_comm.c:18
int HECMW_Allreduce(void *sendbuf, void *recvbuf, int count, HECMW_Datatype datatype, HECMW_Op op, HECMW_Comm comm)
Definition: hecmw_comm.c:404
#define HECMW_INT
Definition: hecmw_config.h:48
#define HECMW_MAX
Definition: hecmw_config.h:58
#define HECMW_DOUBLE
Definition: hecmw_config.h:50
MPI_Comm HECMW_Comm
Definition: hecmw_config.h:30
#define HECMW_SUM
Definition: hecmw_config.h:60
struct hecmwST_local_mesh * mesh
Definition: hecmw_repart.h:71
#define NULL
#define HECMW_calloc(nmemb, size)
Definition: hecmw_malloc.h:21
#define HECMW_free(ptr)
Definition: hecmw_malloc.h:24
#define EPSILON
int change_int_order(int n)
void value2_to_rgb(double value, double color[3], int color_system_type)
void font7_generate(char input, int output[7][7])
unsigned short int change_short_int_order(unsigned short int n)
unsigned int change_unsigned_int_order(unsigned int n)
void find_minmax_sf(struct hecmwST_local_mesh *mesh, int mynode, double range[6])
void find_new_patch_minmax_sf(Result *result, struct surface_module *sf, double range[6])
void get_deform_scale(struct surface_module *sf, int ii, double range_x, double range_y, double range_z, struct hecmwST_local_mesh *mesh, struct hecmwST_result_data *data, int pesize, HECMW_Comm VIS_COMM)
void output_histogram_sf(struct surface_module *sf, int *color_list, struct hecmwST_result_data *data, double *mivalue, double *mavalue, Result *result, int mynode, int pesize, HECMW_Comm VIS_COMM)
void generate_interval_point_sf(struct surface_module *sf, int *color_list, struct hecmwST_result_data *data, double *mivalue, double *mavalue, Result *result, int mynode, int pesize, HECMW_Comm VIS_COMM, double *interval_point)
void find_patch_minmax_sf(Result *result, struct surface_module *sf, double range[6])
void generate_histogram_graph_sf(struct surface_module *sf, int *color_list, struct hecmwST_result_data *data, double *mivalue, double *mavalue, Result *result, int mynode, int pesize, HECMW_Comm VIS_COMM, int color_system_type)
void HECMW_vis_print_exit(char *var)
void HECMW_vis_memory_exit(char *var)
CNFData data
unsigned short bfReserved1
Definition: hecmw_vis_bmp.h:12
unsigned short bfReserved2
Definition: hecmw_vis_bmp.h:13
unsigned int bfSize
Definition: hecmw_vis_bmp.h:11
unsigned int bfOffBits
Definition: hecmw_vis_bmp.h:14
unsigned int biCompression
Definition: hecmw_vis_bmp.h:25
unsigned int biSizeImage
Definition: hecmw_vis_bmp.h:26
unsigned short biBitCount
Definition: hecmw_vis_bmp.h:24
unsigned short biPlanes
Definition: hecmw_vis_bmp.h:23
unsigned int biClrUsed
Definition: hecmw_vis_bmp.h:29
unsigned int biSize
Definition: hecmw_vis_bmp.h:20
unsigned int biClrImportant
Definition: hecmw_vis_bmp.h:30