-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ravit_model_prhou.c
156 lines (126 loc) · 4.05 KB
/
test_ravit_model_prhou.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
/*
* This program calculates the pressure for Ravit's models from P(rho, u(rho, T)) in different
* units using the SCvH EOS for H-He and compares to the value of the model.
*
* Author: Christian Reinhardt
* Created: 09.11.2022
* Modified:
*
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "scvheos.h"
struct Model {
double *R;
double *rho;
double *P;
double *T;
double *M;
double *s;
int nTable;
};
struct Model *ReadModel(char *chFile) {
struct Model *model;
FILE *fp;
char *chLine;
size_t nCharMax = 256;
int iRet;
int nTableMax = 100000;
int i;
/* Allocate memory. */
model = (struct Model *) calloc(1, sizeof(struct Model));
assert(model != NULL);
model->R = (double *) calloc(nTableMax, sizeof(double));
assert(model->R != NULL);
model->rho = (double *) calloc(nTableMax, sizeof(double));
assert(model->rho != NULL);
model->P = (double *) calloc(nTableMax, sizeof(double));
assert(model->P != NULL);
model->T = (double *) calloc(nTableMax, sizeof(double));
assert(model->T != NULL);
model->M = (double *) calloc(nTableMax, sizeof(double));
assert(model->M != NULL);
model->s = (double *) calloc(nTableMax, sizeof(double));
assert(model->s != NULL);
model->nTable = 0;
chLine = (char *) calloc(nCharMax, sizeof(char));
/* Open the file. */
fp = fopen(chFile, "r");
assert(fp != NULL);
i = 0;
while (getline(&chLine, &nCharMax, fp) != -1) {
/* Check if its a comment. */
if (strchr(chLine, '#') != NULL) continue;
iRet = sscanf(chLine, "%lf %lf %lf %lf %lf", &model->R[i], &model->rho[i],
&model->P[i], &model->T[i], &model->M[i]);
/* Check if the number of matches is correct. */
assert(iRet == 5);
/* Check that the values are sensible. */
assert(model->R[i] >= 0.0);
assert(model->rho[i] >= 0.0);
assert(model->T[i] >= 0.0);
i++;
/* Initialize entropy to a weird value. */
model->s[i] = -1e50;
assert(i < nTableMax);
}
model->nTable = i;
fprintf(stderr, "ReadModel: read %i lines.\n", model->nTable);
return model;
}
int main(int argc, char **argv) {
// SCvH EOS library
SCVHEOSMAT *Mat;
double dKpcUnit;
double dMsolUnit;
int iMat = 113;
// model
struct Model *model;
double rel_err;
int nErr = 0;
#if 0
/* L_unit = 1 RE, v_unit = 1 km/s. */
dKpcUnit = 2.06701e-13;
dMsolUnit = 4.80438e-08;
/* L_unit = 1 AU, M_unit = 1 MJ. */
dKpcUnit = 4.84821E-09;
dMsolUnit = 9.53869E-04;
#endif
/* cgs */
dKpcUnit = 0;
dMsolUnit = 0;
if (argc != 3) {
fprintf(stderr,"Usage: test_ravit_model_prhou <model.dat> <rel_err>\n");
exit(1);
}
/* Read equilibrium model in cgs. */
model = ReadModel(argv[1]);
assert(model != NULL);
rel_err = atof(argv[2]);
assert(rel_err > 0.0);
Mat = scvheosInitMaterial(iMat, dKpcUnit, dMsolUnit);
fprintf(stdout, "#%14s%15s%15s%15s%15s\n", "R [cm]", "rho [cgs]", "P [cgs]", "T [K]", "err");
/* Calculate the error in pressure pressure. */
for (int i=0; i<model->nTable; i++) {
double u = scvheosUofRhoT(Mat, model->rho[i]/Mat->dGmPerCcUnit, model->T[i]);
double P = scvheosPofRhoU(Mat, model->rho[i]/Mat->dGmPerCcUnit, u);
/* Convert to cgs. */
P *= Mat->dErgPerGmUnit*Mat->dGmPerCcUnit;
double err = fabs((P-model->P[i])/P);
if (err > rel_err) {
fprintf(stderr, "Error: err= %15.7E (rho=%15.7E g/cm^3 T=%15.7E K P=%15.7E P_model=%15.7E)\n",
err, model->rho[i], model->T[i], P, model->P[i]);
nErr++;
//exit(1);
}
fprintf(stdout, "%15.7E%15.7E%15.7E%15.7E%15.7E\n", model->R[i], model->rho[i], model->P[i], model->T[i], err);
}
/* Free memory. */
scvheosFinalizeMaterial(Mat);
free(model);
fprintf(stderr, "nErr = %i (rel_err=%15.7E)\n", nErr, rel_err);
return 0;
}