-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc_model_entropy.c
142 lines (112 loc) · 3.62 KB
/
calc_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
/*
* Author: Christian Reinhardt
* Created: 31.10.2022
* Modified:
*
* This program calculates the entropy of a model generated with ballic.
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "scvheos.h"
struct BallicModel {
double *R;
double *rho;
double *u;
double *M;
int *iMat;
double *P;
double *T;
double *s;
int nTable;
};
struct BallicModel *ReadBallicModel(char *chFile) {
struct BallicModel *model;
FILE *fp;
char *chLine;
size_t nCharMax = 256;
int iRet;
int nTableMax = 100000;
int i;
/* Allocate memory. */
model = (struct BallicModel *) calloc(1, sizeof(struct BallicModel));
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->u = (double *) calloc(nTableMax, sizeof(double));
assert(model->u != NULL);
model->M = (double *) calloc(nTableMax, sizeof(double));
assert(model->M != NULL);
model->iMat = (int *) calloc(nTableMax, sizeof(int));
assert(model->iMat != NULL);
model->P = (double *) calloc(nTableMax, sizeof(double));
assert(model->P != NULL);
model->T = (double *) calloc(nTableMax, sizeof(double));
assert(model->T != 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 %d %lf %lf", &model->R[i], &model->rho[i],
&model->M[i], &model->u[i], &model->iMat[i], &model->P[i], &model->T[i]);
/* Check if the number of matches is correct. */
assert(iRet == 7);
/* Check that the values are sensible. */
assert(model->R[i] >= 0.0);
assert(model->rho[i] >= 0.0);
i++;
/* Initialize entropy to a weird value. */
model->s[i] = -1e50;
assert(i < nTableMax);
}
model->nTable = i;
fprintf(stderr, "ReadBallicModel: 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;
// ballic model
struct BallicModel *model;
/* 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;
if (argc != 3) {
fprintf(stderr,"Usage: calc_model_entropy <ballic.model> <iMat>\n");
exit(1);
}
/* Read equilibrium model. */
model = ReadBallicModel(argv[1]);
iMat = atoi(argv[2]);
Mat = scvheosInitMaterial(iMat, dKpcUnit, dMsolUnit);
for (int i=0; i<model->nTable; i++) {
model->s[i] = scvheosSofRhoT(Mat, model->rho[i], model->T[i]);
}
/* Print the model. */
fprintf(stdout, "#%14s%15s%15s%15s%4s%15s%15s%15s\n", "R", "rho", "M", "u", "mat", "P", "T", "s");
for (int i=0; i<model->nTable; i++) {
fprintf(stdout, "%15.7E%15.7E%15.7E%15.7E%4i%15.7E%15.7E%15.7E\n", model->R[i], model->rho[i],
model->M[i], model->u[i], model->iMat[i], model->P[i], model->T[i], model->s[i]);
}
/* Free memory. */
scvheosFinalizeMaterial(Mat);
free(model);
return 0;
}