51 {
52
53 FILE *in;
54
55 static char *line = NULL;
56
58 nu0, nu1, nuf[
MAXNF], press, temp, u;
59
60 static int i, idx, nf, npts;
61
62 size_t line_buf_size = 0;
63
64
66
67
68 if (argc != 5)
69 ERRMSG(
"Missing or invalid command-line arguments.\n\n"
70 "Usage: tblgen <press> <temp> <spec> <filter>\n\n"
71 "Use -h for full help.");
72 sscanf(argv[1], "%lg", &press);
73 sscanf(argv[2], "%lg", &temp);
74
75
76 u = 1e-6 * press * 100 / (
KB * temp) * 1000 / 1e4;
77
78
79 if (!(in = fopen(argv[4], "r")))
80 ERRMSG(
"Cannot open filter file!");
81 while (getline(&line, &line_buf_size, in) != -1)
82 if (sscanf(line, "%lg %lg", &nuf[nf], &filt[nf]) == 2)
84 ERRMSG(
"Too many points in filter function");
85 fclose(in);
86 if (nf < 2)
87 ERRMSG(
"Filter function needs at least two points!");
88
89
90 if (!(in = fopen(argv[3], "r")))
91 ERRMSG(
"Cannot open spectrum!");
92 for (i = 0; i < 4; ++i)
93 if (getline(&line, &line_buf_size, in) == -1)
94 ERRMSG(
"Error while reading spectrum!");
95 sscanf(line, "%d %lg %lg %lg", &npts, &nu0, &dnu, &nu1);
97 ERRMSG(
"Too many points in optical depth spectrum!");
98 i = 0;
99 while (getline(&line, &line_buf_size, in) != -1) {
100 char *tok = strtok(line, " \t\n");
101 while (tok != NULL) {
103 ERRMSG(
"Too many data points in spectrum file!");
104 sscanf(tok, "%lg", &abs[i]);
105 abs[i] /= u;
106 ++i;
107 tok = strtok(NULL, " \t\n");
108 }
109 }
110 fclose(in);
111
112
113 dnu = (nu1 - nu0) / ((double) npts - 1.0);
114 const double r0 = (nuf[0] - nu0) / (nu1 - nu0) * (double) npts;
115 const int i0 = (int) r0;
116
117
118 for (u = 1.0; u <= 1e30; u *= 1.122) {
119
120
121 double epssum = 0, fsum = 0;
122 for (i = i0; i < npts; i++) {
123 const double nu = nu0 + dnu * (double) i;
124 if (nu < nuf[0])
125 continue;
126 else if (nu > nuf[nf - 1])
127 break;
128 else {
129 if (nu < nuf[idx] || nu > nuf[idx + 1])
131 f =
LIN(nuf[idx], filt[idx], nuf[idx + 1], filt[idx + 1], nu);
132 fsum += f;
133 epssum += f * exp(-abs[i] * u);
134 }
135 }
136 epssum = 1 - epssum / fsum;
137
138
139 if (epssum >= 1e-6 && epssum <= 0.999999 && epssum > epsold)
140 printf("%g %g %g %g\n", press, temp, u, epssum);
141 epsold = epssum;
142
143
144 if (epssum > 0.999999)
145 return EXIT_SUCCESS;
146 }
147
148 return EXIT_SUCCESS;
149}
int locate_irr(const double *xx, const int n, const double x)
Locate index for interpolation on an irregular grid.
#define KB
Boltzmann constant [kg m^2/(K s^2)].
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
#define USAGE
Print usage information on -h or --help.
#define LIN(x0, y0, x1, y1, x)
Compute linear interpolation.