aboutsummaryrefslogtreecommitdiff
path: root/src/Register.c
blob: bb9ff05e008a5c6ebc0fe899547f33f5c7936bf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
 /*@@
   @file      Register.c
   @date      Tue Dec 14 20:55:06 1999
   @author    Tom Goodale
   @desc 
   Functions for registering and calling EOS routines
   @enddesc 
 @@*/

#include <stdlib.h>

#include "cctk.h"
#include "cctk_FortranString.h"

#include "StoreHandledData.h"

#include "EOS_Base.h"

static cHandledData *methods = NULL;

enum call_methods {none, c, f} ;

typedef struct 
{
  enum call_methods lang;

  CCTK_REAL (*f)(CCTK_REAL *, CCTK_REAL *);
  CCTK_REAL (*c)(CCTK_REAL, CCTK_REAL);
} func_t;

typedef struct 
{
  func_t Pressure;
  func_t SpecificIntEnergy;
  func_t RestMassDens;
  func_t DPressByDRho;
  func_t DPressByDEps;
} method_t;

 /*@@
   @routine    EOS_RegisterMethod
   @date       Tue Dec 14 22:04:23 1999
   @author     Tom Goodale
   @desc 
   Registers an EOS method
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
int EOS_RegisterMethod(const char *name)
{
  int handle;

  method_t *method;

  method = (method_t *)malloc(sizeof(method_t));

  if(method)
  {
    method->Pressure.lang = none;
    method->SpecificIntEnergy.lang = none;
    method->RestMassDens.lang = none;
    method->DPressByDRho.lang = none;
    method->DPressByDEps.lang = none;
  
    handle = Util_NewHandle(&methods, name, (void *)method);
  }
  else
  {
    handle = -1;
  }

  return handle;
}

void CCTK_FCALL CCTK_FNAME(EOS_RegisterMethod)(int *handle, ONE_FORTSTRING_ARG);
void CCTK_FCALL CCTK_FNAME(EOS_RegisterMethod)(int *handle, ONE_FORTSTRING_ARG)
{
  ONE_FORTSTRING_CREATE(name)

  *handle = EOS_RegisterMethod(name);

  free(name);
}


 /*@@
   @routine    EOS_Handle
   @date       Tue Dec 14 22:04:47 1999
   @author     Tom Goodale
   @desc 
   Gets the handle associated with an EOS method
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
int EOS_Handle(const char *name)
{
  int handle;
  method_t method;

  handle = Util_GetHandle(methods, name, (void *)&method);

  return handle;
}

void CCTK_FCALL CCTK_FNAME(EOS_Handle)(int *handle, ONE_FORTSTRING_ARG);
void CCTK_FCALL CCTK_FNAME(EOS_Handle)(int *handle, ONE_FORTSTRING_ARG)
{
  ONE_FORTSTRING_CREATE(name)

  *handle = EOS_Handle(name);

  free(name);
}

#define REGISTER_FUNCTION(x) \
int EOS_Register ## x (int handle, CCTK_REAL (*func)(CCTK_REAL, CCTK_REAL)) \
{                                                                           \
  int retval;                                                               \
  method_t *method;                                                         \
                                                                            \
  method = (method_t *)Util_GetHandledData(methods, handle);                \
                                                                            \
  if(method)                                                                \
  {                                                                         \
    method->x.lang = c;                                                     \
    method->x.c = func;                                                     \
    retval = 0;                                                             \
  }                                                                         \
  else                                                                      \
  {                                                                         \
    retval = 1;                                                             \
  }                                                                         \
                                                                            \
  return retval;                                                            \
}

/* Functions to register C EOS functions */
REGISTER_FUNCTION(Pressure)
REGISTER_FUNCTION(SpecificIntEnergy)
REGISTER_FUNCTION(RestMassDens)
REGISTER_FUNCTION(DPressByDRho)
REGISTER_FUNCTION(DPressByDEps)


#define REGISTER_FORTRAN_FUNCTION_ARGS \
(int *retval, int *handle, CCTK_REAL (*func)(CCTK_REAL *, CCTK_REAL *))
#define REGISTER_FORTRAN_FUNCTION(x)\
REGISTER_FORTRAN_FUNCTION_ARGS                                         \
{                                                                      \
  method_t *method;                                                    \
                                                                       \
  method = (method_t *)Util_GetHandledData(methods, *handle);          \
                                                                       \
  if(method)                                                           \
  {                                                                    \
    method->x.lang = f;                                                \
    method->x.f = func;                                                \
    *retval = 0;                                                       \
  }                                                                    \
  else                                                                 \
  {                                                                    \
    *retval = 1;                                                       \
  }                                                                    \
                                                                       \
}

/* Functions to register Fortran EOS functions */
void CCTK_FCALL CCTK_FNAME(EOS_RegisterPressure) REGISTER_FORTRAN_FUNCTION_ARGS;
void CCTK_FCALL CCTK_FNAME(EOS_RegisterPressure) REGISTER_FORTRAN_FUNCTION(Pressure)
void CCTK_FCALL CCTK_FNAME(EOS_RegisterSpecificIntEnergy) REGISTER_FORTRAN_FUNCTION_ARGS;
void CCTK_FCALL CCTK_FNAME(EOS_RegisterSpecificIntEnergy) REGISTER_FORTRAN_FUNCTION(SpecificIntEnergy)
void CCTK_FCALL CCTK_FNAME(EOS_RegisterRestMassDens) REGISTER_FORTRAN_FUNCTION_ARGS;
void CCTK_FCALL CCTK_FNAME(EOS_RegisterRestMassDens) REGISTER_FORTRAN_FUNCTION(RestMassDens)
void CCTK_FCALL CCTK_FNAME(EOS_RegisterDPressByDRho) REGISTER_FORTRAN_FUNCTION_ARGS;
void CCTK_FCALL CCTK_FNAME(EOS_RegisterDPressByDRho) REGISTER_FORTRAN_FUNCTION(DPressByDRho)
void CCTK_FCALL CCTK_FNAME(EOS_RegisterDPressByDEps) REGISTER_FORTRAN_FUNCTION_ARGS;
void CCTK_FCALL CCTK_FNAME(EOS_RegisterDPressByDEps) REGISTER_FORTRAN_FUNCTION(DPressByDEps)

#define CALL_FUNC(x) \
CCTK_REAL EOS_ ## x (int handle, CCTK_REAL a, CCTK_REAL b) \
{                                                                           \
  CCTK_REAL retval;                                                         \
  method_t *method;                                                         \
                                                                            \
  method = (method_t *)Util_GetHandledData(methods, handle);                \
                                                                            \
  if(method && method->x.lang != none)                                      \
  {                                                                         \
    if(method->x.lang == c)                                                 \
    {                                                                       \
      retval = method->x.c(a,b);                                            \
    }                                                                       \
    else                                                                    \
    {                                                                       \
      retval = method->x.f(&a, &b);                                         \
    }                                                                       \
  }                                                                         \
  else                                                                      \
  {                                                                         \
    retval = -1;                                                            \
  }                                                                         \
  return retval;                                                            \
}

/* Functions to call EOS functions from C */
CALL_FUNC(Pressure)
CALL_FUNC(SpecificIntEnergy)
CALL_FUNC(RestMassDens)
CALL_FUNC(DPressByDRho)
CALL_FUNC(DPressByDEps)

#define CALL_FORTRAN_FUNC_ARGS (int *handle, CCTK_REAL *a, CCTK_REAL *b)
#define CALL_FORTRAN_FUNC(x) \
CALL_FORTRAN_FUNC_ARGS                                                      \
{                                                                           \
  CCTK_REAL retval;                                                         \
  method_t *method;                                                         \
                                                                            \
  method = (method_t *)Util_GetHandledData(methods, *handle);               \
                                                                            \
  if(method && method->x.lang != none)                                      \
  {                                                                         \
    if(method->x.lang == c)                                                 \
    {                                                                       \
      retval = method->x.c(*a,*b);                                          \
    }                                                                       \
    else                                                                    \
    {                                                                       \
      retval = method->x.f(a, b);                                           \
    }                                                                       \
  }                                                                         \
  else                                                                      \
  {                                                                         \
    retval = -1;                                                            \
  }                                                                         \
  return retval;                                                            \
}

/* Functions to call EOS functions from Fortran*/
CCTK_REAL CCTK_FCALL CCTK_FNAME(EOS_Pressure) CALL_FORTRAN_FUNC_ARGS;
CCTK_REAL CCTK_FCALL CCTK_FNAME(EOS_Pressure) CALL_FORTRAN_FUNC(Pressure)
CCTK_REAL CCTK_FCALL CCTK_FNAME(EOS_SpecificIntEnergy) CALL_FORTRAN_FUNC_ARGS;
CCTK_REAL CCTK_FCALL CCTK_FNAME(EOS_SpecificIntEnergy) CALL_FORTRAN_FUNC(SpecificIntEnergy)
CCTK_REAL CCTK_FCALL CCTK_FNAME(EOS_RestMassDens) CALL_FORTRAN_FUNC_ARGS;
CCTK_REAL CCTK_FCALL CCTK_FNAME(EOS_RestMassDens) CALL_FORTRAN_FUNC(RestMassDens)
CCTK_REAL CCTK_FCALL CCTK_FNAME(EOS_DPressByDRho) CALL_FORTRAN_FUNC_ARGS;
CCTK_REAL CCTK_FCALL CCTK_FNAME(EOS_DPressByDRho) CALL_FORTRAN_FUNC(DPressByDRho)
CCTK_REAL CCTK_FCALL CCTK_FNAME(EOS_DPressByDEps) CALL_FORTRAN_FUNC_ARGS;
CCTK_REAL CCTK_FCALL CCTK_FNAME(EOS_DPressByDEps) CALL_FORTRAN_FUNC(DPressByDEps)