FrontISTR  5.7.0
Large-scale structural analysis program with finit element method
hecmw_couple_copy_c2f.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 
10 #include "hecmw_struct.h"
11 #include "hecmw_couple_define.h"
12 #include "hecmw_couple_startup.h"
13 
14 static struct hecmw_couple_value *couple_value;
15 
16 /*================================================================================================*/
17 static int set_n(void *dst) {
18  void *src;
19  int size;
20 
21  src = &couple_value->n;
22  size = sizeof(couple_value->n);
23  memcpy(dst, src, size);
24 
25  return 0;
26 }
27 
28 static int set_item_type(void *dst) {
29  void *src;
30  int size;
31 
32  src = &couple_value->item_type;
33  size = sizeof(couple_value->item_type);
34  memcpy(dst, src, size);
35 
36  return 0;
37 }
38 
39 static int set_n_dof(void *dst) {
40  void *src;
41  int size;
42 
43  src = &couple_value->n_dof;
44  size = sizeof(couple_value->n_dof);
45  memcpy(dst, src, size);
46 
47  return 0;
48 }
49 
50 static int set_item(void *dst) {
51  void *src;
52  int size;
53 
54  if (couple_value->n <= 0) return 0;
55 
56  src = couple_value->item;
57  if (couple_value->item_type == HECMW_COUPLE_NODE_GROUP) {
58  size = sizeof(*couple_value->item) * couple_value->n;
59  } else if (couple_value->item_type == HECMW_COUPLE_ELEMENT_GROUP) {
60  size = sizeof(*couple_value->item) * couple_value->n;
61  } else if (couple_value->item_type == HECMW_COUPLE_SURFACE_GROUP) {
62  size = sizeof(*couple_value->item) * couple_value->n * 2;
63  } else {
64  return 0;
65  }
66  memcpy(dst, src, size);
67 
68  return 0;
69 }
70 
71 static int set_value(void *dst) {
72  void *src;
73  int size;
74 
75  if (couple_value->n <= 0 && couple_value->n_dof <= 0) return 0;
76 
77  src = couple_value->value;
78  size = sizeof(*couple_value->value) * couple_value->n * couple_value->n_dof;
79  memcpy(dst, src, size);
80 
81  return 0;
82 }
83 
84 /*------------------------------------------------------------------------------------------------*/
85 static int is_alloc_item(void) { return couple_value->item ? 1 : 0; }
86 
87 static int is_alloc_value(void) { return couple_value->value ? 1 : 0; }
88 
89 /*------------------------------------------------------------------------------------------------*/
90 typedef int (*SetFunc)(void *);
91 typedef int (*IsAllocatedFunc)(void);
92 
93 static struct func_table {
94  char *struct_name;
95  char *var_name;
96  SetFunc set_func;
97  IsAllocatedFunc is_allocated_func;
98 } functions[] =
99  {
100  /* { Struct name, Variable name, memcpy function, check allocation
101  function } */
102  {"hecmw_couple_value", "n", set_n, NULL},
103  {"hecmw_couple_value", "item_type", set_item_type, NULL},
104  {"hecmw_couple_value", "n_dof", set_n_dof, NULL},
105  {"hecmw_couple_value", "item", set_item, is_alloc_item},
106  {"hecmw_couple_value", "value", set_value, is_alloc_value},
107 };
108 
109 static const int NFUNC = sizeof(functions) / sizeof(functions[0]);
110 
111 static IsAllocatedFunc get_is_allocated_func(char *struct_name,
112  char *var_name) {
113  int i;
114 
115  for (i = 0; i < NFUNC; i++) {
116  if (strcmp(functions[i].struct_name, struct_name) == 0 &&
117  strcmp(functions[i].var_name, var_name) == 0) {
118  return functions[i].is_allocated_func;
119  }
120  }
121  return NULL;
122 }
123 
124 static SetFunc get_set_func(char *struct_name, char *var_name) {
125  int i;
126 
127  for (i = 0; i < NFUNC; i++) {
128  if (strcmp(functions[i].struct_name, struct_name) == 0 &&
129  strcmp(functions[i].var_name, var_name) == 0) {
130  return functions[i].set_func;
131  }
132  }
133  return NULL;
134 }
135 
136 /*------------------------------------------------------------------------------------------------*/
137 
139  struct hecmw_couple_value *_couple_value) {
140  couple_value = _couple_value;
141  return 0;
142 }
143 
145  couple_value = NULL;
146  return 0;
147 }
148 
149 /*------------------------------------------------------------------------------------------------*/
150 
151 extern void hecmw_cpl_copy_c2f_isalloc_if(char *struct_name, char *var_name,
152  int *is_allocated, int *err, int slen,
153  int vlen) {
154  IsAllocatedFunc func;
155  char sname[HECMW_NAME_LEN + 1];
156  char vname[HECMW_NAME_LEN + 1];
157 
158  *err = 1;
159 
160  if (couple_value == NULL) {
162  "hecmw_cpl_copy_c2f_isalloc_if(): 'couple_value' is not "
163  "initialized yet");
164  return;
165  }
166  if (struct_name == NULL) {
168  "hecmw_cpl_copy_c2f_isalloc_if(): 'struct_name' is NULL");
169  return;
170  }
171  if (var_name == NULL) {
173  "hecmw_cpl_copy_c2f_isalloc_if(): 'var_name' is NULL");
174  return;
175  }
176  if (is_allocated == NULL) {
178  "hecmw_cpl_copy_c2f_isalloc_if(): 'is_allocated' is NULL");
179  return;
180  }
181 
182  if (HECMW_strcpy_f2c_r(struct_name, slen, sname, sizeof(sname)) == NULL)
183  return;
184  if (HECMW_strcpy_f2c_r(var_name, vlen, vname, sizeof(vname)) == NULL) return;
185 
186  if ((func = get_is_allocated_func(sname, vname)) == NULL) {
189  "hecmw_cpl_copy_c2f_isalloc_if(): IsAllocatedFunc not found");
190  return;
191  }
192 
193  if ((*func)()) {
194  *is_allocated = 1;
195  } else {
196  *is_allocated = 0;
197  }
198 
199  *err = 0;
200 }
201 
202 extern void hecmw_cpl_copy_c2f_isalloc_if_(char *struct_name, char *var_name,
203  int *is_allocated, int *err,
204  int slen, int vlen) {
205  hecmw_cpl_copy_c2f_isalloc_if(struct_name, var_name, is_allocated, err, slen,
206  vlen);
207 }
208 
209 extern void hecmw_cpl_copy_c2f_isalloc_if__(char *struct_name, char *var_name,
210  int *is_allocated, int *err,
211  int slen, int vlen) {
212  hecmw_cpl_copy_c2f_isalloc_if(struct_name, var_name, is_allocated, err, slen,
213  vlen);
214 }
215 
216 extern void HECMW_CPL_COPY_C2F_ISALLOC_IF(char *struct_name, char *var_name,
217  int *is_allocated, int *err, int slen,
218  int vlen) {
219  hecmw_cpl_copy_c2f_isalloc_if(struct_name, var_name, is_allocated, err, slen,
220  vlen);
221 }
222 
223 /*------------------------------------------------------------------------------------------------*/
224 
225 extern void hecmw_cpl_copy_c2f_set_if(char *struct_name, char *var_name,
226  void *dst, int *err, int slen, int vlen) {
227  SetFunc func;
228  char sname[HECMW_NAME_LEN + 1];
229  char vname[HECMW_NAME_LEN + 1];
230 
231  *err = 1;
232 
233  if (couple_value == NULL) {
236  "hecmw_cpl_copy_c2f_set_if(): 'couple_value' has not initialized yet");
237  return;
238  }
239  if (struct_name == NULL) {
241  "hecmw_cpl_copy_c2f_set_if(): 'struct_name' is NULL");
242  return;
243  }
244  if (var_name == NULL) {
246  "hecmw_cpl_copy_c2f_set_if(): 'var_name' is NULL");
247  return;
248  }
249  if (dst == NULL) {
251  "hecmw_cpl_copy_c2f_set_if(): 'dst' is NULL");
252  return;
253  }
254 
255  if (HECMW_strcpy_f2c_r(struct_name, slen, sname, sizeof(sname)) == NULL)
256  return;
257  if (HECMW_strcpy_f2c_r(var_name, vlen, vname, sizeof(vname)) == NULL) return;
258 
259  if ((func = get_set_func(sname, vname)) == NULL) {
261  "hecmw_cpl_copy_c2f_set_if(): SetFunc not found");
262  return;
263  }
264 
265  if ((*func)(dst)) return;
266 
267  *err = 0;
268 }
269 
270 extern void hecmw_cpl_copy_c2f_set_if_(char *struct_name, char *var_name,
271  void *dst, int *err, int slen,
272  int vlen) {
273  hecmw_cpl_copy_c2f_set_if(struct_name, var_name, dst, err, slen, vlen);
274 }
275 
276 extern void hecmw_cpl_copy_c2f_set_if__(char *struct_name, char *var_name,
277  void *dst, int *err, int slen,
278  int vlen) {
279  hecmw_cpl_copy_c2f_set_if(struct_name, var_name, dst, err, slen, vlen);
280 }
281 
282 extern void HECMW_CPL_COPY_C2F_SET_IF(char *struct_name, char *var_name,
283  void *dst, int *err, int slen, int vlen) {
284  hecmw_cpl_copy_c2f_set_if(struct_name, var_name, dst, err, slen, vlen);
285 }
hecmw_cpl_copy_c2f_isalloc_if
void hecmw_cpl_copy_c2f_isalloc_if(char *struct_name, char *var_name, int *is_allocated, int *err, int slen, int vlen)
Definition: hecmw_couple_copy_c2f.c:151
HECMWCPL_E_INVALID_NULL_PTR
#define HECMWCPL_E_INVALID_NULL_PTR
Definition: hecmw_couple_define.h:95
SetFunc
int(* SetFunc)(void *)
Definition: hecmw_couple_copy_c2f.c:90
HECMW_CPL_COPY_C2F_ISALLOC_IF
void HECMW_CPL_COPY_C2F_ISALLOC_IF(char *struct_name, char *var_name, int *is_allocated, int *err, int slen, int vlen)
Definition: hecmw_couple_copy_c2f.c:216
hecmw_cpl_copy_c2f_set_if
void hecmw_cpl_copy_c2f_set_if(char *struct_name, char *var_name, void *dst, int *err, int slen, int vlen)
Definition: hecmw_couple_copy_c2f.c:225
hecmw_cpl_copy_c2f_isalloc_if__
void hecmw_cpl_copy_c2f_isalloc_if__(char *struct_name, char *var_name, int *is_allocated, int *err, int slen, int vlen)
Definition: hecmw_couple_copy_c2f.c:209
HECMW_couple_copy_c2f_init
int HECMW_couple_copy_c2f_init(struct hecmw_couple_value *_couple_value)
Definition: hecmw_couple_copy_c2f.c:138
hecmw_couple_value::n
int n
Definition: hecmw_couple_startup.h:13
hecmw_couple_value::value
double * value
Definition: hecmw_couple_startup.h:17
HECMW_COUPLE_ELEMENT_GROUP
#define HECMW_COUPLE_ELEMENT_GROUP
Definition: hecmw_couple_define.h:37
hecmw_couple_value::item_type
int item_type
Definition: hecmw_couple_startup.h:14
hecmw_struct.h
hecmw_cpl_copy_c2f_set_if_
void hecmw_cpl_copy_c2f_set_if_(char *struct_name, char *var_name, void *dst, int *err, int slen, int vlen)
Definition: hecmw_couple_copy_c2f.c:270
hecmw_cpl_copy_c2f_set_if__
void hecmw_cpl_copy_c2f_set_if__(char *struct_name, char *var_name, void *dst, int *err, int slen, int vlen)
Definition: hecmw_couple_copy_c2f.c:276
HECMW_NAME_LEN
#define HECMW_NAME_LEN
Definition: hecmw_config.h:70
HECMW_COUPLE_NODE_GROUP
#define HECMW_COUPLE_NODE_GROUP
Definition: hecmw_couple_define.h:35
HECMW_COUPLE_SURFACE_GROUP
#define HECMW_COUPLE_SURFACE_GROUP
Definition: hecmw_couple_define.h:39
hecmw_couple_startup.h
hecmw_couple_value::n_dof
int n_dof
Definition: hecmw_couple_startup.h:15
HECMW_CPL_COPY_C2F_SET_IF
void HECMW_CPL_COPY_C2F_SET_IF(char *struct_name, char *var_name, void *dst, int *err, int slen, int vlen)
Definition: hecmw_couple_copy_c2f.c:282
hecmw_cpl_copy_c2f_isalloc_if_
void hecmw_cpl_copy_c2f_isalloc_if_(char *struct_name, char *var_name, int *is_allocated, int *err, int slen, int vlen)
Definition: hecmw_couple_copy_c2f.c:202
hecmw_couple_value::item
int * item
Definition: hecmw_couple_startup.h:16
HECMW_strcpy_f2c_r
char * HECMW_strcpy_f2c_r(const char *fstr, int flen, char *buf, int bufsize)
Definition: hecmw_lib_fc.c:45
hecmw_couple_value
Definition: hecmw_couple_startup.h:9
HECMW_set_error
int HECMW_set_error(int errorno, const char *fmt,...)
Definition: hecmw_error.c:37
NULL
#define NULL
Definition: hecmw_io_nastran.c:30
HECMWCPL_E_INVALID_ARG
#define HECMWCPL_E_INVALID_ARG
Definition: hecmw_couple_define.h:91
IsAllocatedFunc
int(* IsAllocatedFunc)(void)
Definition: hecmw_couple_copy_c2f.c:91
hecmw_couple_define.h
HECMW_couple_copy_c2f_finalize
int HECMW_couple_copy_c2f_finalize(void)
Definition: hecmw_couple_copy_c2f.c:144