-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc_ravit_model_codeunits.c
172 lines (142 loc) · 4.59 KB
/
calc_ravit_model_codeunits.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
/*
* This program calculates the pressure and entropy for Ravit's models in different units using the
* SCvH EOS for H-He.
*
* 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;
// Units
const double MSOLG = 1.99e33;
const double KPCCM = 3.085678e21;
double dLUnit;
double dMUnit;
// model
struct Model *model;
#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 != 2) {
fprintf(stderr,"Usage: calc_ravit_model_codeunits <model.dat>\n");
exit(1);
}
/* Read equilibrium model in cgs. */
model = ReadModel(argv[1]);
Mat = scvheosInitMaterial(iMat, dKpcUnit, dMsolUnit);
dLUnit = Mat->dKpcUnit*KPCCM;
dMUnit = Mat->dMsolUnit*MSOLG;
/* Calculate entropy. */
for (int i=0; i<model->nTable; i++) {
model->s[i] = scvheosSofRhoT(Mat, model->rho[i]/Mat->dGmPerCcUnit, model->T[i]);
/* Convert to cgs. */
model->s[i] *= Mat->dErgPerGmUnit;
}
#if 0
/* Print the model. */
fprintf(stdout, "#%13s%11s%11s%11s%11s\n", "R [cm]", "rho [g/cm^3]", "P [cgs]", "T [K]", "M [g]");
for (int i=0; i<model->nTable; i++) {
fprintf(stdout, "%13.3E%11.3E%11.3E%11.3E%11.3E\n", model->R[i], model->rho[i],
model->P[i], model->T[i], model->M[i]);
}
#endif
/* Print the model. */
fprintf(stdout, "#%14s%15s", "R [cm]", "R");
fprintf(stdout, "%15s%15s", "rho [cgs]", "rho");
fprintf(stdout, "%15s%15s", "P [cgs]", "P");
fprintf(stdout, "%15s", "T [K]");
fprintf(stdout, "%15s%15s", "M [g]", "M");
fprintf(stdout, "%15s%15s", "s [cgs]", "s");
fprintf(stdout, "\n");
for (int i=0; i<model->nTable; i++) {
fprintf(stdout, "%15.7E%15.7E", model->R[i], model->R[i]/dLUnit);
fprintf(stdout, "%15.7E%15.7E", model->rho[i], model->rho[i]/Mat->dGmPerCcUnit);
fprintf(stdout, "%15.7E%15.7E", model->P[i], model->P[i]/(Mat->dErgPerGmUnit*Mat->dGmPerCcUnit));
fprintf(stdout, "%15.7E", model->T[i]);
fprintf(stdout, "%15.7E%15.7E", model->M[i], model->M[i]/dMUnit);
fprintf(stdout, "%15.7E%15.7E", model->s[i], model->s[i]/Mat->dErgPerGmUnit);
fprintf(stdout, "\n");
}
/* Free memory. */
scvheosFinalizeMaterial(Mat);
free(model);
return 0;
}