FrontISTR  5.9.0
Large-scale structural analysis program with finit element method
hecmw_couple_bounding_box.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 
16 #include "hecmw_couple_define.h"
17 #include "hecmw_couple_struct.h"
18 #include "hecmw_couple_control.h"
21 
22 #define INFINITE (1.0E+37)
23 
24 /*================================================================================================*/
25 
27  struct hecmw_couple_bounding_box *bbox) {
28  if (bbox == NULL) return;
29 
30  HECMW_free(bbox->just);
31  HECMW_free(bbox->enlarged);
32  HECMW_free(bbox);
33  bbox = NULL;
34 }
35 
36 static struct hecmw_couple_bounding_box *alloc_struct_bbox(void) {
37  struct hecmw_couple_bounding_box *bbox = NULL;
38  int size;
39 
40  size = sizeof(struct hecmw_couple_bounding_box);
41  bbox = (struct hecmw_couple_bounding_box *)HECMW_malloc(size);
42  if (bbox == NULL) {
43  HECMW_set_error(errno, "");
44  return NULL;
45  }
46  bbox->just = NULL;
47  bbox->enlarged = NULL;
48 
49  bbox->just =
50  (struct hecmw_couple_box *)HECMW_malloc(sizeof(struct hecmw_couple_box));
51  if (bbox->just == NULL) {
52  HECMW_set_error(errno, "");
53  goto error;
54  }
55  bbox->enlarged =
56  (struct hecmw_couple_box *)HECMW_malloc(sizeof(struct hecmw_couple_box));
57  if (bbox->enlarged == NULL) {
58  HECMW_set_error(errno, "");
59  goto error;
60  }
61 
62  bbox->coef = 0.0;
63  bbox->tolerance = 0.0;
64 
65  bbox->just->min_x = 0.0;
66  bbox->just->min_y = 0.0;
67  bbox->just->min_z = 0.0;
68  bbox->just->max_x = 0.0;
69  bbox->just->max_y = 0.0;
70  bbox->just->max_z = 0.0;
71 
72  bbox->enlarged->min_x = 0.0;
73  bbox->enlarged->min_y = 0.0;
74  bbox->enlarged->min_z = 0.0;
75  bbox->enlarged->max_x = 0.0;
76  bbox->enlarged->max_y = 0.0;
77  bbox->enlarged->max_z = 0.0;
78 
79  return bbox;
80 
81 error:
83  return NULL;
84 }
85 
86 /*================================================================================================*/
87 
89  const char *boundary_id, const struct hecmwST_local_mesh *mesh,
90  const struct hecmw_couple_boundary *boundary) {
91  struct hecmw_couple_bounding_box *bbox = NULL;
92  double length_x, length_y, length_z, coord_x, coord_y, coord_z, half_coef;
93  double min_x, min_y, min_z, max_x, max_y, max_z;
94  int elem, node, i;
95  long long j;
96 
97  if (boundary_id == NULL) {
99  "HECMW_couple_set_bounding_box(): 'boundary_id' is NULL");
100  return NULL;
101  }
102  if (mesh == NULL) {
104  "HECMW_couple_set_bounding_box(): 'mesh' is NULL");
105  return NULL;
106  }
107  if (boundary == NULL) {
109  "HECMW_couple_set_bounding_box(): 'boundary' is NULL");
110  return NULL;
111  }
112 
113  /* allocation & initialization */
114  if ((bbox = alloc_struct_bbox()) == NULL) return NULL;
115 
116  HECMW_couple_ctrl_get_tolerance(boundary_id, &bbox->tolerance);
117  if (bbox->tolerance < 0.0) goto error;
118  HECMW_couple_ctrl_get_bbcoef(boundary_id, &bbox->coef);
119  if (bbox->coef < 0.0) goto error;
120 
121  if (boundary->node->n == 0) return bbox;
122 
123  min_x = min_y = min_z = +INFINITE;
124  max_x = max_y = max_z = -INFINITE;
125 
126  if (boundary->geom_type == HECMW_COUPLE_NODE_GROUP) { /* node group */
128  "In current version, node group is not supported");
129  goto error;
130 
131  /*
132  for(i=0; i<boundary->node->n; i++) {
133  node = boundary->node->item[i];
134  coord_x = mesh->node[3*(node-1) ];
135  coord_y = mesh->node[3*(node-1)+1];
136  coord_z = mesh->node[3*(node-1)+2];
137 
138  if(coord_x < min_x) min_x = coord_x;
139  if(coord_y < min_y) min_y = coord_y;
140  if(coord_z < min_z) min_z = coord_z;
141  if(coord_x > max_x) max_x = coord_x;
142  if(coord_y > max_y) max_y = coord_y;
143  if(coord_z > max_z) max_z = coord_z;
144  }
145  */
146 
147  } else if (boundary->geom_type ==
148  HECMW_COUPLE_ELEMENT_GROUP) { /* element group */
150  "In current version, element group is not supported");
151  goto error;
152 
153  /*
154  for(i=0; i<boundary->node->n; i++) {
155  node = boundary->node->item[i];
156  coord_x = mesh->node[3*(node-1) ];
157  coord_y = mesh->node[3*(node-1)+1];
158  coord_z = mesh->node[3*(node-1)+2];
159 
160  if(coord_x < min_x) min_x = coord_x;
161  if(coord_y < min_y) min_y = coord_y;
162  if(coord_z < min_z) min_z = coord_z;
163  if(coord_x > max_x) max_x = coord_x;
164  if(coord_y > max_y) max_y = coord_y;
165  if(coord_z > max_z) max_z = coord_z;
166  }
167  */
168 
169  } else if (boundary->geom_type ==
170  HECMW_COUPLE_SURFACE_GROUP) { /* surface group */
171  for (i = 0; i < boundary->surf->n; i++) {
172  elem = boundary->surf->item[2 * i];
173  for (j = mesh->elem_node_index[elem - 1]; j < mesh->elem_node_index[elem];
174  j++) {
175  node = mesh->elem_node_item[j];
176  coord_x = mesh->node[3 * (node - 1)];
177  coord_y = mesh->node[3 * (node - 1) + 1];
178  coord_z = mesh->node[3 * (node - 1) + 2];
179 
180  if (coord_x < min_x) min_x = coord_x;
181  if (coord_y < min_y) min_y = coord_y;
182  if (coord_z < min_z) min_z = coord_z;
183  if (coord_x > max_x) max_x = coord_x;
184  if (coord_y > max_y) max_y = coord_y;
185  if (coord_z > max_z) max_z = coord_z;
186  }
187  }
188 
189  } else { /* error */
191  goto error;
192  }
193 
194  /* just size bounding box */
195  bbox->just->min_x = min_x;
196  bbox->just->min_y = min_y;
197  bbox->just->min_z = min_z;
198  bbox->just->max_x = max_x;
199  bbox->just->max_y = max_y;
200  bbox->just->max_z = max_z;
201 
202  /* enlarged size bounding box */
203  half_coef = (bbox->coef - 1.0) * 0.5;
204  length_x = bbox->just->max_x - bbox->just->min_x;
205  length_y = bbox->just->max_y - bbox->just->min_y;
206  length_z = bbox->just->max_z - bbox->just->min_z;
207 
208  if (length_x > half_coef) {
209  bbox->enlarged->min_x = min_x - length_x * half_coef;
210  bbox->enlarged->max_x = max_x + length_x * half_coef;
211  } else {
212  bbox->enlarged->min_x = min_x - half_coef;
213  bbox->enlarged->max_x = max_x + half_coef;
214  }
215  if (length_y > half_coef) {
216  bbox->enlarged->min_y = min_y - length_y * half_coef;
217  bbox->enlarged->max_y = max_y + length_y * half_coef;
218  } else {
219  bbox->enlarged->min_y = min_y - half_coef;
220  bbox->enlarged->max_y = max_y + half_coef;
221  }
222  if (length_z > half_coef) {
223  bbox->enlarged->min_z = min_z - length_z * half_coef;
224  bbox->enlarged->max_z = max_z + length_z * half_coef;
225  } else {
226  bbox->enlarged->min_z = min_z - half_coef;
227  bbox->enlarged->max_z = max_z + half_coef;
228  }
229 
230  return bbox;
231 
232 error:
234  return NULL;
235 }
struct hecmw_couple_bounding_box * HECMW_couple_set_bounding_box(const char *boundary_id, const struct hecmwST_local_mesh *mesh, const struct hecmw_couple_boundary *boundary)
void HECMW_couple_free_bounding_box(struct hecmw_couple_bounding_box *bbox)
#define INFINITE
int HECMW_couple_ctrl_get_tolerance(const char *boundary_id, double *tolerance)
int HECMW_couple_ctrl_get_bbcoef(const char *boundary_id, double *bbcoef)
#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 * node
struct hecmw_couple_boundary_item * surf
struct hecmw_couple_box * just
struct hecmw_couple_box * enlarged
long long * elem_node_index
Definition: hecmw_struct.h:195