FrontISTR 5.2.0
Large-scale structural analysis program with finit element method
Loading...
Searching...
No Matches
hecmw_couple_init.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_config.h"
14#include "hecmw_struct.h"
15
16#include "hecmw_couple_define.h"
17#include "hecmw_couple_struct.h"
19#include "hecmw_couple_info.h"
26#include "hecmw_couple_weight.h"
28#include "hecmw_couple_init.h"
29
30struct couple_info {
31 char *boundary_id;
32 struct hecmw_couple_info *couple_info;
33 struct couple_info *next;
34} couple_list = {
35 NULL, /* boundary_id */
36 NULL, /* couple_info */
37 NULL, /* next */
38};
39
40/*================================================================================================*/
41
42static void free_couple_info(struct hecmw_couple_info *p) {
43 if (p == NULL) return;
44
57
58 p->comm_src = NULL;
59 p->comm_dst = NULL;
60 p->intercomm = NULL;
61 p->boundary_src = NULL;
62 p->boundary_dst = NULL;
65 p->inter_tbl = NULL;
66 p->mapped_point = NULL;
67 p->ip_list_pre = NULL;
68 p->ip_list_main = NULL;
69 p->ip_list_post = NULL;
70 HECMW_free(p);
71 p = NULL;
72}
73
74static void free_couple_list(struct couple_info *p) {
75 if (p == NULL) return;
76
77 free_couple_info(p->couple_info);
78 HECMW_free(p);
79}
80
81static struct hecmw_couple_info *alloc_couple_info(void) {
82 struct hecmw_couple_info *p = NULL;
83
84 p = (struct hecmw_couple_info *)HECMW_malloc(
85 sizeof(struct hecmw_couple_info));
86 if (p == NULL) {
87 HECMW_set_error(errno, "");
88 return NULL;
89 }
92 p->comm_src = NULL;
93 p->comm_dst = NULL;
94 p->intercomm = NULL;
95 p->boundary_src = NULL;
96 p->boundary_dst = NULL;
99 p->inter_tbl = NULL;
100 p->mapped_point = NULL;
101 p->ip_list_pre = NULL;
102 p->ip_list_main = NULL;
103 p->ip_list_post = NULL;
104
105 return p;
106}
107
108static struct couple_info *alloc_couple_list(void) {
109 struct couple_info *p = NULL;
110
111 p = (struct couple_info *)HECMW_malloc(sizeof(struct couple_info));
112 if (p == NULL) {
113 HECMW_set_error(errno, "");
114 return NULL;
115 }
116 p->boundary_id = NULL;
117 p->couple_info = NULL;
118 p->next = NULL;
119
120 if ((p->couple_info = alloc_couple_info()) == NULL) {
121 HECMW_free(p);
122 return NULL;
123 }
124
125 return p;
126}
127
128static struct couple_info *cmp_couple_list(const char *boundary_id) {
129 struct couple_info *p;
130
131 for (p = couple_list.next; p; p = p->next) {
132 if (strcmp(p->boundary_id, boundary_id) == 0) return p;
133 }
134
135 return NULL;
136}
137
138static void del_couple_list(const char *boundary_id) {
139 struct couple_info *p, *q;
140
141 for (p = couple_list.next, q = &couple_list; p; p = p->next) {
142 if (strcmp(p->boundary_id, boundary_id) == 0) {
143 q->next = p->next;
144 free_couple_list(p);
145 break;
146 }
147 }
148}
149
150static int add_couple_list(const char *boundary_id, int unit_specifier_src,
151 int unit_specifier_dst,
152 struct hecmw_couple_comm *comm_src,
153 struct hecmw_couple_comm *comm_dst,
154 struct hecmw_couple_comm *intercomm,
155 struct hecmw_couple_boundary *boundary_src,
156 struct hecmw_couple_boundary *boundary_dst,
157 struct hecmw_couple_intra_iftable *intra_tbl_src,
158 struct hecmw_couple_intra_iftable *intra_tbl_dst,
159 struct hecmw_couple_inter_iftable *inter_tbl,
160 struct hecmw_couple_mapped_point *mapped_point,
161 struct hecmw_couple_weight_list *ip_list_pre,
162 struct hecmw_couple_weight_list *ip_list_main,
163 struct hecmw_couple_weight_list *ip_list_post) {
164 struct couple_info *p = NULL;
165
166 if ((p = cmp_couple_list(boundary_id)) != NULL) {
167 HECMW_set_error(HECMWCPL_E, ""); /*@@*/
168 return -1;
169 }
170
171 if ((p = alloc_couple_list()) == NULL) return -1;
172
173 p->boundary_id = HECMW_strdup(boundary_id);
174 if (p->boundary_id == NULL) {
175 HECMW_set_error(errno, "");
176 goto error;
177 }
178
179 p->couple_info->unit_specifier_src = unit_specifier_src;
180 p->couple_info->unit_specifier_dst = unit_specifier_dst;
181
182 p->couple_info->comm_src = comm_src;
183 p->couple_info->comm_dst = comm_dst;
184 p->couple_info->intercomm = intercomm;
185 p->couple_info->boundary_src = boundary_src;
186 p->couple_info->boundary_dst = boundary_dst;
187 p->couple_info->intra_tbl_src = intra_tbl_src;
188 p->couple_info->intra_tbl_dst = intra_tbl_dst;
189 p->couple_info->inter_tbl = inter_tbl;
190 p->couple_info->mapped_point = mapped_point;
191 p->couple_info->ip_list_pre = ip_list_pre;
192 p->couple_info->ip_list_main = ip_list_main;
193 p->couple_info->ip_list_post = ip_list_post;
194
195 p->next = couple_list.next;
196 couple_list.next = p;
197
198 return 0;
199
200error:
201 return -1;
202}
203
205 const char *boundary_id) {
206 struct couple_info *p;
207
208 if (boundary_id == NULL) {
210 "HECMW_couple_get_info(): 'boundary_id' is NULL");
211 return NULL;
212 }
213
214 if ((p = cmp_couple_list(boundary_id)) == NULL) {
215 HECMW_set_error(HECMWCPL_E, ""); /*@@*/
216 return NULL;
217 }
218
219 return p->couple_info;
220}
221
222/*================================================================================================*/
223
224extern void HECMW_couple_free_init(const char *boundary_id) {
225 del_couple_list(boundary_id);
226}
227
228extern int HECMW_couple_init(const char *boundary_id,
229 struct hecmwST_local_mesh *mesh_unit1,
230 struct hecmwST_local_mesh *mesh_unit2) {
231 struct hecmwST_local_mesh *mesh_src, *mesh_dst;
232 struct hecmw_couple_boundary *boundary_src = NULL, *boundary_dst = NULL;
233 struct hecmw_couple_bounding_box *bbox_src = NULL, *bbox_dst = NULL;
234 struct hecmw_couple_background_cell *bgcell_src = NULL, *bgcell_dst = NULL;
235 struct hecmw_couple_comm *comm_src = NULL, *comm_dst = NULL,
236 *intercomm = NULL;
237 struct hecmw_couple_intra_iftable *intra_tbl_src = NULL,
238 *intra_tbl_dst = NULL;
239 struct hecmw_couple_inter_iftable *inter_tbl = NULL;
240 struct hecmw_couple_mapped_point *mapped_point_dst = NULL;
241 struct hecmw_couple_weight_list *ip_list_pre = NULL;
242 struct hecmw_couple_weight_list *ip_list_main = NULL;
243 struct hecmw_couple_weight_list *ip_list_post = NULL;
244 char src_unit_id[HECMW_NAME_LEN + 1], dst_unit_id[HECMW_NAME_LEN + 1];
245 int is_unit1_memb, is_unit2_memb, is_src_memb, is_dst_memb;
246 int unit_specifier_src, unit_specifier_dst;
247 int direction;
248
249 /* check argument */
250 if (boundary_id == NULL) {
252 "HECMW_couple_init(): 'boundary_id' is NULL");
253 return HECMW_ERROR;
254 }
255
256 is_unit1_memb = HECMW_couple_is_unit_member(boundary_id, HECMW_COUPLE_UNIT1);
257 if (is_unit1_memb < 0) return HECMW_ERROR;
258 is_unit2_memb = HECMW_couple_is_unit_member(boundary_id, HECMW_COUPLE_UNIT2);
259 if (is_unit2_memb < 0) return HECMW_ERROR;
260
261 if (is_unit1_memb && mesh_unit1 == NULL) {
263 "HECMW_couple_init(): 'mesh_unit1' is NULL");
264 return HECMW_ERROR;
265 }
266 if (is_unit2_memb && mesh_unit2 == NULL) {
268 "HECMW_couple_init(): 'mesh_unit2' is NULL");
269 return HECMW_ERROR;
270 }
271
272 /* set each couple unit */
273 HECMW_couple_ctrl_get_direction(boundary_id, &direction);
274 if (direction == HECMW_COUPLE_UNIT1_TO_UNIT2) { /* UNIT1 -> UNIT2 */
275 unit_specifier_src = HECMW_COUPLE_UNIT1;
276 unit_specifier_dst = HECMW_COUPLE_UNIT2;
277 is_src_memb = is_unit1_memb;
278 is_dst_memb = is_unit2_memb;
279 mesh_src = mesh_unit1;
280 mesh_dst = mesh_unit2;
281
282 } else if (direction ==
283 HECMW_COUPLE_UNIT2_TO_UNIT1) { /* UNIT2 -> UNIT1 */
284 unit_specifier_src = HECMW_COUPLE_UNIT2;
285 unit_specifier_dst = HECMW_COUPLE_UNIT1;
286 is_src_memb = is_unit2_memb;
287 is_dst_memb = is_unit1_memb;
288 mesh_src = mesh_unit2;
289 mesh_dst = mesh_unit1;
290
291 } else { /* error */
293 goto error;
294 }
295
296 if (HECMW_couple_get_unit_id(boundary_id, unit_specifier_src, src_unit_id,
297 HECMW_NAME_LEN + 1) == NULL)
298 goto error;
299 if (HECMW_couple_get_unit_id(boundary_id, unit_specifier_dst, dst_unit_id,
300 HECMW_NAME_LEN + 1) == NULL)
301 goto error;
302 if ((comm_src =
303 HECMW_couple_get_intracomm(boundary_id, unit_specifier_src)) == NULL)
304 goto error;
305 if ((comm_dst =
306 HECMW_couple_get_intracomm(boundary_id, unit_specifier_dst)) == NULL)
307 goto error;
308 if ((intercomm = HECMW_couple_get_intercomm(boundary_id)) == NULL) goto error;
309
310 if (is_src_memb) {
311 boundary_src = HECMW_couple_set_boundary_info(boundary_id,
312 unit_specifier_src, mesh_src);
313 if (boundary_src == NULL) goto error;
314
315 bbox_src =
316 HECMW_couple_set_bounding_box(boundary_id, mesh_src, boundary_src);
317 if (bbox_src == NULL) goto error;
318
319 bgcell_src = HECMW_couple_set_background_cell(boundary_id, mesh_src,
320 bbox_src, boundary_src);
321 if (bgcell_src == NULL) goto error;
322
323 intra_tbl_src =
324 HECMW_couple_make_intra_iftable(mesh_src, boundary_src, comm_src);
325 if (intra_tbl_src == NULL) goto error;
326 }
327
328 if (is_dst_memb) {
329 boundary_dst = HECMW_couple_set_boundary_info(boundary_id,
330 unit_specifier_dst, mesh_dst);
331 if (boundary_dst == NULL) goto error;
332
333 bbox_dst =
334 HECMW_couple_set_bounding_box(boundary_id, mesh_dst, boundary_dst);
335 if (bbox_dst == NULL) goto error;
336
337 bgcell_dst = HECMW_couple_set_background_cell(boundary_id, mesh_dst,
338 bbox_dst, boundary_dst);
339 if (bgcell_dst == NULL) goto error;
340
341 mapped_point_dst =
342 HECMW_couple_set_mapped_point(boundary_id, mesh_dst, boundary_dst);
343 if (mapped_point_dst == NULL) goto error;
344
345 intra_tbl_dst =
346 HECMW_couple_make_intra_iftable(mesh_dst, boundary_dst, comm_dst);
347 if (intra_tbl_dst == NULL) goto error;
348 }
349
350 /* make interface table for inter-communication */
351 inter_tbl = HECMW_couple_set_map_data(mesh_src, mesh_dst, comm_src, comm_dst,
352 intercomm, boundary_src, bbox_src,
353 bbox_dst, bgcell_src, mapped_point_dst);
354 if (inter_tbl == NULL) goto error;
355
356 /* make weight list */
357 if (comm_src->is_member) {
358 ip_list_pre = HECMW_couple_make_pre_ip_list(mesh_src, boundary_src,
359 comm_src, intra_tbl_src);
360 if (ip_list_pre == NULL) goto error;
361 }
362
363 ip_list_main = HECMW_couple_make_main_ip_list(
364 mesh_src, mesh_dst, boundary_src, boundary_dst, mapped_point_dst,
365 comm_src, comm_dst, intercomm, inter_tbl);
366 if (ip_list_main == NULL) goto error;
367
368 if (comm_dst->is_member) {
369 ip_list_post = HECMW_couple_make_post_ip_list(
370 mesh_dst, boundary_dst, mapped_point_dst, comm_dst, intra_tbl_dst);
371 if (ip_list_post == NULL) goto error;
372 }
373
374 if (add_couple_list(boundary_id, unit_specifier_src, unit_specifier_dst,
375 comm_src, comm_dst, intercomm, boundary_src, boundary_dst,
376 intra_tbl_src, intra_tbl_dst, inter_tbl, mapped_point_dst,
377 ip_list_pre, ip_list_main, ip_list_post))
378 goto error;
379
384 return HECMW_SUCCESS;
385
386error:
391 return HECMW_ERROR;
392}
#define HECMW_ERROR
Definition: hecmw_config.h:66
#define HECMW_SUCCESS
Definition: hecmw_config.h:64
#define HECMW_NAME_LEN
Definition: hecmw_config.h:70
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)
void HECMW_couple_free_boundary_info(struct hecmw_couple_boundary *boundary)
struct hecmw_couple_boundary * HECMW_couple_set_boundary_info(const char *boundary_id, int unit_specifier, const struct hecmwST_local_mesh *mesh)
void HECMW_couple_free_bounding_box(struct hecmw_couple_bounding_box *bbox)
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)
int HECMW_couple_ctrl_get_direction(const char *boundary_id, int *direction)
#define HECMW_COUPLE_UNIT1
#define HECMW_COUPLE_UNIT_UNDEF
#define HECMW_COUPLE_UNIT2_TO_UNIT1
#define HECMWCPL_E_INVALID_ARG
#define HECMWCPL_E
#define HECMW_COUPLE_UNIT1_TO_UNIT2
#define HECMW_COUPLE_UNIT2
#define HECMWCPL_E_INVALID_DIRECTION
int HECMW_couple_is_unit_member(const char *boundary_id, int unit_specifier)
struct hecmw_couple_comm * HECMW_couple_get_intercomm(const char *boundary_id)
struct hecmw_couple_comm * HECMW_couple_get_intracomm(const char *boundary_id, int unit_specifier)
void HECMW_couple_free_comm(struct hecmw_couple_comm *comm)
char * HECMW_couple_get_unit_id(const char *boundary_id, int unit_specifier, char *buf, int bufsize)
struct couple_info couple_list
int HECMW_couple_init(const char *boundary_id, struct hecmwST_local_mesh *mesh_unit1, struct hecmwST_local_mesh *mesh_unit2)
struct hecmw_couple_info * HECMW_couple_get_info(const char *boundary_id)
void HECMW_couple_free_init(const char *boundary_id)
void HECMW_couple_free_inter_iftable(struct hecmw_couple_inter_iftable *p)
struct hecmw_couple_inter_iftable * HECMW_couple_set_map_data(const struct hecmwST_local_mesh *mesh_src, const struct hecmwST_local_mesh *mesh_dst, const struct hecmw_couple_comm *comm_src, const struct hecmw_couple_comm *comm_dst, const struct hecmw_couple_comm *intercomm, const struct hecmw_couple_boundary *boundary_src, const struct hecmw_couple_bounding_box *bbox_src, const struct hecmw_couple_bounding_box *bbox_dst, const struct hecmw_couple_background_cell *bgcell_src, const struct hecmw_couple_mapped_point *mapped_point)
struct hecmw_couple_weight_list * HECMW_couple_make_main_ip_list(const struct hecmwST_local_mesh *mesh_src, const struct hecmwST_local_mesh *mesh_dst, const struct hecmw_couple_boundary *boundary_src, const struct hecmw_couple_boundary *boundary_dst, const struct hecmw_couple_mapped_point *mapped_point, const struct hecmw_couple_comm *comm_src, const struct hecmw_couple_comm *comm_dst, const struct hecmw_couple_comm *intercomm, const struct hecmw_couple_inter_iftable *inter_tbl)
struct hecmw_couple_weight_list * HECMW_couple_make_pre_ip_list(const struct hecmwST_local_mesh *mesh_src, const struct hecmw_couple_boundary *boundary_src, const struct hecmw_couple_comm *comm_src, const struct hecmw_couple_intra_iftable *intra_tbl_src)
struct hecmw_couple_weight_list * HECMW_couple_make_post_ip_list(const struct hecmwST_local_mesh *mesh_dst, const struct hecmw_couple_boundary *boundary_dst, const struct hecmw_couple_mapped_point *mapped_point, const struct hecmw_couple_comm *comm_dst, const struct hecmw_couple_intra_iftable *intra_tbl_dst)
void HECMW_couple_free_intra_iftable(struct hecmw_couple_intra_iftable *p)
struct hecmw_couple_intra_iftable * HECMW_couple_make_intra_iftable(const struct hecmwST_local_mesh *mesh, const struct hecmw_couple_boundary *boundary, const struct hecmw_couple_comm *intracomm)
void HECMW_couple_free_mapped_point(struct hecmw_couple_mapped_point *p)
struct hecmw_couple_mapped_point * HECMW_couple_set_mapped_point(const char *boundary_id, const struct hecmwST_local_mesh *mesh_dst, const struct hecmw_couple_boundary *boundary_dst)
void HECMW_couple_free_weight_list(struct hecmw_couple_weight_list *r)
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_strdup(s)
Definition: hecmw_malloc.h:23
#define HECMW_malloc(size)
Definition: hecmw_malloc.h:20
struct hecmw_couple_intra_iftable * intra_tbl_dst
struct hecmw_couple_mapped_point * mapped_point
struct hecmw_couple_weight_list * ip_list_pre
struct hecmw_couple_comm * comm_src
struct hecmw_couple_comm * comm_dst
struct hecmw_couple_comm * intercomm
struct hecmw_couple_boundary * boundary_dst
struct hecmw_couple_weight_list * ip_list_post
struct hecmw_couple_weight_list * ip_list_main
struct hecmw_couple_boundary * boundary_src
struct hecmw_couple_intra_iftable * intra_tbl_src
struct hecmw_couple_inter_iftable * inter_tbl
struct hecmw_couple_weight_list * next