1 #define BUILDING_LIBRARY 6 std::vector<double> EccentricityExpansionCoefficients::load_coefficient(
12 bool error_flag =
false;
13 int ms_index = local_index(m, s);
15 std::vector<double> pms;
17 sqlite3_stmt *statement;
18 std::string stmnt =
"SELECT y_value, step_number";
19 stmnt.append(
" FROM interpolation_data WHERE p_id = ");
20 stmnt.append(std::to_string(
__db_pms_id[ms_index]));
21 stmnt.append(
" ORDER BY step_number DESC");
22 const char *sql = stmnt.c_str();
23 if(sqlite3_prepare_v2(db, sql, -1, &statement, NULL) == SQLITE_OK) {
24 int rc = sqlite3_step(statement);
25 int i_b = sqlite3_column_int(statement, 1);
28 while(rc == SQLITE_ROW) {
29 pms[i_b] = sqlite3_column_double(statement, 0);
31 rc = sqlite3_step(statement);
34 if (rc != SQLITE_DONE)
39 sqlite3_finalize(statement);
43 "Unable to retrieve expansion id " 47 " in eccentricity expansion file during load_coefficient!" 56 sqlite3_stmt *statement;
57 std::string stmnt=
"SELECT MAX(s) FROM interpolations";
58 const char *sql = stmnt.c_str();
60 bool error_flag =
false;
62 int codeOne=sqlite3_prepare_v2(db,sql,-1,&statement,NULL);
63 if(codeOne==SQLITE_OK) {
64 int rc = sqlite3_step(statement);
65 while(rc==SQLITE_ROW) {
66 result_s = sqlite3_column_double(statement,0);
67 rc=sqlite3_step(statement);
69 if (rc!=SQLITE_DONE) error_flag=
true;
72 sqlite3_finalize(statement);
76 std::string msg =
"Eccentricity expansion file could not be read in";
77 msg.append(
" EccentricityExpansionCoefficients::set_max_s()!");
86 sqlite3_stmt *statement;
87 std::string stmnt =
"SELECT id, m, s, min_interp_e, number_of_steps,";
88 stmnt.append(
"max_checked_e, interp_accuracy FROM interpolations");
89 const char *sql = stmnt.c_str();
98 if(sqlite3_prepare_v2(db, sql, -1, &statement, NULL) == SQLITE_OK)
100 int rc = sqlite3_step(statement);
101 while(rc == SQLITE_ROW)
103 int m = sqlite3_column_int(statement, 1);
104 int s = sqlite3_column_int(statement, 2);
105 int ms_index = local_index(m, s);
106 __db_pms_id[ms_index] = sqlite3_column_int(statement, 0);
107 __min_e[ms_index] = sqlite3_column_double(statement, 3);
108 __num_steps[ms_index] = sqlite3_column_int(statement, 4);
109 __max_e[ms_index] = sqlite3_column_double(statement, 5);
114 rc = sqlite3_step(statement);
116 if (rc != SQLITE_DONE)
120 sqlite3_finalize(statement);
124 "Eccentricity expansion file could not be read in " 125 "EccentricityExpansionCoefficients::load_metadata()!" 136 for(
int m=2; m >= -2; m -= 2)
138 for(
int s=0; s <=
__max_s; ++s)
140 int destination_i = local_index(m, s);
144 sqlite3_stmt *statement;
145 std::ostringstream stmnt;
146 stmnt.setf(std::ios_base::scientific);
149 "SELECT MIN(b.step_number) FROM interpolations a " 150 "LEFT JOIN interpolation_data b ON a.id = b.p_id " 154 <<
" AND a.s = " << s
155 <<
" AND ABS(b.y_value) >= " << precision/
double(s);
159 sqlite3_prepare_v2(db,
167 if(sqlite3_step(statement) != SQLITE_ROW)
170 int max_ignore_step = (
171 sqlite3_column_int(statement, 0)
177 ? step_to_e(m, s, max_ignore_step)
180 if(sqlite3_step(statement) != SQLITE_DONE)
185 sqlite3_finalize(statement);
189 "Eccentricity expansion file could not be read in " 190 "EccentricityExpansionCoefficients::" 191 "load_max_ignore_eccentricity()!" 198 double EccentricityExpansionCoefficients::load_specific_e(
206 bool error_flag =
false;
208 int ms_index = local_index(m, s);
215 "Unable to open eccentricity expansion file: " 223 sqlite3_stmt *statement;
224 std::string stmnt=
"SELECT y_value FROM interpolation_data";
225 stmnt.append(
" WHERE p_id = ");
226 stmnt.append(std::to_string(
__db_pms_id[ms_index]));
227 stmnt.append(
" AND step_number=");
228 stmnt.append(std::to_string(e_step));
229 const char *sql = stmnt.c_str();
230 int theCode=sqlite3_prepare_v2(db,sql,-1,&statement,NULL);
231 if(theCode==SQLITE_OK)
233 int rc = sqlite3_step(statement);
234 while(rc==SQLITE_ROW)
236 result=( sqlite3_column_double(statement,0) );
237 rc=sqlite3_step(statement);
243 std::cout<<
"Loop finished without being done.\n";
249 std::cout<<
"prepare was not ok. error code " << std::to_string(theCode) <<
"\n";
250 std::cout<<sqlite3_errmsg(db);
252 sqlite3_finalize(statement);
256 "Unable to retrieve expansion id " 260 " in eccentricity expansion file during load_specific_e!" 273 for(
int s=0; s <=
__max_s; s++)
274 for(
int m=-2; m<=2; m+=2)
289 : load_specific_e(m, s, e_step)
293 std::vector<double> EccentricityExpansionCoefficients::find_pms_boundary_values(
299 std::vector<double> results (4);
300 int lo_i = e_to_nearest_step(m,s,e,
true);
303 results[0] = step_to_e(m, s, lo_i);
304 results[1] = step_to_e(m, s, hi_i);
310 double EccentricityExpansionCoefficients::return_known_e(
317 ( (s==0 || e==1.0) && m==0 )
319 ( m==2 && s==2 && e==0.0 )
324 ( s==0 && std::abs(m)==2 )
332 if( e <
__min_e[local_index(m,s)] )
338 bool EccentricityExpansionCoefficients::check_known_e(
357 inline int EccentricityExpansionCoefficients::e_to_nearest_step(
364 int li = local_index(m,s);
366 square_step = ( e-double(
__min_e[li]) )
371 if(flr)
return int( floor(square_step) );
372 else return int( ceil(square_step) );
375 inline double EccentricityExpansionCoefficients::step_to_e(
381 int li = local_index(m,s);
382 double result = ( double(step) / (double(
__num_steps[li]) - 1) )
390 inline int EccentricityExpansionCoefficients::local_index(
int m,
393 return s * 3 + (m + 2) / 2;
397 const std::string &tabulated_pms_fname,
400 bool disable_precision_fail
410 rc = sqlite3_open(tabulated_pms_fname.c_str(), &db);
414 "Unable to open eccentricity expansion file: " 445 "Asking for EccentricityExpansionCoefficients::" 446 "interp_precision() before reading interpolation data" 448 if(m != 0l && std::abs(m) != 2)
450 "Asking EccentricityExpansionCoefficients::interp_precision() " 451 "for p_{m,s} with m other than +-2 and 0" 463 "Asking EccentricityExpansionCoefficients::" 464 "required_expansion_order() before reading interpolation data" 470 std::ostringstream message;
471 message <<
"Tabulated eccentricity interpolation is unsufficient " 472 <<
"to achieve the specified precision in tidal potential " 473 <<
"expansion for e = " 482 std::pair<double,double>
490 "Asking EccentricityExpansionCoefficients::" 491 "get_expansion_range() before reading interpolation data" 494 std::cerr <<
"Max ignore e(m=" 509 return std::make_pair(
526 "Attempting to evaluate p_ms before reading interpolation data" 529 if(m != 0 && std::abs(m) != 2)
531 "Asking for p_{m,s} with m other than +-2 and 0" 535 "Attempting to evaluate larger s than is available!" 537 if(e >
__max_e[local_index(m, s)])
539 "Attempting to evaluate larger e than is accounted for!" 542 if(check_known_e(m, s, e)) {
544 return (deriv ? Core::NaN : return_known_e(m, s, e));
547 std::vector<double> e_and_y_values(4);
548 e_and_y_values = find_pms_boundary_values(m, s, e);
551 (e_and_y_values[3] - e_and_y_values[2])
553 (e_and_y_values[1] - e_and_y_values[0])
558 return slope * (e - e_and_y_values[0]) + e_and_y_values[2];
std::vector< double > __max_e
The maximum eccentricity at which each coefficient can be reliably interpolated. ...
double operator()(int m, int s, double e, bool deriv) const
Approximate the value of .
Function arguments do not satisfy some requirement.
std::vector< double > __max_ignore_eccentricity
The maximum eccentricity at which a given can be ignored.
std::vector< double > __min_e
Declares a class which provides the [ coefficients]{InclinationEccentricity_pms1}.
std::vector< int > __num_steps
The number of e values at which each coefficient is tabulated.
double __expansion_precision
The currently defined expansion precision (see prepare())
Orientations of zones of bodies in a binary system.
void load_metadata(sqlite3 *db)
Read the metadata for the available coefficients from the databate.
std::vector< std::vector< double > > __pms_interp_data
The expansion coefficients for all .
std::vector< int > __db_pms_id
The identifiers of particular coefficients in the database.
void load_max_ignore_eccentricity(sqlite3 *db, double precision)
Fill the __max_ignore_eccentricity member per the database.
std::string __file_name
The name of the file contanining the interpolatiod sqlite database.
bool __allow_precision_fail
See do_not_fail argument to prepare().
double get_specific_e(int m, int s, int e_step) const
Return a single tabulated value of .
void prepare(const std::string &tabulated_pms_fname, double precision, bool pre_load, bool disable_precision_fail=false)
Reads in tabulated expansion coefficients, making this object useable.
double interp_precision(int m, int s) const
The guaranteed interpolation precision for a given .
void get_interp_data(sqlite3 *db)
The callback SQL function that updates the above values (__pms_interp_data)
std::vector< double > __interp_precision
The guaranteed interpolation precision for each u coefficient.
void set_max_s(sqlite3 *db)
TODO: use smallest max s over m=+-2 and m=0.
bool __load_all
Whether we load the whole database at the start (true) or part of it as needed (false) ...
int required_expansion_order(double e, int m) const
Return the smallest s value such that all for.
int __max_s
The largest s (second) index at which all three coefficients are tabulated.
std::pair< double, double > get_expansion_range(int m, int max_s) const
Return the range of eccentricities (min, max) over which an expansion going up to given max s is vali...