FrontISTR  5.9.0
Large-scale structural analysis program with finit element method
hecmw_couple_background_cell.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 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <assert.h>
10 #include <errno.h>
11 
12 #include "hecmw_msgno.h"
13 #include "hecmw_struct.h"
14 #include "hecmw_error.h"
15 #include "hecmw_comm.h"
16 
17 #include "hecmw_couple_define.h"
18 #include "hecmw_couple_control.h"
22 
23 #define INFINITE (1.0E+37)
24 
25 #define EPS (1.0E-06)
26 
27 /*================================================================================================*/
28 
30  struct hecmw_couple_background_cell *bgcell) {
31  if (bgcell == NULL) return;
32 
33  HECMW_free(bgcell);
34  bgcell = NULL;
35 }
36 
37 static struct hecmw_couple_background_cell *alloc_struct_bgcell(void) {
38  struct hecmw_couple_background_cell *bgcell = NULL;
39  int size;
40 
41  size = sizeof(struct hecmw_couple_background_cell);
42  bgcell = (struct hecmw_couple_background_cell *)HECMW_malloc(size);
43  if (bgcell == NULL) {
44  HECMW_set_error(errno, "");
45  return NULL;
46  }
47 
48  bgcell->n = 0;
49  bgcell->coef = 0.0;
50  bgcell->nx = 0;
51  bgcell->ny = 0;
52  bgcell->nz = 0;
53  bgcell->dx = 0.0;
54  bgcell->dy = 0.0;
55  bgcell->dz = 0.0;
56 
57  return bgcell;
58 }
59 
60 static int elem_size_by_elem(const struct hecmwST_local_mesh *mesh,
61  const struct hecmw_couple_boundary *boundary,
62  double *min_dx, double *min_dy, double *min_dz,
63  double *max_dx, double *max_dy, double *max_dz) {
64  double min_x, min_y, min_z, max_x, max_y, max_z, dx, dy, dz, coord_x, coord_y,
65  coord_z;
66  int elem, node, i;
67  long long j;
68 
69  *min_dx = *min_dy = *min_dz = INFINITE;
70  *max_dx = *max_dy = *max_dz = 0.0;
71 
72  for (i = 0; i < boundary->elem->n; i++) {
73  elem = boundary->elem->item[i];
74 
75  min_x = min_y = min_z = +INFINITE;
76  max_x = max_y = max_z = -INFINITE;
77 
78  for (j = mesh->elem_node_index[elem - 1]; j < mesh->elem_node_index[elem];
79  j++) {
80  node = mesh->elem_node_item[j];
81  coord_x = mesh->node[3 * (node - 1)];
82  coord_y = mesh->node[3 * (node - 1) + 1];
83  coord_z = mesh->node[3 * (node - 1) + 2];
84 
85  if (coord_x < min_x) min_x = coord_x;
86  if (coord_y < min_y) min_y = coord_y;
87  if (coord_z < min_z) min_z = coord_z;
88  if (coord_x > max_x) max_x = coord_x;
89  if (coord_y > max_y) max_y = coord_y;
90  if (coord_z > max_z) max_z = coord_z;
91  }
92 
93  dx = max_x - min_x;
94  dy = max_y - min_y;
95  dz = max_z - min_z;
96 
97  if (dx < *min_dx) *min_dx = dx;
98  if (dy < *min_dy) *min_dy = dy;
99  if (dz < *min_dz) *min_dz = dz;
100  if (dx > *max_dx) *max_dx = dx;
101  if (dy > *max_dy) *max_dy = dy;
102  if (dz > *max_dz) *max_dz = dz;
103  }
104 
105  return 0;
106 }
107 
108 static int elem_size_by_surf(const struct hecmwST_local_mesh *mesh,
109  const struct hecmw_couple_boundary *boundary,
110  double *min_dx, double *min_dy, double *min_dz,
111  double *max_dx, double *max_dy, double *max_dz) {
112  double min_x, min_y, min_z, max_x, max_y, max_z, dx, dy, dz, coord_x, coord_y,
113  coord_z;
114  int elem, node, i;
115  long long j;
116 
117  *min_dx = *min_dy = *min_dz = INFINITE;
118  *max_dx = *max_dy = *max_dz = 0.0;
119 
120  for (i = 0; i < boundary->surf->n; i++) {
121  elem = boundary->surf->item[2 * i];
122 
123  min_x = min_y = min_z = +INFINITE;
124  max_x = max_y = max_z = -INFINITE;
125 
126  for (j = mesh->elem_node_index[elem - 1]; j < mesh->elem_node_index[elem];
127  j++) {
128  node = mesh->elem_node_item[j];
129  coord_x = mesh->node[3 * (node - 1)];
130  coord_y = mesh->node[3 * (node - 1) + 1];
131  coord_z = mesh->node[3 * (node - 1) + 2];
132 
133  if (coord_x < min_x) min_x = coord_x;
134  if (coord_y < min_y) min_y = coord_y;
135  if (coord_z < min_z) min_z = coord_z;
136  if (coord_x > max_x) max_x = coord_x;
137  if (coord_y > max_y) max_y = coord_y;
138  if (coord_z > max_z) max_z = coord_z;
139  }
140 
141  dx = max_x - min_x;
142  dy = max_y - min_y;
143  dz = max_z - min_z;
144 
145  if (dx < *min_dx) *min_dx = dx;
146  if (dy < *min_dy) *min_dy = dy;
147  if (dz < *min_dz) *min_dz = dz;
148  if (dx > *max_dx) *max_dx = dx;
149  if (dy > *max_dy) *max_dy = dy;
150  if (dz > *max_dz) *max_dz = dz;
151  }
152 
153  return 0;
154 }
155 
156 /*================================================================================================*/
157 
159  const char *boundary_id, const struct hecmwST_local_mesh *mesh,
160  const struct hecmw_couple_bounding_box *bbox,
161  const struct hecmw_couple_boundary *boundary) {
162  struct hecmw_couple_background_cell *bgcell;
163  double min_dx, min_dy, min_dz, max_dx, max_dy, max_dz, dx, dy, dz;
164  double bbox_size_x, bbox_size_y, bbox_size_z;
165  int nx, ny, nz;
166 
167  if (boundary_id == NULL) {
170  "HECMW_couple_set_background_cell(): 'boundary_id' is NULL");
171  return NULL;
172  }
173  if (mesh == NULL) {
175  "HECMW_couple_set_background_cell(): 'mesh' is NULL");
176  return NULL;
177  }
178  if (bbox == NULL) {
180  "HECMW_couple_set_background_cell(): 'bbox' is NULL");
181  return NULL;
182  }
183  if (boundary == NULL) {
185  "HECMW_couple_set_background_cell(): 'boundary' is NULL");
186  return NULL;
187  }
188 
189  if ((bgcell = alloc_struct_bgcell()) == NULL) return NULL;
190 
191  if (HECMW_couple_ctrl_get_bgcoef(boundary_id, &bgcell->coef) != HECMW_SUCCESS)
192  goto error;
193 
194  if (boundary->geom_type ==
195  HECMW_COUPLE_NODE_GROUP) { /* node group */
197  "In current version, node group is not supported.");
198  goto error;
199 
200  } else if (boundary->geom_type ==
201  HECMW_COUPLE_ELEMENT_GROUP) { /* element group */
203  "In current version, element group is not supported.");
204  goto error;
205  /* elem_size_by_elem(mesh, boundary, &min_dx, &min_dy, &min_dz, &max_dx,
206  * &max_dy, &max_dz); */
207 
208  } else if (boundary->geom_type ==
209  HECMW_COUPLE_SURFACE_GROUP) { /* surface group */
210  elem_size_by_surf(mesh, boundary, &min_dx, &min_dy, &min_dz, &max_dx,
211  &max_dy, &max_dz);
212 
213  } else { /* error */
215  goto error;
216  }
217 
218  bbox_size_x = bbox->enlarged->max_x - bbox->enlarged->min_x;
219  bbox_size_y = bbox->enlarged->max_y - bbox->enlarged->min_y;
220  bbox_size_z = bbox->enlarged->max_z - bbox->enlarged->min_z;
221 
222  if (max_dx * bgcell->coef > bbox->tolerance + EPS) {
223  dx = max_dx * bgcell->coef;
224  } else {
225  dx = bbox->tolerance + EPS;
226  }
227  if (max_dy * bgcell->coef > bbox->tolerance + EPS) {
228  dy = max_dy * bgcell->coef;
229  } else {
230  dy = bbox->tolerance + EPS;
231  }
232  if (max_dz * bgcell->coef > bbox->tolerance + EPS) {
233  dz = max_dz * bgcell->coef;
234  } else {
235  dz = bbox->tolerance + EPS;
236  }
237 
238  nx = bbox_size_x / dx;
239  ny = bbox_size_y / dy;
240  nz = bbox_size_z / dz;
241 
242  bgcell->nx = (nx > 0) ? nx : 1;
243  bgcell->ny = (ny > 0) ? ny : 1;
244  bgcell->nz = (nz > 0) ? nz : 1;
245 
246  bgcell->dx = bbox_size_x / bgcell->nx;
247  bgcell->dy = bbox_size_y / bgcell->ny;
248  bgcell->dz = bbox_size_z / bgcell->nz;
249 
250  bgcell->n = bgcell->nx * bgcell->ny * bgcell->nz;
251 
252  return bgcell;
253 
254 error:
256  return NULL;
257 }
#define HECMW_SUCCESS
Definition: hecmw_config.h:66
#define INFINITE
struct hecmw_couple_background_cell * HECMW_couple_set_background_cell(const char *boundary_id, const struct hecmwST_local_mesh *mesh, const struct hecmw_couple_bounding_box *bbox, const struct hecmw_couple_boundary *boundary)
void HECMW_couple_free_background_cell(struct hecmw_couple_background_cell *bgcell)
int HECMW_couple_ctrl_get_bgcoef(const char *boundary_id, double *bgcoef)
#define HECMW_COUPLE_ELEMENT_GROUP
#define HECMWCPL_E_NONSUPPORT_GEOMTYPE
#define HECMW_COUPLE_NODE_GROUP
#define HECMWCPL_E_INVALID_ARG
#define HECMW_COUPLE_SURFACE_GROUP
#define HECMWCPL_E_INVALID_GEOMTYPE
struct hecmwST_local_mesh * mesh
Definition: hecmw_repart.h:71
int HECMW_set_error(int errorno, const char *fmt,...)
Definition: hecmw_error.c:37
#define NULL
#define HECMW_free(ptr)
Definition: hecmw_malloc.h:24
#define HECMW_malloc(size)
Definition: hecmw_malloc.h:20
struct hecmw_couple_boundary_item * elem
struct hecmw_couple_boundary_item * surf
struct hecmw_couple_box * enlarged
long long * elem_node_index
Definition: hecmw_struct.h:195