-
Notifications
You must be signed in to change notification settings - Fork 0
/
testscvheosinv.c
151 lines (118 loc) · 4.53 KB
/
testscvheosinv.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
/*
* A simple program to test the SCVH EOS library.
*
* Test the inversion routines T(rho, u), T(rho, s) and rho(P, T).
*
* Author: Christian Reinhardt
* Created: 16.08.2022
* Modified: 18.08.2022
*/
#include <math.h>
#include <stdio.h>
#include <assert.h>
#include "scvheos.h"
int main(int argc, char **argv) {
// SCvH material
SCVHEOSMAT *Mat;
int iMat = SCVHEOS_HHE_LOWRHOT;
//int iMat = SCVHEOS_HHE;
double dKpcUnit = 0.0;
double dMsolUnit = 0.0;
double *logrhoAxis;
double *logTAxis;
int nRho;
int nT;
FILE *fp;
fprintf(stderr, "SCVHEOS: Initializing material %i\n", iMat);
Mat = scvheosInitMaterial(iMat, dKpcUnit, dMsolUnit);
fprintf(stderr, "Doing %s interpolation for logP(logrho, logT)\n", gsl_interp2d_name(Mat->InterpLogP));
fprintf(stderr, "\n");
nRho = Mat->nRho*10;
nT = Mat->nT*10;
/* Generate rho and T axis. */
logrhoAxis = (double *) calloc(nRho, sizeof(double));
logTAxis = (double *) calloc(nT, sizeof(double));
for (int i=0; i<nRho; i++) {
logrhoAxis[i] = Mat->dLogRhoAxis[0] + i*(Mat->dLogRhoAxis[Mat->nRho-1]-Mat->dLogRhoAxis[0])/(nRho-1);
}
for (int i=0; i<nT; i++) {
logTAxis[i] = Mat->dLogTAxis[0] + i*(Mat->dLogTAxis[Mat->nT-1]-Mat->dLogTAxis[0])/(nT-1);
}
#if 0
nRho = (Mat->nRho-1)*2+1;
nT = (Mat->nT-1)*2+1;
/* Generate rho and T axis. */
logrhoAxis = (double *) calloc(nRho, sizeof(double));
logTAxis = (double *) calloc(nT, sizeof(double));
for (int i=0; i<Mat->nRho-1; i++) {
for (int j=0; j<2; j++) {
logrhoAxis[i*2+j] = Mat->dLogRhoAxis[i] + j*(Mat->dLogRhoAxis[i+1]-Mat->dLogRhoAxis[i])/2;
}
}
logrhoAxis[nRho-1] = Mat->dLogRhoAxis[Mat->nRho-1];
for (int i=0; i<Mat->nT-1; i++) {
for (int j=0; j<2; j++) {
logTAxis[i*2+j] = Mat->dLogTAxis[i] + j*(Mat->dLogTAxis[i+1]-Mat->dLogTAxis[i])/2;
}
}
logTAxis[nT-1] = Mat->dLogTAxis[Mat->nT-1];
#endif
/* Write both axis to a file. */
fp = fopen("testscvheosinv_rhoaxis.txt", "w");
for (int i=0; i<nRho; i++) {
fprintf(fp, "%15.7E\n", logrhoAxis[i]);
}
fclose(fp);
fp = fopen("testscvheosinv_taxis.txt", "w");
for (int i=0; i<nT; i++) {
fprintf(fp, "%15.7E\n", logTAxis[i]);
}
fclose(fp);
/* Calculate the relative error on logT(logrho, logu). */
fprintf(stderr, "Calculate logT(logrho, logu) on a logrho x logT grid.\n");
fp = fopen("testscvheosinv_logtoflogrhologu.txt", "w");
fprintf(fp, "# logT(logrho, logu) (nRho = %i nT= %i)\n", nRho, nT);
fprintf(fp, "# Interpolator: %s (GSL)\n", gsl_interp2d_name(Mat->InterpLogU));
for (int i=0; i<nT; i++) {
for (int j=0; j<nRho; j++) {
double logu = scvheosLogUofLogRhoLogT(Mat, logrhoAxis[j], logTAxis[i]);
double logT = scvheosLogTofLogRhoLogU(Mat, logrhoAxis[j], logu);
fprintf(fp, "%15.7E", fabs((logT-logTAxis[i])/logT));
}
fprintf(fp, "\n");
}
fclose(fp);
/* Calculate the relative error on logT(logrho, logs). */
fprintf(stderr, "Calculate logT(logrho, logs) on a logrho x logT grid.\n");
fp = fopen("testscvheosinv_logtoflogrhologs.txt", "w");
fprintf(fp, "# logT(logrho, logs) (nRho = %i nT= %i)\n", nRho, nT);
fprintf(fp, "# Interpolator: %s (GSL)\n", gsl_interp2d_name(Mat->InterpLogS));
for (int i=0; i<nT; i++) {
for (int j=0; j<nRho; j++) {
double logs = scvheosLogSofLogRhoLogT(Mat, logrhoAxis[j], logTAxis[i]);
double logT = scvheosLogTofLogRhoLogS(Mat, logrhoAxis[j], logs);
fprintf(fp, "%15.7E", fabs((logT-logTAxis[i])/logT));
}
fprintf(fp, "\n");
}
fclose(fp);
/* Calculate the relative error on logrho(logP, logT). */
fprintf(stderr, "Calculate logrho(logP, logT) on a logrho x logT grid.\n");
fp = fopen("testscvheosinv_logrhooflogplogt.txt", "w");
fprintf(fp, "# logrho(logP, logT) (nRho = %i nT= %i)\n", nRho, nT);
fprintf(fp, "# Interpolator: %s (GSL)\n", gsl_interp2d_name(Mat->InterpLogP));
for (int i=0; i<nT; i++) {
for (int j=0; j<nRho; j++) {
double logP = scvheosLogPofLogRhoLogT(Mat, logrhoAxis[j], logTAxis[i]);
double logrho = scvheosLogRhoofLogPLogT(Mat, logP, logTAxis[i]);
fprintf(fp, "%15.7E", fabs((logrho-logrhoAxis[j])/logrho));
}
fprintf(fp, "\n");
}
fclose(fp);
/* Free memory. */
scvheosFinalizeMaterial(Mat);
//free(logrhoAxis);
//free(logTAxis);
return 0;
}