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