FrontISTR  5.7.0
Large-scale structural analysis program with finit element method
hecmw_couple_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 
11 #include "hecmw_malloc.h"
12 #include "hecmw_lib_fc.h"
13 #include "hecmw_msgno.h"
14 #include "hecmw_struct.h"
15 #include "hecmw_couple_define.h"
16 #include "hecmw_couple_startup.h"
17 
18 static struct hecmw_couple_value *couple_value;
19 
20 /*------------------------------------------------------------------------------------------------*/
21 /*
22  * SetFunc
23  */
24 
25 static int set_n(void *src) {
26  couple_value->n = *((int *)src);
27  return 0;
28 }
29 
30 static int set_item_type(void *src) {
31  couple_value->item_type = *((int *)src);
32  return 0;
33 }
34 
35 static int set_n_dof(void *src) {
36  couple_value->n_dof = *((int *)src);
37  return 0;
38 }
39 
40 static int set_item(void *src) {
41  int size;
42 
43  if (couple_value->n <= 0) return 0;
44 
45  if (couple_value->item_type == HECMW_COUPLE_NODE_GROUP) {
46  size = sizeof(*couple_value->item) * couple_value->n;
47  } else if (couple_value->item_type == HECMW_COUPLE_ELEMENT_GROUP) {
48  size = sizeof(*couple_value->item) * couple_value->n;
49  } else if (couple_value->item_type == HECMW_COUPLE_SURFACE_GROUP) {
50  size = sizeof(*couple_value->item) * couple_value->n * 2;
51  } else {
52  return 0;
53  }
54  couple_value->item = HECMW_malloc(size);
55  if (couple_value->item == NULL) {
56  HECMW_set_error(errno, "");
57  return -1;
58  }
59  memcpy(couple_value->item, src, size);
60  return 0;
61 }
62 
63 static int set_value(void *src) {
64  int size;
65 
66  if (couple_value->n <= 0 || couple_value->n_dof <= 0) return 0;
67 
68  size = sizeof(*couple_value->value) * couple_value->n * couple_value->n_dof;
69  couple_value->value = HECMW_malloc(size);
70  if (couple_value->value == NULL) {
71  HECMW_set_error(errno, "");
72  return -1;
73  }
74  memcpy(couple_value->value, src, size);
75  return 0;
76 }
77 
78 /*------------------------------------------------------------------------------------------------*/
79 /*
80  * SetFunc table
81  */
82 
83 typedef int (*SetFunc)(void *);
84 
85 static struct func_table {
86  char *struct_name;
87  char *var_name;
88  SetFunc set_func;
89 } functions[] = {
90  /* { Struct_name, Variable name, memcpy function } */
91  {"hecmw_couple_value", "n", set_n},
92  {"hecmw_couple_value", "item_type", set_item_type},
93  {"hecmw_couple_value", "n_dof", set_n_dof},
94  {"hecmw_couple_value", "item", set_item},
95  {"hecmw_couple_value", "value", set_value},
96 };
97 
98 static const int NFUNC = sizeof(functions) / sizeof(functions[0]);
99 
100 static SetFunc get_set_func(char *struct_name, char *var_name) {
101  int i;
102 
103  for (i = 0; i < NFUNC; i++) {
104  if (strcmp(functions[i].struct_name, struct_name) == 0 &&
105  strcmp(functions[i].var_name, var_name) == 0) {
106  return functions[i].set_func;
107  }
108  }
109  return NULL;
110 }
111 
112 /*------------------------------------------------------------------------------------------------*/
113 
115  struct hecmw_couple_value *_couple_value) {
116  couple_value = _couple_value;
117  return 0;
118 }
119 
121  couple_value = NULL;
122  return 0;
123 }
124 
125 /*------------------------------------------------------------------------------------------------*/
126 
127 extern void hecmw_cpl_copy_f2c_set_if(char *struct_name, char *var_name,
128  void *src, int *err, int slen, int vlen) {
129  SetFunc func;
130  char sname[HECMW_NAME_LEN + 1];
131  char vname[HECMW_NAME_LEN + 1];
132 
133  *err = 1;
134 
135  if (couple_value == NULL) {
138  "hecmw_cpl_copy_f2c_set_if(): 'couple_value' has not initialized yet");
139  return;
140  }
141  if (struct_name == NULL) {
143  "hecmw_cpl_copy_f2c_set_if(): 'struct_name' is NULL");
144  return;
145  }
146  if (var_name == NULL) {
148  "hecmw_cpl_copy_f2c_set_if(): 'var_name' is NULL");
149  return;
150  }
151 
152  if (HECMW_strcpy_f2c_r(struct_name, slen, sname, sizeof(sname)) == NULL)
153  return;
154  if (HECMW_strcpy_f2c_r(var_name, vlen, vname, sizeof(vname)) == NULL) return;
155 
156  if ((func = get_set_func(sname, vname)) == NULL) {
158  "hecmw_cpl_copy_f2c_set_if(): SetFunc not found");
159  return;
160  }
161 
162  if ((*func)(src)) return;
163 
164  *err = 0;
165 }
166 
167 extern void hecmw_cpl_copy_f2c_set_if_(char *struct_name, char *var_name,
168  void *src, int *err, int slen,
169  int vlen) {
170  hecmw_cpl_copy_f2c_set_if(struct_name, var_name, src, err, slen, vlen);
171 }
172 
173 extern void hecmw_cpl_copy_f2c_set_if__(char *struct_name, char *var_name,
174  void *src, int *err, int slen,
175  int vlen) {
176  hecmw_cpl_copy_f2c_set_if(struct_name, var_name, src, err, slen, vlen);
177 }
178 
179 extern void HECMW_CPL_COPY_F2C_SET_IF(char *struct_name, char *var_name,
180  void *src, int *err, int slen, int vlen) {
181  hecmw_cpl_copy_f2c_set_if(struct_name, var_name, src, err, slen, vlen);
182 }
hecmw_malloc.h
HECMW_CPL_COPY_F2C_SET_IF
void HECMW_CPL_COPY_F2C_SET_IF(char *struct_name, char *var_name, void *src, int *err, int slen, int vlen)
Definition: hecmw_couple_copy_f2c.c:179
HECMW_couple_copy_f2c_finalize
int HECMW_couple_copy_f2c_finalize(void)
Definition: hecmw_couple_copy_f2c.c:120
HECMWCPL_E_INVALID_NULL_PTR
#define HECMWCPL_E_INVALID_NULL_PTR
Definition: hecmw_couple_define.h:95
HECMW_malloc
#define HECMW_malloc(size)
Definition: hecmw_malloc.h:20
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_cpl_copy_f2c_set_if__
void hecmw_cpl_copy_f2c_set_if__(char *struct_name, char *var_name, void *src, int *err, int slen, int vlen)
Definition: hecmw_couple_copy_f2c.c:173
hecmw_struct.h
HECMW_NAME_LEN
#define HECMW_NAME_LEN
Definition: hecmw_config.h:70
hecmw_msgno.h
HECMW_COUPLE_NODE_GROUP
#define HECMW_COUPLE_NODE_GROUP
Definition: hecmw_couple_define.h:35
HECMW_couple_copy_f2c_init
int HECMW_couple_copy_f2c_init(struct hecmw_couple_value *_couple_value)
Definition: hecmw_couple_copy_f2c.c:114
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_lib_fc.h
hecmw_cpl_copy_f2c_set_if_
void hecmw_cpl_copy_f2c_set_if_(char *struct_name, char *var_name, void *src, int *err, int slen, int vlen)
Definition: hecmw_couple_copy_f2c.c:167
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_cpl_copy_f2c_set_if
void hecmw_cpl_copy_f2c_set_if(char *struct_name, char *var_name, void *src, int *err, int slen, int vlen)
Definition: hecmw_couple_copy_f2c.c:127
HECMW_set_error
int HECMW_set_error(int errorno, const char *fmt,...)
Definition: hecmw_error.c:37
SetFunc
int(* SetFunc)(void *)
Definition: hecmw_couple_copy_f2c.c:83
NULL
#define NULL
Definition: hecmw_io_nastran.c:30
HECMWCPL_E_INVALID_ARG
#define HECMWCPL_E_INVALID_ARG
Definition: hecmw_couple_define.h:91
hecmw_couple_define.h