-
Notifications
You must be signed in to change notification settings - Fork 0
/
writeparams_job.cpp
287 lines (261 loc) · 10.7 KB
/
writeparams_job.cpp
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
* @file writeparams_job.h
* @brief Contains functions for writing and manipulating star mode parameters.
*/
#include <Eigen/Dense>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include "writeparams_job.h"
using Eigen::VectorXd;
using Eigen::VectorXi;
using Eigen::MatrixXd;
/**
* @brief Writes star mode parameters to a file in a specific format.
*
* This function takes a matrix of mode parameters and writes them to a file. The mode parameters are written in a specific format, including headers and labels for each parameter.
*
* @param mode_params The matrix of mode parameters to be written.
* @param file_out The output file path.
* @param append A flag indicating whether to append to an existing file or create a new file.
*/
void write_star_mode_params_asympt_model(MatrixXd mode_params, std::string file_out, bool append){
VectorXi Nchars(17), precision(17);
std::ofstream outfile;
Nchars << 5, 20, 20, 20, 16, 16, 16 , 16, 16, 16, 16, 16, 16, 16, 16, 16, 10;
precision << 1, 10, 10, 10, 10, 10, 10, 10 , 10, 10, 8, 8, 8, 8, 8, 2, 3;
if(append == false){
outfile.open(file_out.c_str());
} else{
outfile.open(file_out.c_str(), std::ios::app);
}
if(outfile.is_open()){
outfile << "# Configuration of mode parameters. This file was generated by write_star_mode_params_aj (write_star_params.cpp)" << std::endl;
outfile << "# Input mode parameters. degree / freq / H / W / a1 / a2 / a3 / a4 / a5 / a6 / asymetry / inclination" << std::endl;
for(int i=0; i<mode_params.rows(); i++){
for(int j=0;j<mode_params.cols(); j++){
outfile << std::setw(Nchars[j]) << std::setprecision(precision[j]) << mode_params(i,j);
}
outfile << std::endl;
}
outfile.close();
}
else {
file_read_error(file_out);
}
}
/**
* @brief Converts synthetic star parameters to a matrix of mode parameters with a specific inclination.
*
* This function takes synthetic star parameters and converts them into a matrix of mode parameters. The mode parameters are organized based on their degree and include additional parameters such as asymmetry and inclination.
*
* @param params The synthetic star parameters.
* @param inc The inclination angle.
* @return The matrix of mode parameters.
*/
MatrixXd bumpoutputs_2_MatrixXd_with_aj(Params_synthetic_star params, double inc){
int i, cpt=0;
int Nt=params.nu_l0.size() + params.nu_m_l1.size() + params.nu_l2.size() + params.nu_l3.size();
MatrixXd mode_params(Nt, 12);
for (i=0;i<params.nu_l0.size();i++){
mode_params(cpt, 0)=0;
mode_params(cpt, 1)=params.nu_l0[i];
mode_params(cpt, 2)=params.height_l0[i];
mode_params(cpt, 3)=params.width_l0[i];
mode_params(cpt, 4)=0; // a1 is 0 for l=0
mode_params(cpt, 5)=0; // a2
mode_params(cpt, 6)=0; // a3
mode_params(cpt, 7)=0; // a4
mode_params(cpt, 8)=0; // a5
mode_params(cpt, 9)=0; // a6
mode_params(cpt, 10)=0; // asym
mode_params(cpt, 11)=inc; // inc
cpt=cpt+1;
}
for (i=0;i<params.nu_m_l1.size();i++){
mode_params(cpt, 0)=1;
mode_params(cpt, 1)=params.nu_m_l1[i];
mode_params(cpt, 2)=params.height_l1[i];
mode_params(cpt, 3)=params.width_l1[i];
mode_params(cpt, 4)=params.a1_l1[i]; // a1
mode_params(cpt, 5)=params.a2_l1[i]; // a2
mode_params(cpt, 6)=0; // a3 not available for l=1
mode_params(cpt, 7)=0;// a4 not available for l=1
mode_params(cpt, 8)=0; // a5 not available for l=1
mode_params(cpt, 9)=0; // a6 not available for l=1
mode_params(cpt, 10)=0; // asym is 0 in simulations
mode_params(cpt, 11)=inc; // inc
cpt=cpt+1;
}
for (i=0;i<params.nu_l2.size();i++){
mode_params(cpt, 0)=2;
mode_params(cpt, 1)=params.nu_l2[i];
mode_params(cpt, 2)=params.height_l2[i];
mode_params(cpt, 3)=params.width_l2[i];
mode_params(cpt, 4)=params.a1_l2[i]; // a1
mode_params(cpt, 5)=params.a2_l2[i]; // a2
mode_params(cpt, 6)=params.a3_l2[i]; // a3
mode_params(cpt, 7)=params.a4_l2[i]; // a4
mode_params(cpt, 8)=0; // a5 not available for l=2
mode_params(cpt, 9)=0; // a6 not available for l=2
mode_params(cpt, 10)=0; // asym is 0 in simulations
mode_params(cpt, 11)=inc; // inc
cpt=cpt+1;
}
for (i=0;i<params.nu_l3.size();i++){
mode_params(cpt, 0)=3;
mode_params(cpt, 1)=params.nu_l3[i];
mode_params(cpt, 2)=params.height_l3[i];
mode_params(cpt, 3)=params.width_l3[i];
mode_params(cpt, 4)=params.a1_l3[i]; // a1 is 0 for l=0
mode_params(cpt, 5)=params.a2_l3[i]; // a2
mode_params(cpt, 6)=params.a3_l3[i]; // a3
mode_params(cpt, 7)=params.a4_l3[i]; // a4
mode_params(cpt, 8)=params.a5_l3[i]; // a5
mode_params(cpt, 9)=params.a6_l3[i]; // a6
mode_params(cpt, 10)=0; // asym is 0 in simulations
mode_params(cpt, 11)=inc; // inc
cpt=cpt+1;
}
return mode_params;
}
/**
* @brief Writes range modes to a file.
*
* This function takes the configuration and parameters of a synthetic star and writes the range modes to a file. The range modes include the numax, minimum and maximum frequencies, and the position of the curvature for the 2nd order equation of frequencies.
*
* @param cfg_star The configuration of the synthetic star.
* @param params The parameters of the synthetic star.
* @param output_file The output file path.
* @param append A flag indicating whether to append to an existing file or create a new file.
*/
void write_range_modes(Cfg_synthetic_star cfg_star, Params_synthetic_star params, std::string output_file, bool append){
const int Nchars=20;
const int precision=6;
std::ofstream outfile;
if(append == false){
outfile.open(output_file.c_str());
} else{
outfile.open(output_file.c_str(), std::ios::app);
}
if (outfile.is_open()){
outfile << "# numax and min and max frequencies for the relevant modes. The third parameter is nmax_star, the position of the curvature for the 2nd order equation of Freqs. This file was generated by io_star_params.cpp" << std::endl;
outfile << std::setw(Nchars) << std::setprecision(precision) << cfg_star.numax_star;
outfile << std::setw(Nchars) << std::setprecision(precision) << cfg_star.fmin;
outfile << std::setw(Nchars) << std::setprecision(precision) << cfg_star.fmax;
outfile << std::setw(Nchars) << std::setprecision(precision) << cfg_star.nmax_star << std::endl;
outfile.close();
}
else {
file_read_error(output_file);
}
}
/**
* @brief Displays an error message when a file cannot be read.
*
* This function is called when there is an error opening a file for reading. It displays an error message and exits the program.
*
* @param file_out The file that could not be read.
*/
void file_read_error(std::string file_out){
std::cout << " Unable to open file " << file_out << std::endl;
std::cout << " Check that the full path exists" << std::endl;
std::cout << " The program will exit now" << std::endl;
exit(EXIT_FAILURE);
}
/**
* @brief Writes star noise parameters to a file.
*
* This function takes a matrix of noise parameters and writes them to a file. The noise parameters are organized based on their degree and include additional information such as H0, tau_0, p0, H1, tau_1, p1, and N0.
*
* @param noise_params The matrix of noise parameters.
* @param file_out The output file path.
* @param append A flag indicating whether to append to an existing file or create a new file.
*/
void write_star_noise_params(MatrixXd noise_params, std::string file_out, bool append){
VectorXi Nchars(3), precision(3);
std::ofstream outfile;
Nchars << 16, 16, 16;
precision << 6, 6, 6;
if (append == false){
outfile.open(file_out.c_str());
} else{
outfile.open(file_out.c_str(), std::ios::app);
}
if(outfile.is_open()){
outfile << "# Configuration of mode parameters. This file was generated by write_star_mode_params (write_star_params.cpp)" << std::endl;
outfile << "# Input mode parameters. H0 , tau_0 , p0 / H1, tau_1, p1 / N0. Set at -1 if not used. -2 means that the parameter is not even written on the file (because irrelevant)." << std::endl;
for(int i=0; i<noise_params.rows(); i++){
for(int j=0;j<noise_params.cols(); j++){
outfile << std::setw(Nchars[j]) << std::setprecision(precision[j]) << noise_params(i,j);
}
outfile << std::endl;
}
outfile.close();
}
else {
file_read_error(file_out);
}
}
/**
* @brief Writes l=1 p and g modes to a file.
*
* This function takes the parameters of a synthetic star and writes the l=1 p and g modes to a file. The p and g modes are used for the mixed mode computation.
*
* @param params_out The parameters of the synthetic star.
* @param file_out The output file path.
* @param append A flag indicating whether to append to an existing file or create a new file.
*/
void write_star_l1_roots(Params_synthetic_star params_out, std::string file_out, bool append){
std::ofstream outfile;
int Nchars = 16;
int precision = 8;
if (append == false){
outfile.open(file_out.c_str());
} else{
outfile.open(file_out.c_str(), std::ios::app);
}
if(outfile.is_open()){
outfile << "# l=1 p and g modes used for the mixed mode computation. This file was generated" << std::endl;
// ---- Show outputs ----
for (int en=0; en<params_out.nu_p_l1.size(); en++ )
{
outfile << "p 1" << std::setw(Nchars) << std::setprecision(precision) << params_out.nu_p_l1[en] << std::endl;
}
for (int en=0; en<params_out.nu_g_l1.size(); en++ )
{
outfile << "g 1" << std::setw(Nchars) << std::setprecision(precision) << params_out.nu_g_l1[en] << std::endl;
}
}
}
/**
* @brief Copies the contents of one file to another.
*
* This function copies the contents of the input file to the output file. It removes trailing white space and tabulation from each line and excludes lines starting with "#". The copied contents are written to the output file.
*
* @param filename The path of the input file.
* @param outputFilename The path of the output file.
* @param append A flag indicating whether to append to an existing file or create a new file.
*/
void copy_cfg(const std::string& filename, const std::string& outputFilename, bool append) {
std::ifstream inputFile(filename);
std::ofstream outputFile(outputFilename, append ? std::ios::app : std::ios::trunc);
if (inputFile.is_open() && outputFile.is_open()) {
outputFile << "# Configuration used to generate the stellar pulsations" << std::endl;
std::string line;
while (std::getline(inputFile, line)) {
// Remove trailing white space and tabulation
//line.erase(line.find_last_not_of(" \t") + 1);
line =strtrim(line);
// Check if line starts with "#"
if (!line.empty() && line[0] != '#') {
outputFile << line << std::endl;
}
}
inputFile.close();
outputFile.close();
} else {
std::cout << "Failed to open input or output file." << std::endl;
}
}