FrontISTR  5.7.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, j;
67 
68  *min_dx = *min_dy = *min_dz = INFINITE;
69  *max_dx = *max_dy = *max_dz = 0.0;
70 
71  for (i = 0; i < boundary->elem->n; i++) {
72  elem = boundary->elem->item[i];
73 
74  min_x = min_y = min_z = +INFINITE;
75  max_x = max_y = max_z = -INFINITE;
76 
77  for (j = mesh->elem_node_index[elem - 1]; j < mesh->elem_node_index[elem];
78  j++) {
79  node = mesh->elem_node_item[j];
80  coord_x = mesh->node[3 * (node - 1)];
81  coord_y = mesh->node[3 * (node - 1) + 1];
82  coord_z = mesh->node[3 * (node - 1) + 2];
83 
84  if (coord_x < min_x) min_x = coord_x;
85  if (coord_y < min_y) min_y = coord_y;
86  if (coord_z < min_z) min_z = coord_z;
87  if (coord_x > max_x) max_x = coord_x;
88  if (coord_y > max_y) max_y = coord_y;
89  if (coord_z > max_z) max_z = coord_z;
90  }
91 
92  dx = max_x - min_x;
93  dy = max_y - min_y;
94  dz = max_z - min_z;
95 
96  if (dx < *min_dx) *min_dx = dx;
97  if (dy < *min_dy) *min_dy = dy;
98  if (dz < *min_dz) *min_dz = dz;
99  if (dx > *max_dx) *max_dx = dx;
100  if (dy > *max_dy) *max_dy = dy;
101  if (dz > *max_dz) *max_dz = dz;
102  }
103 
104  return 0;
105 }
106 
107 static int elem_size_by_surf(const struct hecmwST_local_mesh *mesh,
108  const struct hecmw_couple_boundary *boundary,
109  double *min_dx, double *min_dy, double *min_dz,
110  double *max_dx, double *max_dy, double *max_dz) {
111  double min_x, min_y, min_z, max_x, max_y, max_z, dx, dy, dz, coord_x, coord_y,
112  coord_z;
113  int elem, node, i, j;
114 
115  *min_dx = *min_dy = *min_dz = INFINITE;
116  *max_dx = *max_dy = *max_dz = 0.0;
117 
118  for (i = 0; i < boundary->surf->n; i++) {
119  elem = boundary->surf->item[2 * i];
120 
121  min_x = min_y = min_z = +INFINITE;
122  max_x = max_y = max_z = -INFINITE;
123 
124  for (j = mesh->elem_node_index[elem - 1]; j < mesh->elem_node_index[elem];
125  j++) {
126  node = mesh->elem_node_item[j];
127  coord_x = mesh->node[3 * (node - 1)];
128  coord_y = mesh->node[3 * (node - 1) + 1];
129  coord_z = mesh->node[3 * (node - 1) + 2];
130 
131  if (coord_x < min_x) min_x = coord_x;
132  if (coord_y < min_y) min_y = coord_y;
133  if (coord_z < min_z) min_z = coord_z;
134  if (coord_x > max_x) max_x = coord_x;
135  if (coord_y > max_y) max_y = coord_y;
136  if (coord_z > max_z) max_z = coord_z;
137  }
138 
139  dx = max_x - min_x;
140  dy = max_y - min_y;
141  dz = max_z - min_z;
142 
143  if (dx < *min_dx) *min_dx = dx;
144  if (dy < *min_dy) *min_dy = dy;
145  if (dz < *min_dz) *min_dz = dz;
146  if (dx > *max_dx) *max_dx = dx;
147  if (dy > *max_dy) *max_dy = dy;
148  if (dz > *max_dz) *max_dz = dz;
149  }
150 
151  return 0;
152 }
153 
154 /*================================================================================================*/
155 
157  const char *boundary_id, const struct hecmwST_local_mesh *mesh,
158  const struct hecmw_couple_bounding_box *bbox,
159  const struct hecmw_couple_boundary *boundary) {
160  struct hecmw_couple_background_cell *bgcell;
161  double min_dx, min_dy, min_dz, max_dx, max_dy, max_dz, dx, dy, dz;
162  double bbox_size_x, bbox_size_y, bbox_size_z;
163  int nx, ny, nz;
164 
165  if (boundary_id == NULL) {
168  "HECMW_couple_set_background_cell(): 'boundary_id' is NULL");
169  return NULL;
170  }
171  if (mesh == NULL) {
173  "HECMW_couple_set_background_cell(): 'mesh' is NULL");
174  return NULL;
175  }
176  if (bbox == NULL) {
178  "HECMW_couple_set_background_cell(): 'bbox' is NULL");
179  return NULL;
180  }
181  if (boundary == NULL) {
183  "HECMW_couple_set_background_cell(): 'boundary' is NULL");
184  return NULL;
185  }
186 
187  if ((bgcell = alloc_struct_bgcell()) == NULL) return NULL;
188 
189  if (HECMW_couple_ctrl_get_bgcoef(boundary_id, &bgcell->coef) != HECMW_SUCCESS)
190  goto error;
191 
192  if (boundary->geom_type ==
193  HECMW_COUPLE_NODE_GROUP) { /* node group */
195  "In current version, node group is not supported.");
196  goto error;
197 
198  } else if (boundary->geom_type ==
199  HECMW_COUPLE_ELEMENT_GROUP) { /* element group */
201  "In current version, element group is not supported.");
202  goto error;
203  /* elem_size_by_elem(mesh, boundary, &min_dx, &min_dy, &min_dz, &max_dx,
204  * &max_dy, &max_dz); */
205 
206  } else if (boundary->geom_type ==
207  HECMW_COUPLE_SURFACE_GROUP) { /* surface group */
208  elem_size_by_surf(mesh, boundary, &min_dx, &min_dy, &min_dz, &max_dx,
209  &max_dy, &max_dz);
210 
211  } else { /* error */
213  goto error;
214  }
215 
216  bbox_size_x = bbox->enlarged->max_x - bbox->enlarged->min_x;
217  bbox_size_y = bbox->enlarged->max_y - bbox->enlarged->min_y;
218  bbox_size_z = bbox->enlarged->max_z - bbox->enlarged->min_z;
219 
220  if (max_dx * bgcell->coef > bbox->tolerance + EPS) {
221  dx = max_dx * bgcell->coef;
222  } else {
223  dx = bbox->tolerance + EPS;
224  }
225  if (max_dy * bgcell->coef > bbox->tolerance + EPS) {
226  dy = max_dy * bgcell->coef;
227  } else {
228  dy = bbox->tolerance + EPS;
229  }
230  if (max_dz * bgcell->coef > bbox->tolerance + EPS) {
231  dz = max_dz * bgcell->coef;
232  } else {
233  dz = bbox->tolerance + EPS;
234  }
235 
236  nx = bbox_size_x / dx;
237  ny = bbox_size_y / dy;
238  nz = bbox_size_z / dz;
239 
240  bgcell->nx = (nx > 0) ? nx : 1;
241  bgcell->ny = (ny > 0) ? ny : 1;
242  bgcell->nz = (nz > 0) ? nz : 1;
243 
244  bgcell->dx = bbox_size_x / bgcell->nx;
245  bgcell->dy = bbox_size_y / bgcell->ny;
246  bgcell->dz = bbox_size_z / bgcell->nz;
247 
248  bgcell->n = bgcell->nx * bgcell->ny * bgcell->nz;
249 
250  return bgcell;
251 
252 error:
254  return NULL;
255 }
hecmw_couple_boundary::surf
struct hecmw_couple_boundary_item * surf
Definition: hecmw_couple_boundary_info.h:23
hecmw_couple_boundary::geom_type
int geom_type
Definition: hecmw_couple_boundary_info.h:19
hecmw_couple_bounding_box.h
hecmw_couple_background_cell::dy
double dy
Definition: hecmw_couple_background_cell.h:23
HECMW_couple_ctrl_get_bgcoef
int HECMW_couple_ctrl_get_bgcoef(const char *boundary_id, double *bgcoef)
Definition: hecmw_couple_control.c:1874
hecmw_couple_background_cell::nz
int nz
Definition: hecmw_couple_background_cell.h:21
INFINITE
#define INFINITE
Definition: hecmw_couple_background_cell.c:23
hecmw_couple_box::max_x
double max_x
Definition: hecmw_couple_bounding_box.h:19
hecmwST_local_mesh::elem_node_item
int * elem_node_item
Definition: hecmw_struct.h:196
HECMW_malloc
#define HECMW_malloc(size)
Definition: hecmw_malloc.h:20
hecmw_couple_box::max_y
double max_y
Definition: hecmw_couple_bounding_box.h:20
mesh
struct hecmwST_local_mesh * mesh
Definition: hecmw_repart.h:71
hecmw_couple_background_cell::coef
double coef
Definition: hecmw_couple_background_cell.h:18
hecmw_comm.h
hecmwST_local_mesh
Definition: hecmw_struct.h:139
hecmw_couple_boundary_item::n
int n
Definition: hecmw_couple_boundary_info.h:17
hecmw_couple_boundary
Definition: hecmw_couple_boundary_info.h:18
hecmw_error.h
hecmw_couple_bounding_box::enlarged
struct hecmw_couple_box * enlarged
Definition: hecmw_couple_bounding_box.h:25
hecmw_couple_box::min_z
double min_z
Definition: hecmw_couple_bounding_box.h:18
hecmw_couple_background_cell::ny
int ny
Definition: hecmw_couple_background_cell.h:20
hecmw_couple_background_cell::n
int n
Definition: hecmw_couple_background_cell.h:17
HECMW_COUPLE_ELEMENT_GROUP
#define HECMW_COUPLE_ELEMENT_GROUP
Definition: hecmw_couple_define.h:37
hecmw_couple_box::min_y
double min_y
Definition: hecmw_couple_bounding_box.h:17
hecmw_struct.h
hecmwST_local_mesh::node
double * node
Definition: hecmw_struct.h:170
hecmw_couple_control.h
hecmw_msgno.h
HECMW_COUPLE_NODE_GROUP
#define HECMW_COUPLE_NODE_GROUP
Definition: hecmw_couple_define.h:35
hecmwST_local_mesh::elem_node_index
int * elem_node_index
Definition: hecmw_struct.h:195
HECMW_COUPLE_SURFACE_GROUP
#define HECMW_COUPLE_SURFACE_GROUP
Definition: hecmw_couple_define.h:39
hecmw_couple_boundary::elem
struct hecmw_couple_boundary_item * elem
Definition: hecmw_couple_boundary_info.h:22
HECMWCPL_E_INVALID_GEOMTYPE
#define HECMWCPL_E_INVALID_GEOMTYPE
Definition: hecmw_couple_define.h:167
HECMW_couple_free_background_cell
void HECMW_couple_free_background_cell(struct hecmw_couple_background_cell *bgcell)
Definition: hecmw_couple_background_cell.c:29
HECMW_SUCCESS
#define HECMW_SUCCESS
Definition: hecmw_config.h:64
hecmw_couple_boundary_info.h
HECMW_couple_set_background_cell
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)
Definition: hecmw_couple_background_cell.c:156
hecmw_couple_background_cell::dx
double dx
Definition: hecmw_couple_background_cell.h:22
hecmw_couple_box::min_x
double min_x
Definition: hecmw_couple_bounding_box.h:16
hecmw_couple_bounding_box
Definition: hecmw_couple_bounding_box.h:21
HECMW_set_error
int HECMW_set_error(int errorno, const char *fmt,...)
Definition: hecmw_error.c:37
hecmw_couple_background_cell.h
NULL
#define NULL
Definition: hecmw_io_nastran.c:30
HECMWCPL_E_INVALID_ARG
#define HECMWCPL_E_INVALID_ARG
Definition: hecmw_couple_define.h:91
EPS
#define EPS
Definition: hecmw_couple_background_cell.c:25
HECMW_free
#define HECMW_free(ptr)
Definition: hecmw_malloc.h:24
hecmw_couple_background_cell::dz
double dz
Definition: hecmw_couple_background_cell.h:24
HECMWCPL_E_NONSUPPORT_GEOMTYPE
#define HECMWCPL_E_NONSUPPORT_GEOMTYPE
Definition: hecmw_couple_define.h:179
hecmw_couple_box::max_z
double max_z
Definition: hecmw_couple_bounding_box.h:21
hecmw_couple_boundary_item::item
int * item
Definition: hecmw_couple_boundary_info.h:18
hecmw_couple_background_cell
Definition: hecmw_couple_background_cell.h:13
hecmw_couple_define.h
hecmw_couple_background_cell::nx
int nx
Definition: hecmw_couple_background_cell.h:19
hecmw_couple_bounding_box::tolerance
double tolerance
Definition: hecmw_couple_bounding_box.h:22