SEVN
Loading...
Searching...
No Matches
utilities.h
Go to the documentation of this file.
1//
2// Created by mario on 04/05/18.
3//
4
5#ifndef SEVN_REVISED_UTILITIES_H
6#define SEVN_REVISED_UTILITIES_H
7
8#ifdef _OPENMP
9#include <omp.h>
10#endif
11
12#include <vector>
13#include <sstream>
14#include <iomanip>
15#include <regex>
16#include <random>
17#include <algorithm>
18#include <stdexcept>
19#include <memory>
20#include <limits>
21#include <map>
22
23
24#include <sevnlog.h>
26
27#define _UNUSED __attribute__ ((unused))
28#define openfile(a, b) utilities::_openfile(a, b, __FILE__, __LINE__)
29
30class Star;
31class Binstar;
32
33namespace utilities{
34
35 //random number generator (OpenMP thread-safe)
36 extern std::mt19937_64 mtrand;
37 #ifdef _OPENMP
38 #pragma omp threadprivate(utilities::mtrand)
39 #endif
40
41
42 //GI 140520 changed this because a firstprivate clause is added in the main files
43 //GI 130321 Changed again because a firstprivate clause could not be used with the current implementation
44 //this means that this version is not compilable with INTEL compiler (beacuse mtrand is defined as extern)
45 //extern std::uniform_real_distribution<double> rand_unif_0_1(0.0, 1.0);
46
47
48 //SEVN NAME
49 const std::string SEVN_NAME = "SEVN"; //NOTICE THIS HAS TO BE THE NAME OF THE FOLDER CONTAINING THE CODE
50
51 //TODO: Add cgs -> SEVN units constants
52
53 //constants
54 //TODO The timestep and other time property are in Myr, but some costant and the eq. of some process are in yr, should we use only a timescale?
55
57 //TODO Check all the constants to be consistent with these values
58 //GI, 27/07/2022, all the constant updated to match the definition in astropy-4.3.1
59 constexpr double G = 3.925125598496094e8; //RSUN^3 YR^-2 MSUN^-1 (astropy: constant.G.to("Rsun^3/yr^2/Msun"))
60 constexpr double yr_cgs = 3.1557600e7; //yr in s (astropy: u.yr.to(u.s))
61 constexpr double parsec_cgs= 3.085677581491367e+18; //parsec in cm (astropy: u.parsec.to(u.cm))
62 constexpr double Rsun_cgs = 6.95700e10; //rsun in cm (astropy constant.R_sun.to("cm"))
63 constexpr double Msun_cgs = 1.988409870698051e+33; //msun in g (astropy constant.M_sun.to("cm"))
64 constexpr double G_cgs = G*Rsun_cgs*Rsun_cgs*Rsun_cgs/(Msun_cgs*yr_cgs*yr_cgs); //cm^3 s^-2 g^-1
65
66
67 constexpr double Sigma_StefBoltz = 7.1694165533435e-17; //LSun^3 RSun^-2 K^-4 (astropy: constant.sigma_sb.to('Lsun/(K^4 * Rsun^2)')
68 constexpr double Myr_to_yr = 1.0e6;
69 constexpr double yr_to_Myr = 1.0e-6;
70 constexpr double AU_to_RSun = 215.03215567054764; //(astropy u.au.to(u.Rsun))
71 constexpr double kms_to_RSunyr = 45.360931435963785; //(astropy: (u.km/u.s).to(u.Rsun/u.yr))
72 constexpr double LSun_to_Solar = 12.500687924182579; //From Lsun to MSun RSun^2 yr^-3 (astropy: u.Lsun.to((u.Msun*u.Rsun**2)/(u.yr**3)))
73 constexpr double c = 1.3598865132357053e7; // RSun/yr (astropy: constant.c.to('Rsun/yr'))
74 constexpr double km_to_RSun = 1.4374011786689665e-06; //(astropy: u.km.to(u.Rsun))
76 constexpr double g_to_MSun = 5.029144215870041e-34; //(astropy: u.g.to(u.Msun))
77 constexpr double G_over_c2 = G / (c*c);
78 constexpr double G3_over_c5 = (G*G*G)/(c*c*c*c*c); //Scaling for GW processes
79 constexpr double tH = 13.7*1e3; //Hubble time in Myr
80 constexpr double Mchandra = 1.41; //Chandrasekhar mass in Msun
81
82
83
84
85 const std::string PLACEHOLDER="xxx"; //Standard placeholder for input properties
86 //SY
87
88 //MAGIC NULL VALUES
89 constexpr double NULL_DOUBLE = -9e30;
90 constexpr int NULL_INT = -999999999;
91 constexpr size_t NULL_SINT = 999999999;
92 //constexpr std::string NULL_STR = "FORZAROMA"; //NOT POSSIBLE in C11 (It will be possible in C20)
93 const std::string NULL_STR = "FORZAROMA";
94
95 //MAGIC LARGE AN TINY VALUES
96 constexpr double DIFF_TOLL = 1e-10; //Tollerance on difference between two values
97 constexpr double LARGE = 1e30;
98 constexpr double TINY = 1e-15;
99 constexpr double DOUBLE_EPS = std::numeric_limits<double>::epsilon();
100
101
102 //INT CONSTANT TO HANDLE RETURN FROM EVOLUTION
103 typedef unsigned int evolution;
104 constexpr int SINGLE_STEP_EVOLUTION=0;
105 constexpr int REPEATED_EVOLUTION=1;
106
107 //INT CONSTANT TO HANDLE RETURN FROM FUNCTIONS
108 typedef unsigned int jump_convergence;
109 constexpr int JUMP_CONVERGE=0;
110 constexpr int JUMP=1;
111 constexpr int NO_JUMP=2;
112
113 //INT CONST FOR SN EXPLOSION
114 typedef unsigned int sn_explosion;
115 constexpr int SNIA_EXPLODE=1;
116 constexpr int SNII_EXPLODE=2;
117 constexpr int SN_NOT_EXPLODE=0;
118
119 //INT CONST FOR RLO
120 typedef unsigned int rlo;
121 constexpr int RLO_FALSE=0; //RLO is happening or happened
122 constexpr int RLO_TRUE=1; //RLO is happening or happened
123
124 //bool CONST FOR BINARY EVOLUTION
125 typedef bool bse_evolution;
126 constexpr int BIN_EV_DONE = 1; //This is the return if the properties of the binary have been evolved with the proper evolve method
127 constexpr int BIN_EV_NOT_DONE = 0; //This is the return if the properties of the binary have been evolved without the proper evolve method
128 constexpr int BIN_EV_SETBROKEN = 2; //This is the return if the properties of the binary have not been evolved but a set broken has been called
129
130 double maxwellian_cdf(double x, double sigma);
131 double maxwellian_pdf(double x, double sigma);
132 inline double R_Schwarzschild(double Mass){return 2.0*G_over_c2*Mass; } //rs=2GM/c^2}
133
134
144 template <typename T> const std::string n2s(T val, const char* file_input, const int line_input, const unsigned int precision=6) {
145 SevnLogging svlog;
146
147 std::ostringstream stream;
148 stream << std::scientific << std::setprecision(precision);
149 stream << val;
150
151 if (stream.fail())
152 svlog.critical("Cannot convert into a string", file_input, line_input);
153
154 return stream.str();
155 }
156
157
159
164 const double Mass; //Mass
165 const double MHE; //Mass of the HE core
166 const double MCO; //Mass of the CO core
167 };
168
176 double roche_lobe_Eg(double Mass_primary, double Mass_secondary, double a);
177
185 double R_Alfven(Star *s, double dMdt, bool get0=false);
186
193 double Hfrac(Star *s);
194
205 double dMdt_Eddington_accretion(Star *donor, Star *accretor, double eddfact=1.0);
206
207
214 inline static double omega_crit(double Mass, double Rpolar){
215 double Reqc = 1.5 *Rpolar;
216 return std::sqrt(utilities::G*Mass/(Reqc*Reqc*Reqc));
217 }
218
219
223 inline void hardwait(){
224 std::cout<<"\nWaiting"<<std::endl;
225 std::cin.get();
226 }
230 template<typename T, typename... Tail>
231 void hardwait(T head, Tail... tail){
232 std::cout << head << " ";
233 hardwait(tail...);
234 }
235
236
237
238
239
244 inline void wait(){
245 #ifdef DEBUG
246 std::cout<<"\nWaiting"<<std::endl;
247 char _fake;
248 std::cin>>_fake;
249 #endif
250 }
251
259 template<typename T, typename... Tail>
260 void wait(_UNUSED T head, _UNUSED Tail... tail){
261 #ifdef DEBUG
262 std::cout << head << " ";
263 wait(tail...);
264 #endif
265 }
266
267 /********** Template variadic function to be used in the log *******/
268
269 std::string get_name(Star* s);
270 long get_ID(Star* s);
271 std::string get_name(Binstar* b);
272 long get_ID(Binstar* b);
273 double get_current_time(Star* s);
274 double get_current_time(Binstar* b);
275
276
277
278
279 template <typename T>
280 void _log_print_core(std::stringstream &ss, T t){
281 ss<<t;
282 return;
283 }
284
285 template <typename T, typename... ListP>
286 void _log_print_core(std::stringstream &ss, T t, ListP... args){
287
288 ss<<t<<":";
289 _log_print_core(ss,args...);
290
291 return;
292 }
293
294 template <class System, typename... ListP>
295 std::string common_log_print(const std::string& label, System* system, ListP... args){
296
297 std::stringstream ss;
298 _log_print_core(ss,args...);
299
300 std::string id_str=std::to_string(get_ID(system));
301 std::string time = utilities::n2s(get_current_time(system),__FILE__,__LINE__);
302
303 return get_name(system) +";" +id_str + ";" +label + ";" + time + ";" + ss.str();
304
305 }
306
307 template <class System, typename... ListP>
308 std::string common_log_print(const std::string& label, System* system){
309
310
311 std::string id_str=std::to_string(get_ID(system));
312 std::string time = utilities::n2s(get_current_time(system),__FILE__,__LINE__);
313
314 return get_name(system) +";" +id_str + ";" +label + ";" + time + ";";
315
316 }
317
318
319 template <typename... ListP>
320 std::string log_print(const std::string& label, Star* star, ListP... args){
321
322 return "S;" + common_log_print(label,star,args...);
323 }
324
325 template <typename... ListP>
326 std::string log_print(const std::string& label, Binstar *binstar, ListP... args){
327
328 return "B;" + common_log_print(label,binstar,args...);
329 }
330
337 std::string log_star_info(Star* s, bool oldstep=false);
338
339
340 /*************************************************************************************/
341
342
343 unsigned long gen_rseed();
344 unsigned long gen_rseed(std::random_device &rd);
345
346 inline const std::string random_keygen(std::mt19937_64 *mtrand){
347
348 //std::string allkeys = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
349 std::string keys_init = "123456789"; //GI71119: MORE SIMILAT TO A source_id GAIA like (it can easier to load and manage as a pure number table)
350 std::string keys = "0123456789";
351
352 std::uniform_int_distribution<int> unifo_keys_init(0, (int)keys_init.size()-1);
353 std::uniform_int_distribution<int> unifo_keys(0, (int)keys.size()-1);
354
355 std::string key = "";
356 key += keys_init[(unifo_keys_init(*mtrand))];
357 for(int i = 0; i < 14; i++){
358 int position = unifo_keys(*mtrand);
359 key += keys[position];
360 }
361
362 return key;
363
364 }
365
366 std::vector<std::string> split(const std::string& s, char delimiter);
367
368
369
370 template <class T> inline bool isifstream();
371 template <> inline bool isifstream<std::ifstream>() {return true;}
372
373 //general functions
374 template <typename T> size_t binary_search(T *array, const size_t left, const size_t right, const T value){
375
376
377 if (right > left+1) {
378
379 const size_t mid = left + ((right - left)>>1);
380 //std::cout<<left<<" "<<mid<<" "<<right<<std::endl;
381 //std::cout<<"****"<<array[left]<<std::endl;
382 //std::cout<<array[mid]<<std::endl;
383 //std::cout<<array[right]<<std::endl;
384
385 if(array[mid] > value)
386 return binary_search(array, left, mid, value);
387 else if (array[mid] == value)
388 return mid;
389 else
390 return binary_search(array, mid, right, value);
391 }
392
393 if(array[left] == value) return left;
394 else if(array[right] == value) return right;
395 else return left;
396
397 }
398
399
400 template <typename T> bool string_is_number(std::string str) {
401 T value;
402
403 std::stringstream stream(str);
404 stream >> value;
405
406 return (!stream.fail() and stream.eof()) ;
407 }
408
419 inline double kepler(const double &ecc, const double &m, const double tol = 1e-6, const int maxit = 50) { // it solves the Kepler's equation giving the mean anomaly and the eccentricity
420
421 //We use a Generalize Newton-Raphson method that converge cubically (instead of quadratically for the classical Newton Raphson)
422 //Given the Kepler Equation we can have problem if f1 is 0, if f2 is 0 or if f1*f1-2*f*f2 is <=0
423 //f1=1-ecc*cos(E) but ecc<1 always so f1<1 as well. When f2 is 0 or f1*f1-2*f*f2 is <=0 we revert to a simple NR for that step
424
425 int k = 1; // to check max iterations
426 double em = m; //em = eccentric anomaly (output), m = mean anomaly (input)
427 double sinem = sin(em);
428
429 //If eccentricity is 0 we have M=E, i.e. the Eccentricity anomaly is equal to the Mean anomaly
430 if (ecc==0)
431 return m;
432
433 double f = em - ecc*sinem - m;
434 double f1 = 1.0 - ecc*cos(em);
435 double f2 = ecc*sinem;
436 double fsquare=f1*f1 - 2*f*f2;
437 auto check_problem = [&f2,&fsquare](){ return f2==0 or fsquare<=0;}; //Lmabda expression to check if I have to pass to a NR
438
439
440 if (check_problem()) //If the second derivatie is 0 make a step using the first order Newthon Methond
441 em = em - f/f1;
442 else
443 em = em + (-f1 + sqrt(fsquare))/f2;
444
445 // iterations
446 while(fabs(f) > tol && k < maxit){
447
448
449
450 sinem = sin(em);
451 f = em - ecc*sinem - m;
452 f1 = 1.0 - ecc*cos(em);
453 f2 = ecc*sinem;
454 fsquare=f1*f1 - 2*f*f2;
455
456
457 if (check_problem()) //If the second derivatie is 0 make a step using the first order Newthon Methond
458 em = em - f/f1;
459 else
460 em = em + (-f1 + sqrt(fsquare))/f2;
461
462
463
464 k++;
465 }
466
467 return em;
468
469 }
470
471 //templates
472 template <typename T> const T s2n(std::string &str, const char* file_input, const int line_input) {
473 SevnLogging svlog;
474
475 T value;
476
477
478 std::stringstream stream(str);
479 stream >> value;
480
481
482 if (stream.fail())
483 svlog.critical("Cannot convert the string " + str + "into a number", file_input, line_input,sevnstd::sevnio_error());
484
485 return value;
486 }
487
488
489 template <typename T> void _openfile(T &in, const std::string f, const char* file_input, const int line_input){
490 SevnLogging svlog;
491
492
493 if(in.is_open()) in.close();
494 if(utilities::isifstream<T>())
495 in.open(f.c_str(), std::ios::in);
496 else
497 in.open(f.c_str(), std::ios::out);
498
499 if(!in) svlog.critical("Cannot open file " + f, file_input, line_input,sevnstd::sevnio_error());
500 }
501
502
503
504
505 //useful function to sort arrays and indexes
506 inline bool wayToSort(int i, int j) { return i > j; }
507
508
509 template <typename T> T dirname2n(std::string str, const char* file_input, const int line_input){
510
511 std::size_t found = str.find('.');
512 if (found == std::string::npos)
513 str.insert(1,".");
514
515 return utilities::s2n<T>(str, file_input, line_input);
516
517 }
518
519
520 //GI
524 template <typename T> void print_vector(const std::vector<T>& v){
525 std::cout<< "[ ";
526 for (auto& element : v)
527 std::cout << element << " ";
528 std::cout<< "]"<<std::endl;
529 }
530
531
532 //GI
540 inline std::string gen_filename(const std::string &_folder, const std::string &_fname, bool print_threads=true){
541
542 std::string return_string;
543 std::string folder = _folder.back()=='/' ? _folder.substr(0, _folder.length()-1) : _folder; //GI 141219: To avoid to have a double / if the _folder in input already contains a / at the end
544
545 if (print_threads){
546
547 std::size_t found_extension;
548
549 //GI 141219: Simple loop to get only the last occurence of . in the string
550 for ( std::size_t pos=0; pos!=std::string::npos; pos=_fname.find('.',pos+1)) found_extension = pos;
551 if (found_extension==0) found_extension = _fname.length();
552
553 #ifdef _OPENMP
554 int num_thread=omp_get_thread_num();
555 #else
556 int num_thread=0;
557 #endif
558
559 return_string = folder + "/" + _fname.substr(0, found_extension) + "_" + std::to_string(num_thread) + _fname.substr(found_extension);
560 } else {
561
562 return_string = folder + "/" + _fname;
563 }
564
565 return return_string;
566 }
567
568
573 inline std::string get_absolute_SEVN_path(){
574 std::string here_token = "include/";
575 std::string sevn_path = __FILE__;
576 std::size_t found = sevn_path.find("/src/");
577
578
579 static sevnstd::SevnLogging svlog;
580 if (found!=std::string::npos){
581 sevn_path = sevn_path.substr(0,found);
582 }
583 else{
584 svlog.critical("Error in the initialisation of parameter myself (path of the SEVN folder)."
585 " The path finding it is based on the fact that the utilities.h file is inside the folder include "
586 "in the main SEVN folder. Did you change it?",__FILE__,__LINE__,sevnstd::sevnio_error());
587 }
588
589 return sevn_path;
590 }
591
602 inline int find_line(const double & x1, const double & x2, const double & y1, const double & y2, double & slope, double & intercept){
603 slope = (y2 - y1)/(x2-x1);
604 intercept = y2 -slope*x2;
605
606 return EXIT_SUCCESS;
607 }
608
609 template<typename T>
610 double rel_difference(T val1, T val2){
611
612 return fabs( (val1-val2)/val1 );
613 }
614
615
616
617 inline void swap_stars(Star* & s1, Star* & s2){
618 Star *stmp=s1;
619 s1=s2;
620 s2=stmp;
621 }
622
628 inline std::string trim(const std::string& s) {
629 return std::regex_replace(s, std::regex("^[ \\s]+|[ \\s]+$"), std::string(""));
630 }
631
641 template<typename T, typename Iter>
642 bool isinlist(T element, Iter it, Iter end){
643
644 return std::find(it, end, element)!=end;
645 }
646
653 inline std::string make_pfile_str(const double plife, const size_t Phase, const unsigned int min_precision=6){
654
655 //In the following rows we estimate plife and the transform the number to a string to initialise stars.
656 //Hovewer, if plife is very close to 0 or 1 without enough number of digits we can artificially force plife to be 0 or 1
657 //Theregore we dynamically set the precision estimating the difference between plife and 0 or 1, taking the exponent of log10
658 //and using the next larger integer.
659 unsigned int precision; //Set the precision to transform pfile from number to string
660 unsigned int digit_0= std::ceil(std::abs(std::log10(std::abs(plife-0))));
661 unsigned int digit_1= std::ceil(std::abs(std::log10(std::abs(plife-1))));
662 precision=std::max(std::max(digit_0,digit_1),min_precision);
663
664 //Consistency check
665 std::stringstream tini_ss; //string containing the plife (see below)
666 //Close to plife=1 is necessary to use a large number of digits to avoid to set plife=1 when trasformi to string
667 tini_ss << "%" << std::setprecision(precision) << plife * 100 << ":" << Phase; //Starting time
668
669 return tini_ss.str();
670 }
671
672
673 //Interpolator
684 template <typename T> T interpolate_1D(T xp, std::vector<T>& x_interp, std::vector<T>& y_interp, bool equispaced_interval=false, bool ext_raise=false){
685
686
687 if (xp<x_interp[0] and ext_raise)
688 throw sevnstd::sevnerr("Error in interpolate_1D in utility.h: xp is out of boundary.");
689 else if (xp<=x_interp[0])
690 return y_interp[0];
691
692 if (xp>x_interp.back() and ext_raise)
693 throw sevnstd::sevnerr("Error in interpolate_1D in utility.h: xp is out of boundary.");
694 else if (xp>=x_interp.back())
695 return y_interp.back();
696
697 size_t pos;
698 if (equispaced_interval){
699 double dx = x_interp[1]-x_interp[0];
700 pos = int( (xp-x_interp[0])/dx); //int return floor
701 }
702 else{
703 pos = binary_search(&x_interp[0], 0, x_interp.size()-1, xp);
704 }
705 return (y_interp[pos+1]-y_interp[pos])/(x_interp[pos+1]-x_interp[pos])*(xp-x_interp[pos])+y_interp[pos];
706 }
707
714 inline std::string get_subpath(std::string path, std::string split_string, bool include_split_string=true){
715
716 size_t tt= path.find(split_string);
717 std::string subpath;
718
719 if (include_split_string){
720 subpath = path.substr(0,tt+split_string.size());
721 } else{
722 subpath = path.substr(0,tt);
723 }
724
725 return subpath;
726 }
727
734 template <typename T>
735 void transpose(std::vector<std::vector<T>>& MatrixT, std::vector<std::vector<T>>& Matrix){
736
737 MatrixT.resize(Matrix.size());
738
739 for (auto& Matrix_row : Matrix){
740 for (int j=0; j<(int)Matrix_row.size(); j++)
741 MatrixT[j].push_back(Matrix_row[j]);
742 }
743
744 }
745
746
755 template < typename T>
756 int findInVector(const std::vector<T> & vecOfElements, const T & element)
757 {
758 int result ;
759 // Find given element in vector
760 auto it = std::find(vecOfElements.begin(), vecOfElements.end(), element);
761 if (it != vecOfElements.end())
762 {
763 result = distance(vecOfElements.begin(), it);
764
765 }
766 else
767 {
768 result=-1;
769 }
770 return result;
771 }
772
773
774 template <typename Key, typename Value>
775 std::map<Value,Key> flip_map(const std::map<Key,Value> &original_map){
776 std::map<Value,Key> flipped_map;
777
778 for (auto& pair : original_map){
779 flipped_map[pair.second] = pair.first;
780 }
781
782 return flipped_map;
783 }
784
795 bool areEqual(double x, double y);
796
803 double smallestSignificativeStep(double x);
804
805
814 template<typename T, typename... Args>
815 inline std::unique_ptr<T> make_unique(Args&&... args) {
816 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
817 }
818
820
826
827 private:
828
829 double vcurrent;
830
831 double vstep=std::nan("");
832 double vmin=0.;
833 double vmax=2E30;
834
835 std::vector<double> vlist;
836 std::vector<double>::iterator begin;
837 std::vector<double>::iterator end;
838 std::vector<double>::iterator current;
839 bool end_of_list=false;
840
842 begin=vlist.begin();
843 end=vlist.end();
845 }
846 public:
848 vlist={};
850 }
851
859 explicit ListGenerator(double _vstep, double _vstep_max=std::nan(""), double _vstep_min=std::nan(""))
860 : vstep{_vstep}, vmin{_vstep_min}, vmax{_vstep_max} {
861
862 if (vstep<=0)
863 throw std::runtime_error("ListGenerator::vstep cannot be negative or zero");
864 if (std::isnan(vmin))
865 vmin=vstep;
866 if (std::isnan(vmax))
867 vmax=2E30;
868
869 if(vmin >= vmax)
870 throw std::runtime_error("ListGenerator::vmin cannot be larger than ListGenerator::vmax");
871
873 }
874
880 explicit ListGenerator(const std::vector<double>& _tlist) : vlist{_tlist} {
881
882 if (!std::is_sorted(vlist.begin(),vlist.end()))
883 throw std::runtime_error("The input vector in the ListGenerator constructor is not sorted");
884
886 if (!vlist.empty()){
888 vmin=*begin;
889 vmax=*(end-1);
890 }
891 }
892
893 static std::unique_ptr<ListGenerator> make_unique(double _vstep, double _vstep_max=std::nan(""), double _vstep_min=std::nan(""));
894 static std::unique_ptr<ListGenerator> make_unique(std::vector<double> _vlist);
895
896 inline double get() const{
897 if (empty())
898 throw std::out_of_range("The list of times reached the end. The current value is undefined");
899 return vcurrent;
900 }
901 inline double get_max() const {return vmax;}
902 inline double get_min() const {return vmin;}
903
904 inline bool empty() const {return end_of_list;}
905 inline void next(){
906 //If empty just throw an out_of_range error
907 if (empty())
908 throw std::out_of_range("The list of times reached the end. The next value is undefined");
909 //If we are using the vstep implementation and the next step is going beyond vmax flag end_of_list
910 else if(!std::isnan(vstep) and vcurrent+vstep>vmax)
911 end_of_list=true;
912 //If we are using the vstep implementation and the next step is not going beyond vmax just increment vcurrent
913 else if(!std::isnan(vstep))
915 //Now start with the vlist implementation check
916 //Update the iterator and check if we reach the end
917 else if(++current==end){
918 end_of_list=true;
919 }
920 //If we are here We are still inside the list, update the iterator and vcurrent.
921 else{
923 }
924 }
925 inline double operator++(){
926 next();
927 return vcurrent;
928 }
929 inline double operator++(int){
930 double old_t=vcurrent;
931 next();
932 return old_t;
933 }
938 inline double forecast() const {
939 //If empty just throw an out_of_range error
940 if (empty())
941 return std::nan("");
942 else if(!std::isnan(vstep) and vcurrent+vstep>vmax)
943 return std::nan("");
944 else if(!std::isnan(vstep))
945 return vcurrent+vstep;
946 else if(current+1==end)
947 return std::nan("");
948 else
949 return *(current+1);
950 }
951
952 };
953
954
955
956}
957
958
959#endif //SEVN_REVISED_UTILITIES_H
960
#define _UNUSED
Definition: BinaryProperty.h:20
Definition: binstar.h:26
Definition: property.h:776
Definition: property.h:881
Definition: star.h:39
Definition: property.h:1700
Definition: sevnlog.h:43
void critical(std::string errstate, const char *file_input=nullptr, int line_input=-1) const
Definition: sevnlog.cpp:85
Definition: errhand.h:24
Definition: errhand.h:53
Class to handle the generation of a list of values.
Definition: utilities.h:825
std::vector< double >::iterator end
Definition: utilities.h:837
static std::unique_ptr< ListGenerator > make_unique(double _vstep, double _vstep_max=std::nan(""), double _vstep_min=std::nan(""))
Definition: utilities.cpp:158
double operator++(int)
Definition: utilities.h:929
double vmin
Definition: utilities.h:832
std::vector< double > vlist
Definition: utilities.h:835
void next()
Definition: utilities.h:905
double forecast() const
Definition: utilities.h:938
double get() const
Definition: utilities.h:896
ListGenerator(double _vstep, double _vstep_max=std::nan(""), double _vstep_min=std::nan(""))
Definition: utilities.h:859
std::vector< double >::iterator current
Definition: utilities.h:838
double get_max() const
Definition: utilities.h:901
double get_min() const
Definition: utilities.h:902
bool empty() const
Definition: utilities.h:904
double vstep
Definition: utilities.h:831
ListGenerator(const std::vector< double > &_tlist)
Definition: utilities.h:880
ListGenerator()
Definition: utilities.h:847
std::vector< double >::iterator begin
Definition: utilities.h:836
bool end_of_list
Definition: utilities.h:839
double operator++()
Definition: utilities.h:925
double vmax
Definition: utilities.h:833
void initialise_tlist_iterators()
Definition: utilities.h:841
double vcurrent
Definition: utilities.h:829
Definition: utilities.h:33
constexpr double Myr_to_yr
Definition: utilities.h:68
constexpr int SNII_EXPLODE
Definition: utilities.h:116
std::string trim(const std::string &s)
Definition: utilities.h:628
bool areEqual(double x, double y)
Definition: utilities.cpp:141
constexpr double G
Fundamental quantitis.
Definition: utilities.h:59
constexpr double c
Definition: utilities.h:73
double kepler(const double &ecc, const double &m, const double tol=1e-6, const int maxit=50)
Definition: utilities.h:419
T interpolate_1D(T xp, std::vector< T > &x_interp, std::vector< T > &y_interp, bool equispaced_interval=false, bool ext_raise=false)
Definition: utilities.h:684
double smallestSignificativeStep(double x)
Definition: utilities.cpp:150
constexpr double G_cgs
Definition: utilities.h:64
std::string get_name(Star *s)
Definition: utilities.cpp:76
void hardwait()
Definition: utilities.h:223
double Hfrac(Star *s)
Definition: utilities.cpp:120
static double omega_crit(double Mass, double Rpolar)
Definition: utilities.h:214
std::string make_pfile_str(const double plife, const size_t Phase, const unsigned int min_precision=6)
Definition: utilities.h:653
constexpr int RLO_FALSE
Definition: utilities.h:121
constexpr int SINGLE_STEP_EVOLUTION
Definition: utilities.h:104
constexpr double DOUBLE_EPS
Definition: utilities.h:99
double R_Alfven(Star *s, double dMdt, bool get0=false)
Definition: utilities.cpp:55
std::map< Value, Key > flip_map(const std::map< Key, Value > &original_map)
Definition: utilities.h:775
constexpr size_t NULL_SINT
Definition: utilities.h:91
void wait()
Definition: utilities.h:244
std::string common_log_print(const std::string &label, System *system, ListP... args)
Definition: utilities.h:295
std::string get_absolute_SEVN_path()
Definition: utilities.h:573
constexpr double Sigma_StefBoltz
Definition: utilities.h:67
constexpr double kms_to_RSunyr
Definition: utilities.h:71
constexpr int NULL_INT
Definition: utilities.h:90
constexpr double Rsun_cgs
Definition: utilities.h:62
constexpr int REPEATED_EVOLUTION
Definition: utilities.h:105
bool bse_evolution
Definition: utilities.h:125
void _log_print_core(std::stringstream &ss, T t)
Definition: utilities.h:280
constexpr int BIN_EV_DONE
Definition: utilities.h:126
constexpr int BIN_EV_SETBROKEN
Definition: utilities.h:128
bool isinlist(T element, Iter it, Iter end)
Definition: utilities.h:642
double get_current_time(Star *s)
Definition: utilities.cpp:88
constexpr int BIN_EV_NOT_DONE
Definition: utilities.h:127
constexpr double parsec_to_Rsun
Definition: utilities.h:75
constexpr double tH
Definition: utilities.h:79
constexpr int RLO_TRUE
Definition: utilities.h:122
int find_line(const double &x1, const double &x2, const double &y1, const double &y2, double &slope, double &intercept)
Definition: utilities.h:602
const std::string n2s(T val, const char *file_input, const int line_input, const unsigned int precision=6)
Definition: utilities.h:144
constexpr double g_to_MSun
Definition: utilities.h:76
const std::string random_keygen(std::mt19937_64 *mtrand)
Definition: utilities.h:346
constexpr double yr_cgs
Definition: utilities.h:60
int findInVector(const std::vector< T > &vecOfElements, const T &element)
Definition: utilities.h:756
long get_ID(Star *s)
Definition: utilities.cpp:79
unsigned int evolution
Definition: utilities.h:103
double maxwellian_pdf(double x, double sigma)
Definition: utilities.cpp:35
std::vector< std::string > split(const std::string &s, char delimiter)
Definition: utilities.cpp:19
constexpr int JUMP
Definition: utilities.h:110
constexpr double G_over_c2
Definition: utilities.h:77
constexpr double LSun_to_Solar
Definition: utilities.h:72
constexpr double parsec_cgs
Definition: utilities.h:61
void transpose(std::vector< std::vector< T > > &MatrixT, std::vector< std::vector< T > > &Matrix)
Definition: utilities.h:735
std::string gen_filename(const std::string &_folder, const std::string &_fname, bool print_threads=true)
Definition: utilities.h:540
constexpr double G3_over_c5
Definition: utilities.h:78
constexpr double km_to_RSun
Definition: utilities.h:74
std::string log_print(const std::string &label, Star *star, ListP... args)
Definition: utilities.h:320
double dMdt_Eddington_accretion(Star *donor, Star *accretor, double eddfact=1.0)
Definition: utilities.cpp:125
unsigned int jump_convergence
Definition: utilities.h:108
constexpr double TINY
Definition: utilities.h:98
void swap_stars(Star *&s1, Star *&s2)
Definition: utilities.h:617
double R_Schwarzschild(double Mass)
Definition: utilities.h:132
const std::string PLACEHOLDER
Definition: utilities.h:85
const std::string NULL_STR
Definition: utilities.h:93
unsigned int sn_explosion
Definition: utilities.h:114
T dirname2n(std::string str, const char *file_input, const int line_input)
Definition: utilities.h:509
constexpr double Mchandra
Definition: utilities.h:80
constexpr double Msun_cgs
Definition: utilities.h:63
size_t binary_search(T *array, const size_t left, const size_t right, const T value)
Definition: utilities.h:374
const std::string SEVN_NAME
Definition: utilities.h:49
constexpr double yr_to_Myr
Definition: utilities.h:69
constexpr int SN_NOT_EXPLODE
Definition: utilities.h:117
constexpr int SNIA_EXPLODE
Definition: utilities.h:115
unsigned int rlo
Definition: utilities.h:120
constexpr double NULL_DOUBLE
Definition: utilities.h:89
constexpr double AU_to_RSun
Definition: utilities.h:70
std::string log_star_info(Star *s, bool oldstep=false)
Definition: utilities.cpp:96
std::unique_ptr< T > make_unique(Args &&... args)
Definition: utilities.h:815
constexpr int JUMP_CONVERGE
Definition: utilities.h:109
double maxwellian_cdf(double x, double sigma)
Definition: utilities.cpp:30
bool wayToSort(int i, int j)
Definition: utilities.h:506
unsigned long gen_rseed()
Definition: utilities.cpp:11
bool isifstream()
bool isifstream< std::ifstream >()
Definition: utilities.h:371
void print_vector(const std::vector< T > &v)
Definition: utilities.h:524
void _openfile(T &in, const std::string f, const char *file_input, const int line_input)
Definition: utilities.h:489
constexpr int NO_JUMP
Definition: utilities.h:111
const T s2n(std::string &str, const char *file_input, const int line_input)
Definition: utilities.h:472
double rel_difference(T val1, T val2)
Definition: utilities.h:610
constexpr double DIFF_TOLL
Definition: utilities.h:96
std::mt19937_64 mtrand
Definition: utilities.cpp:9
bool string_is_number(std::string str)
Definition: utilities.h:400
std::string get_subpath(std::string path, std::string split_string, bool include_split_string=true)
Definition: utilities.h:714
constexpr double LARGE
Definition: utilities.h:97
double roche_lobe_Eg(double Mass_primary, double Mass_secondary, double a)
Definition: utilities.cpp:41
Definition: utilities.h:163
const double Mass
Definition: utilities.h:164
const double MHE
Definition: utilities.h:165
const double MCO
Definition: utilities.h:166