FrontISTR  5.7.1
Large-scale structural analysis program with finit element method
hecmw_result_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 "hecmw_struct.h"
9 #include "hecmw_util.h"
10 #include "hecmw_result.h"
11 
12 static struct hecmwST_result_data *Result;
13 static int NNode, NElem;
14 
15 /*-----------------------------------------------------------------------------
16  * SetFunc
17  */
18 
19 static int set_ng_component(void *dst) {
20  *((int *)dst) = Result->ng_component;
21  return 0;
22 }
23 
24 static int set_ng_dof(void *dst) {
25  void *src;
26  int size;
27 
28  if (Result->ng_component <= 0) return 0;
29 
30  src = Result->ng_dof;
31  size = sizeof(*Result->ng_dof) * Result->ng_component;
32  memcpy(dst, src, size);
33 
34  return 0;
35 }
36 
37 static int set_global_label(void *dst) {
38  int i;
39 
40  if (Result->ng_component <= 0) return 0;
41 
42  for (i = 0; i < Result->ng_component; i++) {
43  char *dst_point = (char *)dst + HECMW_NAME_LEN * i;
44  char *src = Result->global_label[i];
45  HECMW_strcpy_c2f(src, dst_point, HECMW_NAME_LEN);
46  }
47 
48  return 0;
49 }
50 
51 static int set_global_val_item(void *dst) {
52  void *src;
53  int i, n, size;
54 
55  if (Result->ng_component <= 0) return 0;
56 
57  n = 0;
58  for (i = 0; i < Result->ng_component; i++) {
59  n += Result->ng_dof[i];
60  }
61  src = Result->global_val_item;
62  size = sizeof(*Result->global_val_item) * n;
63  memcpy(dst, src, size);
64 
65  return 0;
66 }
67 
68 static int set_nn_component(void *dst) {
69  *((int *)dst) = Result->nn_component;
70  return 0;
71 }
72 
73 static int set_nn_dof(void *dst) {
74  void *src;
75  int size;
76 
77  if (Result->nn_component <= 0) return 0;
78 
79  src = Result->nn_dof;
80  size = sizeof(*Result->nn_dof) * Result->nn_component;
81  memcpy(dst, src, size);
82 
83  return 0;
84 }
85 
86 static int set_node_label(void *dst) {
87  int i;
88 
89  if (Result->nn_component <= 0) return 0;
90 
91  for (i = 0; i < Result->nn_component; i++) {
92  char *dst_point = (char *)dst + HECMW_NAME_LEN * i;
93  char *src = Result->node_label[i];
94  HECMW_strcpy_c2f(src, dst_point, HECMW_NAME_LEN);
95  }
96 
97  return 0;
98 }
99 
100 static int set_node_val_item(void *dst) {
101  void *src;
102  int i, n, size;
103 
104  if (Result->nn_component <= 0) return 0;
105 
106  n = 0;
107  for (i = 0; i < Result->nn_component; i++) {
108  n += Result->nn_dof[i];
109  }
110  src = Result->node_val_item;
111  size = sizeof(*Result->node_val_item) * n * NNode;
112  memcpy(dst, src, size);
113 
114  return 0;
115 }
116 
117 static int set_ne_component(void *dst) {
118  *((int *)dst) = Result->ne_component;
119  return 0;
120 }
121 
122 static int set_ne_dof(void *dst) {
123  void *src;
124  int size;
125 
126  if (Result->ne_component <= 0) return 0;
127 
128  src = Result->ne_dof;
129  size = sizeof(*Result->ne_dof) * Result->ne_component;
130  memcpy(dst, src, size);
131 
132  return 0;
133 }
134 
135 static int set_elem_label(void *dst) {
136  int i;
137 
138  if (Result->ne_component <= 0) return 0;
139 
140  for (i = 0; i < Result->ne_component; i++) {
141  char *dst_point = (char *)dst + HECMW_NAME_LEN * i;
142  char *src = Result->elem_label[i];
143  HECMW_strcpy_c2f(src, dst_point, HECMW_NAME_LEN);
144  }
145 
146  return 0;
147 }
148 
149 static int set_elem_val_item(void *dst) {
150  void *src;
151  int i, n, size;
152 
153  if (Result->ne_component <= 0) return 0;
154 
155  n = 0;
156  for (i = 0; i < Result->ne_component; i++) {
157  n += Result->ne_dof[i];
158  }
159  src = Result->elem_val_item;
160  size = sizeof(*Result->elem_val_item) * n * NElem;
161  memcpy(dst, src, size);
162 
163  return 0;
164 }
165 
166 /*-----------------------------------------------------------------------------
167  * SetFunc table
168  */
169 
170 typedef int (*SetFunc)(void *);
171 
172 static struct func_table {
173  char *struct_name;
174  char *var_name;
175  SetFunc set_func;
176 } functions[] =
177  {
178  /* { Struct name, Variable name, memcpy function, check allocation
179  function }*/
180  {"hecmwST_result_data", "ng_component", set_ng_component},
181  {"hecmwST_result_data", "ng_dof", set_ng_dof},
182  {"hecmwST_result_data", "global_label", set_global_label},
183  {"hecmwST_result_data", "global_val_item", set_global_val_item},
184 
185  {"hecmwST_result_data", "nn_component", set_nn_component},
186  {"hecmwST_result_data", "nn_dof", set_nn_dof},
187  {"hecmwST_result_data", "node_label", set_node_label},
188  {"hecmwST_result_data", "node_val_item", set_node_val_item},
189 
190  {"hecmwST_result_data", "ne_component", set_ne_component},
191  {"hecmwST_result_data", "ne_dof", set_ne_dof},
192  {"hecmwST_result_data", "elem_label", set_elem_label},
193  {"hecmwST_result_data", "elem_val_item", set_elem_val_item},
194 };
195 
196 static const int NFUNC = sizeof(functions) / sizeof(functions[0]);
197 
198 static SetFunc get_set_func(char *struct_name, char *var_name) {
199  int i;
200 
201  for (i = 0; i < NFUNC; i++) {
202  if (strcmp(functions[i].struct_name, struct_name) == 0 &&
203  strcmp(functions[i].var_name, var_name) == 0) {
204  return functions[i].set_func;
205  }
206  }
207  return NULL;
208 }
209 
210 /*----------------------------------------------------------------------------*/
211 
213  int n_node, int n_elem) {
214  Result = result_data;
215  NNode = n_node;
216  NElem = n_elem;
217  return 0;
218 }
219 
221  Result = NULL;
222  return 0;
223 }
224 
225 /*----------------------------------------------------------------------------*/
226 
227 void hecmw_result_copy_c2f_set_if(char *struct_name, char *var_name, void *dst,
228  int *err, int len_struct, int len_var) {
229  SetFunc func;
230  char sname[HECMW_NAME_LEN + 1];
231  char vname[HECMW_NAME_LEN + 1];
232 
233  *err = 1;
234 
235  if (Result == NULL) {
238  "hecmw_result_copy_c2f_set_if(): 'result' has not initialized yet");
239  return;
240  }
241  if (struct_name == NULL) {
243  "hecmw_result_copy_c2f_set_if(): 'sname' is NULL");
244  return;
245  }
246  if (var_name == NULL) {
248  "hecmw_result_copy_c2f_set_if(): 'vname' is NULL");
249  return;
250  }
251  if (dst == NULL) {
253  "hecmw_result_copy_c2f_set_if(): 'dst' is NULL");
254  return;
255  }
256 
257  if (HECMW_strcpy_f2c_r(struct_name, len_struct, sname, sizeof(sname)) ==
258  NULL) {
259  return;
260  }
261  if (HECMW_strcpy_f2c_r(var_name, len_var, vname, sizeof(vname)) == NULL) {
262  return;
263  }
264 
265  func = get_set_func(sname, vname);
266  if (func == NULL) {
268  "hecmw_result_copy_c2f_set_if(): SetFunc not found");
269  return;
270  }
271 
272  if ((*func)(dst)) {
273  return;
274  }
275 
276  *err = 0;
277 }
278 
279 /*----------------------------------------------------------------------------*/
280 
281 void hecmw_result_read_by_name_if(char *name_ID, int *i_step,
282  int *n_node, int *n_elem, int *err, int len) {
283  char name_ID_str[HECMW_NAME_LEN + 1];
284 
285  *err = 1;
286 
287  if (HECMW_strcpy_f2c_r(name_ID, len, name_ID_str, sizeof(name_ID_str)) ==
288  NULL)
289  return;
290 
291  Result = HECMW_result_read_by_name(name_ID_str, *i_step);
292  if (Result == NULL) return;
293 
294  NNode = HECMW_result_get_nnode();
295  NElem = HECMW_result_get_nelem();
296  *n_node = NNode;
297  *n_elem = NElem;
298 
299  *err = 0;
300 }
301 
302 /*----------------------------------------------------------------------------*/
303 
305  *err = 1;
309  *err = 0;
310 }
#define HECMW_NAME_LEN
Definition: hecmw_config.h:70
int HECMW_set_error(int errorno, const char *fmt,...)
Definition: hecmw_error.c:37
#define NULL
int HECMW_strcpy_c2f(const char *cstr, char *fstr, int flen)
Definition: hecmw_lib_fc.c:72
char * HECMW_strcpy_f2c_r(const char *fstr, int flen, char *buf, int bufsize)
Definition: hecmw_lib_fc.c:45
#define HECMW_ALL_E0101
Definition: hecmw_msgno.h:8
#define HECMW_ALL_E0102
Definition: hecmw_msgno.h:9
int HECMW_result_get_nnode(void)
Definition: hecmw_result.c:200
void HECMW_result_free_nodeID(void)
Definition: hecmw_result.c:230
int HECMW_result_get_nelem(void)
Definition: hecmw_result.c:202
void HECMW_result_free(struct hecmwST_result_data *result)
Definition: hecmw_result.c:21
struct hecmwST_result_data * HECMW_result_read_by_name(char *name_ID, int i_step)
Definition: hecmw_result.c:177
void HECMW_result_free_elemID(void)
Definition: hecmw_result.c:235
int HECMW_result_copy_c2f_finalize(void)
void hecmw_result_read_finalize_if(int *err)
int HECMW_result_copy_c2f_init(struct hecmwST_result_data *result_data, int n_node, int n_elem)
void hecmw_result_copy_c2f_set_if(char *struct_name, char *var_name, void *dst, int *err, int len_struct, int len_var)
int(* SetFunc)(void *)
void hecmw_result_read_by_name_if(char *name_ID, int *i_step, int *n_node, int *n_elem, int *err, int len)
struct _result_struct Result