-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscvheos_calc_isentrope.c
268 lines (216 loc) · 7.37 KB
/
scvheos_calc_isentrope.c
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
261
262
263
264
265
266
267
268
/*
* Calculate u(rho) for constant entropy for the SCVH EOS.
*
* Author: Christian Reinhardt
* Created: 01.11.2022
* Modified:
*/
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <assert.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_odeiv2.h>
#include "scvheos.h"
/* Isentrope. */
struct ISENTROPE {
SCVHEOSMAT *Mat;
int n;
double *rho;
double *u;
double *T;
double *s;
};
/* Parameters for calculating the derivatives. */
struct deriv_params_type {
SCVHEOSMAT *Mat;
int iStatus;
};
/*
* This function calculates derivative
*
* du/drho = P/rho^2
*
* x: Integration variable rho
* y[]: Array containing u(rho)
* f[]: Array that returns du/drho
*/
int derivs(double x, const double y[], double f[], void *params) {
double u = y[0];
double rho = x;
struct deriv_params_type *deriv_params = (struct deriv_params_type *) params;
SCVHEOSMAT *Mat = deriv_params->Mat;
int iRet;
/* Be sure to reset iStatus. */
deriv_params->iStatus = GSL_SUCCESS;
iRet = scvheosIsInExtrapLimit(Mat, rho, u);
if (iRet != SCVHEOS_SUCCESS) {
// CR
fprintf(stderr, "derivs: iRet = %i\n", iRet);
switch(iRet) {
case SCVHEOS_OUTSIDE_RHOMIN:
fprintf(stderr, "rho= %15.7E logrho= %15.7E smaller than LogRhoMin= %15.7E\n",
rho, log10(rho), Mat->LogRhoMin);
break;
case SCVHEOS_OUTSIDE_RHOMAX:
fprintf(stderr, "rho= %15.7E logrho= %15.7E larger than LogRhoMax= %15.7E\n",
rho, log10(rho), Mat->LogRhoMax);
break;
case SCVHEOS_OUTSIDE_TMIN:
fprintf(stderr, "u= %15.7E logu= %15.7E smaller than LoguMin= %15.7E\n",
u, log10(u), scvheosLogUofLogRhoLogT(Mat, log10(rho), Mat->LogTMin));
break;
case SCVHEOS_OUTSIDE_TMAX:
fprintf(stderr, "u= %15.7E logu= %15.7E larger than LoguMax= %15.7E\n",
u, log10(u), scvheosLogUofLogRhoLogT(Mat, log10(rho), Mat->LogTMax));
break;
}
/* Return the error code from EOSlib. */
deriv_params->iStatus = iRet;
return GSL_EBADFUNC;
}
f[0] = scvheosPofRhoU(Mat, rho, u)/(rho*rho);
return GSL_SUCCESS;
}
/*
* Check if a value (rho, T) is inside of the extrapolation limits.
*/
int scvheosIsInExtrapLimitRhoT(SCVHEOSMAT *Mat, double rho, double T) {
double logrho = log10(rho);
double logT = log10(T);
if (logrho < Mat->LogRhoMin) return SCVHEOS_OUTSIDE_RHOMIN;
if (logrho > Mat->LogRhoMax) return SCVHEOS_OUTSIDE_RHOMAX;
if (logT < Mat->LogTMin) return SCVHEOS_OUTSIDE_TMIN;
if (logT > Mat->LogTMax) return SCVHEOS_OUTSIDE_TMAX;
return SCVHEOS_SUCCESS;
}
struct ISENTROPE *SolveIsentrope(SCVHEOSMAT *Mat, double *rhoaxis, double Ti, int nRho) {
double rho;
double u;
double T;
struct ISENTROPE *isentrope;
/* ODE system. */
size_t ndim = 1;
struct deriv_params_type deriv_params = {Mat, GSL_SUCCESS};
gsl_odeiv2_system sys = {derivs, NULL, ndim, &deriv_params};
/* Driver. */
const gsl_odeiv2_step_type *step_type = gsl_odeiv2_step_rkf45;
double eps_abs = 0.0;
double eps_rel = 1e-8;
double h;
gsl_odeiv2_driver *driver;
int iRet;
assert(rhoaxis != NULL);
assert(nRho > 1);
/* Assume that the points are equally spaced. */
h = (rhoaxis[1]-rhoaxis[0])*1e-6;
driver = gsl_odeiv2_driver_alloc_y_new(&sys, step_type, h, eps_abs, eps_rel);
isentrope = (struct ISENTROPE *) calloc(1, sizeof(struct ISENTROPE));
assert(isentrope != NULL);
isentrope->rho = (double *) calloc(nRho, sizeof(double));
assert(isentrope->rho != NULL);
isentrope->u = (double *) calloc(nRho, sizeof(double));
assert(isentrope->u != NULL);
isentrope->T = (double *) calloc(nRho, sizeof(double));
assert(isentrope->T != NULL);
isentrope->s = (double *) calloc(nRho, sizeof(double));
assert(isentrope->s != NULL);
/* Initial values. */
rho = rhoaxis[0];
T = Ti;
iRet = scvheosIsInExtrapLimitRhoT(Mat, rho, T);
switch(iRet) {
case SCVHEOS_OUTSIDE_RHOMIN:
fprintf(stderr, "rho= %15.7E logrho= %15.7E smaller than LogRhoMin= %15.7E\n",
rho, log10(rho), Mat->LogRhoMin);
break;
case SCVHEOS_OUTSIDE_RHOMAX:
fprintf(stderr, "rho= %15.7E logrho= %15.7E larger than LogRhoMax= %15.7E\n",
rho, log10(rho), Mat->LogRhoMax);
break;
case SCVHEOS_OUTSIDE_TMIN:
fprintf(stderr, "T= %15.7E logT= %15.7E smaller than LogTMin= %15.7E\n",
T, log10(T), Mat->LogTMin);
break;
case SCVHEOS_OUTSIDE_TMAX:
fprintf(stderr, "T= %15.7E logT= %15.7E larger than LogTMax= %15.7E\n",
T, log10(T), Mat->LogTMax);
break;
}
// CR
fprintf(stderr, "Initial values:\n");
fprintf(stderr, "rho=%15.7E T=%15.7E\n", rho, T);
u = scvheosUofRhoT(Mat, rho, T);
isentrope->Mat = Mat;
isentrope->n = nRho;
isentrope->rho[0] = rho;
isentrope->u[0] = u;
isentrope->T[0] = T;
isentrope->s[0] = scvheosSofRhoT(Mat, rho, T);
for (int i=1; i<nRho; i++) {
int status = gsl_odeiv2_driver_apply(driver, &rho, rhoaxis[i], &u);
if (status != GSL_SUCCESS) {
fprintf(stderr, "Error: %i (GSL) %i (EOSlib)\n", status, deriv_params.iStatus);
free(isentrope->rho);
free(isentrope->u);
free(isentrope->T);
free(isentrope->s);
free(isentrope);
return NULL;
}
T = scvheosTofRhoU(Mat, rho, u);
isentrope->rho[i] = rho;
isentrope->u[i] = u;
isentrope->T[i] = T;
isentrope->s[i] = scvheosSofRhoT(Mat, rho, T);
}
return isentrope;
}
int PrintIsentrope(struct ISENTROPE *isentrope) {
fprintf(stdout, "#%14s%15s%15s%15s\n", "rho", "u", "T", "s");
for (int i=0; i<isentrope->n; i++) {
fprintf(stdout, "%15.7E%15.7E%15.7E%15.7E\n", isentrope->rho[i], isentrope->u[i], isentrope->T[i], isentrope->s[i]);
}
return 0;
}
int main(int argc, char **argv) {
SCVHEOSMAT *Mat;
int iMat = SCVHEOS_HHE_LOWRHOT;
double dKpcUnit = 2.06701e-13;
double dMsolUnit = 4.80438e-08;
struct ISENTROPE *isentrope;
double rhoi;
double Ti;
double rhof;
double *rhoaxis;
int nRho;
dKpcUnit = 0.0;
dMsolUnit = 0.0;
//dKpcUnit = 4.84821E-09;
//dMsolUnit = 9.53869E-04;
if (argc != 5) {
fprintf(stderr, "Usage: scvheos_calc_isentrope <rhoi> <Ti> <rhof> <nRho>\n");
exit(1);
}
rhoi = atof(argv[1]);
Ti = atof(argv[2]);
rhof = atof(argv[3]);
nRho = atoi(argv[4]);
assert(rhoi > 0.0);
assert(Ti > 0.0);
assert(rhof > 0.0);
assert(nRho > 1);
rhoaxis = (double *) calloc(nRho, sizeof(double));
assert(rhoaxis != NULL);
/* Generate logarithmic axis. */
for (int i=0; i<nRho; i++) {
rhoaxis[i] = rhoi*pow(10.0, (log10(rhof)-log10(rhoi))/(nRho-1)*i);
}
fprintf(stderr, "SCVH EOS: Initializing material %i\n", iMat);
Mat = scvheosInitMaterial(iMat, dKpcUnit, dMsolUnit);
/* Solve ODE. */
isentrope = SolveIsentrope(Mat, rhoaxis, Ti, nRho);
PrintIsentrope(isentrope);
scvheosFinalizeMaterial(Mat);
return 0;
}