-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc_ravit_model_entropy.c
149 lines (120 loc) · 3.71 KB
/
calc_ravit_model_entropy.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
/*
* This program calculates the entropy for Ravit's models using the SCvH EOS for H-He.
*
* Author: Christian Reinhardt
* Created: 31.10.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;
#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_entropy <ballic.model>\n");
exit(1);
}
/* Read equilibrium model. */
model = ReadModel(argv[1]);
Mat = scvheosInitMaterial(iMat, dKpcUnit, dMsolUnit);
for (int i=0; i<model->nTable; i++) {
model->s[i] = scvheosSofRhoT(Mat, model->rho[i], model->T[i]);
}
#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, "#%12s%11s%11s%11s%11s%11s\n", "R [cm]", "rho [cgs]", "P [cgs]", "T [K]", "M [g]", "s [cgs]");
for (int i=0; i<model->nTable; i++) {
fprintf(stdout, "%13.3E%11.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], model->s[i]);
}
/* Free memory. */
scvheosFinalizeMaterial(Mat);
free(model);
return 0;
}