-
Notifications
You must be signed in to change notification settings - Fork 17
/
pops_xdr.c
207 lines (160 loc) · 6.18 KB
/
pops_xdr.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* ------- file: -------------------------- writen_xdr.c ------------
Version: rh2.0
Author: Han Uitenbroek ([email protected])
Last modified: Thu Jan 26 14:50:56 2012 --
-------------------------- ----------RH-- */
/* --- Routines for reading and writing populations from and to file.
XDR (external data representation) version. -- -------------- */
#include <stdlib.h>
#include <string.h>
#include "rh.h"
#include "atom.h"
#include "atmos.h"
#include "error.h"
#include "xdr.h"
/* --- Function prototypes -- -------------- */
/* --- Global variables -- -------------- */
extern Atmosphere atmos;
extern char messageStr[];
/* ------- begin -------------------------- xdr_populations.c ------- */
bool_t xdr_populations(XDR *xdrs, char *atmosID, int Nlevel, long Nspace,
double *n, double *nstar)
{
const char routineName[] = "xdr_populations";
char *ID;
bool_t result = TRUE;
long Npop = Nlevel * Nspace, Nl, Ns;
/* --- The actual reading/writing routine. Upon input the values
for atmosID, Nlevel and Nspace in the file are checked against
the values supplied in the call. -- -------------- */
if (xdrs->x_op == XDR_ENCODE) {
result &= xdr_counted_string(xdrs, &atmosID);
result &= xdr_int(xdrs, &Nlevel);
result &= xdr_long(xdrs, &Nspace);
} else {
result &= xdr_counted_string(xdrs, &ID);
if (!strstr(ID, atmosID)) {
sprintf(messageStr, "Populations were computed with different"
"atmosphere (%s) than current one", ID);
Error(WARNING, routineName, messageStr);
}
free(ID);
result &= xdr_int(xdrs, &Nl);
result &= xdr_int(xdrs, &Ns);
if ((Nl != Nlevel) || (Ns != Nspace)) return FALSE;
}
/* --- Exit if true populations do not exist -- -------------- */
if (n != NULL) {
result &= xdr_vector(xdrs, (char *) n, Npop,
sizeof(double), (xdrproc_t) xdr_double);
} else
return FALSE;
/* --- We can live without the LTE values since they can always be
constructed with routine LTEpops -- -------------- */
if (nstar != NULL)
result &= xdr_vector(xdrs, (char *) nstar, Npop,
sizeof(double), (xdrproc_t) xdr_double);
return result;
}
/* ------- end ---------------------------- xdr_populations.c ------- */
/* ------- begin -------------------------- writePopulations.c ------ */
void writePopulations(Atom *atom)
{
const char routineName[] = "writePopulations";
FILE *fp_out;
XDR xdrs;
if ((fp_out = fopen(atom->popsoutFile, "w")) == NULL) {
sprintf(messageStr, "Unable to open output file %s",
atom->popsoutFile);
Error(ERROR_LEVEL_1, routineName, messageStr);
return;
}
xdrstdio_create(&xdrs, fp_out, XDR_ENCODE);
if (!xdr_populations(&xdrs, atmos.ID, atom->Nlevel, atmos.Nspace,
atom->n[0], atom->nstar[0])) {
sprintf(messageStr, "Unable to write to output file %s",
atom->popsoutFile);
Error(ERROR_LEVEL_1, routineName, messageStr);
}
xdr_destroy(&xdrs);
fclose(fp_out);
}
/* ------- end ---------------------------- writePopulations.c ------ */
/* ------- begin -------------------------- readPopulations.c ------- */
void readPopulations(Atom *atom)
{
const char routineName[] = "readPopulations";
FILE *fp_in;
XDR xdrs;
/* --- Read populations from file.
Note: readPopulations only reads the true populations and not
the LTE populations. To this effect it passes a NULL pointer
to xdr_populations as its last argument.
-- -------------- */
if ((fp_in = fopen(atom->popsinFile, "r")) == NULL) {
sprintf(messageStr, "Unable to open input file %s",
atom->popsinFile);
Error(ERROR_LEVEL_2, routineName, messageStr);
}
xdrstdio_create(&xdrs, fp_in, XDR_DECODE);
if (!xdr_populations(&xdrs, atmos.ID, atom->Nlevel, atmos.Nspace,
atom->n[0], atom->nstar[0])) {
sprintf(messageStr, "Unable to read from input file %s",
atom->popsinFile);
Error(ERROR_LEVEL_2, routineName, messageStr);
}
xdr_destroy(&xdrs);
fclose(fp_in);
}
/* ------- end ---------------------------- readPopulations.c ------- */
/* ------- begin -------------------------- writeMolPops.c ---------- */
void writeMolPops(struct Molecule *molecule)
{
const char routineName[] = "writeMolPops";
FILE *fp_out;
XDR xdrs;
/* --- Write molecular (vibration) populations to file. -- -------- */
if ((fp_out = fopen(molecule->popsFile, "w")) == NULL) {
sprintf(messageStr, "Unable to open output file %s",
molecule->popsFile);
Error(ERROR_LEVEL_1, routineName, messageStr);
return;
}
xdrstdio_create(&xdrs, fp_out, XDR_ENCODE);
if (!xdr_populations(&xdrs, atmos.ID, molecule->Nv, atmos.Nspace,
molecule->nv[0], molecule->nvstar[0])) {
sprintf(messageStr, "Unable to write to output file %s",
molecule->popsFile);
Error(ERROR_LEVEL_1, routineName, messageStr);
}
xdr_destroy(&xdrs);
fclose(fp_out);
}
/* ------- end ---------------------------- writeMolPops.c ---------- */
/* ------- begin -------------------------- readMolPops.c ----------- */
void readMolPops(struct Molecule *molecule)
{
const char routineName[] = "readMolPops";
FILE *fp_in;
XDR xdrs;
/* --- Read molecular (vibration) populations from file.
Note: readMolPops only reads the true populations and not
the LTE populations. To this effect it passes a NULL pointer
to xdr_populations as its last argument.
-- -------------- */
if ((fp_in = fopen(molecule->popsFile, "r")) == NULL) {
sprintf(messageStr, "Unable to open input file %s",
molecule->popsFile);
Error(ERROR_LEVEL_2, routineName, messageStr);
}
xdrstdio_create(&xdrs, fp_in, XDR_DECODE);
if (!xdr_populations(&xdrs, atmos.ID, molecule->Nv, atmos.Nspace,
molecule->nv[0], NULL)) {
sprintf(messageStr, "Unable to read from input file %s",
molecule->popsFile);
Error(ERROR_LEVEL_2, routineName, messageStr);
}
xdr_destroy(&xdrs);
fclose(fp_in);
}
/* ------- end ---------------------------- readPopulations.c ------- */