SEVN
Loading...
Searching...
No Matches
evolve.h
Go to the documentation of this file.
1//
2// Created by Giuliano Iorio on 2020-12-26.
3//
4
5#ifndef SEVN_EVOLVE_H
6#define SEVN_EVOLVE_H
7
8#ifdef _OPENMP
9#include <omp.h>
10#endif
11
12#include <star.h>
13#include <binstar.h>
14#include <IO.h>
15#include <vector>
16#include <random>
17#include <errhand.h>
18#include <sevnlog.h>
20
21//TODO With the new functor implementation, the evolve function can be passed as parameters as used for other options. TO be implemented
22//TODO the evolve function should not be put in the SEVN libraries, so we should move it in the main folder
23
139
146 struct Options{
147 std::vector<double> num_parameters;
148 std::vector<std::string> str_parameters;
149 };
150
151
162 public:
163 explicit EvolveFunctor(SevnLogging& svlog, bool record_state=true, bool include_failed=false, Options* options=nullptr)
164 : _svlog(svlog), _record_state(record_state), _include_failed(include_failed), _options{options} {};
165 virtual ~EvolveFunctor() = default;
166 virtual inline int operator() (_UNUSED Binstar& binary){ return EXIT_SUCCESS;}
167 virtual inline int operator() (_UNUSED Star& star){ return EXIT_SUCCESS;}
168 virtual inline std::string name() { return "Pure Virtual Base EvolveFunctor";}
169
181 template <typename System>
182 static inline bool check_stalling(System& system) {
183 static long dt_threshold = long(system.get_svpar_num("check_stalling_time")); //Threshold to check for stalling system in seconds
184 if (system.get_svpar_bool("check_stalling")){
185
186 static std::chrono::steady_clock::time_point clock_begin;
187 static size_t _id{0};
188 static std::string _name;
189
190 //Set value and start new clock for a new system
191 if (system.get_name()!=_name or system.get_ID()!=_id){
192 clock_begin = std::chrono::steady_clock::now();
193 _id = system.get_ID();
194 _name = system.get_name();
195 }
196
197 if (elapsed_time(clock_begin)>dt_threshold){
198 SevnLogging svlog;
199 svlog.critical("System with name " + system.get_name() +
200 " and ID " + utilities::n2s(system.get_ID(),__FILE__,__LINE__) +
201 " evolved for more than " + utilities::n2s(dt_threshold,__FILE__,__LINE__) +
202 "s this is not expected."
203 "The evolution is skipped and the system flagged ad failed.",
204 __FILE__,
205 __LINE__,
206 sevnstd::sevnerr(""));
207 }
208
209 }
210
211 return EXIT_SUCCESS;
212 }
213
214 protected:
216 const bool _record_state;
217 const bool _include_failed;
219
220 template <class System>
221 inline void final_print(System& system, bool evolution_result){
222
223 if (evolution_result==EXIT_SUCCESS)
224 system.print();
225 else
226 system.print_failed(_include_failed);
227 }
228
237 template <class System>
238 static void catched_error_message(System& system, const sevnstd::sevnerr &e){
239 std::string errmedd= "Evolution of system "+system.get_name() + "(ID "+ std::to_string(system.get_ID())+
240 ") broken by an error: "+e.what();
241 std::cerr<<errmedd<<std::endl;
242 }
243
250 static inline long elapsed_time(std::chrono::steady_clock::time_point start_clock){
251 return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now()-start_clock).count();
252 }
253 };
254
264 class EvolveDebug : public EvolveFunctor {
265 public:
266 explicit EvolveDebug(SevnLogging& svlog, bool record_state=true, bool include_failed=false)
267 : EvolveFunctor(svlog,record_state,include_failed) {};
268 inline int operator() (Binstar& binary) override {
269 return evolve_debug(binary);
270 }
271 inline int operator() (Star& star) override {
272 return evolve_debug(star);
273 }
274 inline std::string name() override { return "EvolveDefault";}
275
276 private:
277 template <typename System>
278 inline int evolve_debug(System& system){
279
280 bool evolution_result = EXIT_SUCCESS; //Reset to true
281 final_print(system,evolution_result); //print out all the recorded states for the star
282 /********************************/
283
284 return evolution_result;
285 }
286 };
287
288
301 public:
302 explicit EvolveDefault(SevnLogging& svlog, bool record_state=true, bool include_failed=false)
303 : EvolveFunctor(svlog,record_state,include_failed) {};
304 inline int operator() (Binstar& binary) override {
305 return evolve_default(binary);
306 }
307 inline int operator() (Star& star) override {
308 return evolve_default(star);
309 }
310 inline std::string name() override { return "EvolveDefault";}
311
312 private:
313 template <typename System>
314 inline int evolve_default(System& system){
315
316 bool evolution_result = EXIT_SUCCESS; //Reset to true
317
318 if (_record_state) system.recordstate(); //always record the initial state
319
320 for(;;) {
321 //TODO Myabe is better to use try..catch.. outside the for?
322 try {
323 check_stalling(system); //Check for stalling systems
324 system.evolve();
325 if (system.breaktrigger()) //stopping condition for evolving the star
326 break; //go to evolve the next star
327
328 if ( (system.isoutputtime() or system.printall()) and _record_state) {
329 system.recordstate_w_timeupdate();
330 }
331 }
332 catch(sevnstd::sevnerr& e){
333
334 catched_error_message(system,e);
335
336 evolution_result=EXIT_FAILURE;
337 break;
338 }
339 }
340 if (_record_state) {
341 system.recordstate(); //always record the final state
342 }
343
344
345 //print out all the recorded states for the star
346 final_print(system,evolution_result); //print out all the recorded states for the star
347 /********************************/
348
349 return evolution_result;
350 }
351 };
352
364 public:
365 explicit EvolveStopCondition(SevnLogging& svlog, bool record_state=true, bool include_failed=false)
366 : EvolveFunctor(svlog,record_state,include_failed) {};
367 virtual inline int operator() (Binstar& binary) override {
368
369
370 bool evolution_result = EXIT_SUCCESS; //Reset to true
371
372 for(;;) {
373
374 try {
375 check_stalling(binary); //Check for stalling systems
377 binary.evolve();
378
380 //If special break condition is flagged and _record_state is true:
381 // record_state, print it to the file and exit.
382 if (stop_condition(binary) and _record_state){
383 binary.recordstate(); //always record the final state
384 break; //Exit
385 }
386 //If special break condition or natural breaktrigger (controlled by input options) just break
387 else if (stop_condition(binary) or binary.breaktrigger())
388 break;
389 }
390 catch(sevnstd::sevnerr& e){
391
392 catched_error_message(binary,e);
393
394 evolution_result = EXIT_FAILURE;
395 break;
396
397 }
398 }
399 /********************************/
400 final_print(binary,evolution_result); //print out all the recorded states for the star
401 return evolution_result;
402 }
403 virtual inline int operator() (Star& star) override {
404
405
406 bool evolution_result = EXIT_SUCCESS; //Reset to true
407
408 for(;;) {
409
410 try {
411 check_stalling(star); //Check for stalling systems
413 star.evolve();
414
416 //If special break condition is flagged and _record_state is true:
417 // record_state, print it to the file and exit.
418 if (stop_condition(star) and _record_state){
419 star.recordstate(); //always record the final state
420 break; //Exit
421 }
422 //If special break condition or natural breaktrigger (controlled by input options) just break
423 else if (stop_condition(star) or star.breaktrigger())
424 break;
425 }
426 catch(sevnstd::sevnerr& e){
427
428 catched_error_message(star,e);
429
430 evolution_result = EXIT_FAILURE;
431 break;
432
433 }
434 }
435 /********************************/
436 final_print(star,evolution_result); //print out all the recorded states for the star
437 return evolution_result;
438 }
439 inline std::string name() override { return "EvolveStopCondition";}
440 protected:
441 virtual inline bool stop_condition(_UNUSED Binstar& binstar){
442 return false;
443 }
444
445 virtual inline bool stop_condition(_UNUSED Star& star){
446 return false;
447 }
448 };
449
463 public:
464 explicit EvolveRecordCondition(SevnLogging& svlog, bool record_state=true, bool include_failed=false)
465 : EvolveFunctor(svlog,record_state,include_failed) {};
466 virtual inline int operator() (Binstar& binary) override {
467
468
469 bool evolution_result = EXIT_SUCCESS; //Reset to true
470 unsigned int Record_counter=0;
471
472 for(;;) {
473
474 try {
475 check_stalling(binary); //Check for stalling systems
477 binary.evolve();
478
479 //If record condition is true, update the BLC counter and record state (if allowed by other flags)
480 if (record_condition(binary)){
481 Record_counter++;
482 //Record only if it is time (or print only end and this is the start of the Record condition Record_counter=1) and if _record state is true
483 if ( ( (binary.isoutputtime() or binary.printall()) or Record_counter==1 ) and _record_state ) {
485 }
486 }
487 //If record_condition is false, but Record_counter>0 we just exit from the record condition, so record state (if allowed by other flags) ans reset the Record_counter
488 else if(Record_counter>0){
489 //Always print the end
490 if (_record_state){
492 }
493 //reset the Record_counter counter
494 Record_counter=0;
495 }
496
497 if (binary.breaktrigger()) //stopping condition for evolving the star
498 break; //go to evolve the next star
499
500 }
501 catch(sevnstd::sevnerr& e){
502
503
504 catched_error_message(binary,e);
505
506 evolution_result = EXIT_FAILURE;
507 break;
508
509 }
510 }
511 /********************************/
512 final_print(binary,evolution_result); //print out all the recorded states for the star
513 return evolution_result;
514 }
515 virtual inline int operator() (Star& star) override {
516
517
518 bool evolution_result = EXIT_SUCCESS; //Reset to true
519 unsigned int Record_counter=0;
520
521 for(;;) {
522
523 try {
524 check_stalling(star); //Check for stalling systems
526 star.evolve();
527
528 //If record condition is true, update the BLC counter and record state (if allowed by other flags)
529 if (record_condition(star)){
530 Record_counter++;
531 //Record only if it is time (or print only end and this is the start of the Record condition Record_counter=1) and if _record state is true
532 if ( ( (star.isoutputtime() or star.printall()) or Record_counter==1 ) and _record_state ) {
534 }
535 }
536 //If record_condition is false, but Record_counter>0 we just exit from the record condition, so record state (if allowed by other flags) ans reset the Record_counter
537 else if(Record_counter>0){
538 //Always print the end
539 if (_record_state){
541 }
542 //reset the Record_counter counter
543 Record_counter=0;
544 }
545
546 if (star.breaktrigger()) //stopping condition for evolving the star
547 break; //go to evolve the next star
548
549 }
550 catch(sevnstd::sevnerr& e){
551
552 catched_error_message(star,e);
553
554 evolution_result = EXIT_FAILURE;
555 break;
556
557 }
558 }
559 /********************************/
560 final_print(star,evolution_result); //print out all the recorded states for the star
561 return evolution_result;
562 }
563 inline std::string name() override { return "EvolveRecordCondition";}
564 protected:
565
566
567 virtual inline bool record_condition(_UNUSED Binstar& binstar){
568 return false;
569 }
570
571 virtual inline bool record_condition(_UNUSED Star& star){
572 return false;
573 }
574 };
575
576
589 public:
590 explicit EvolveBinaryCompact(SevnLogging& svlog, bool record_state=true, bool include_failed=false)
591 : EvolveStopCondition(svlog,record_state,include_failed) {};
592 inline int operator() (_UNUSED Star& star) override {
593 _svlog.critical("Evolve for star is not implemented for functor EvolveBinaryCompact",__FILE__,__LINE__,
595 return -1;
596 }
597
598 inline std::string name() override { return "EvolveBinaryCompact";}
599
600 protected:
601 inline bool stop_condition(Binstar& binstar) override {
602 return binstar.getstar(0)->amiCompact() and binstar.getstar(1)->amiCompact() and !binstar.broken;
603 }
604
605 };
606
618 //TODO Deprecated to be removed, new implementation is EvolveBinaryCompact
620 public:
621 explicit EvolveBinaryCompactOld(SevnLogging& svlog, bool record_state=true, bool include_failed=false)
622 : EvolveFunctor(svlog,record_state,include_failed) {};
623 inline int operator() (Binstar& binary) override {
624
625 bool special_break_condition = false;
626 bool evolution_result = EXIT_SUCCESS; //Reset to true
627
628 for(;;) {
629
630 try {
631 check_stalling(binary); //Check for stalling systems
632
634 binary.evolve();
635
637 special_break_condition = binary.getstar(0)->amiCompact() and binary.getstar(1)->amiCompact() and !binary.broken;
638
640 //If special break condition is flagged and _record_state is true:
641 // record_state, print it to the file and exit.
642 if (special_break_condition and _record_state){
643 binary.recordstate(); //always record the final state
644 break; //Exit
645 }
646 //If special break condition or natural breaktrigger (controlled by input options) just break
647 else if (special_break_condition or binary.breaktrigger())
648 break;
649 }
650 catch(sevnstd::sevnerr& e){
651
652 catched_error_message(binary,e);
653
654 evolution_result = EXIT_FAILURE;
655 break;
656
657 }
658 }
659 /********************************/
660 final_print(binary,evolution_result); //print out all the recorded states for the star
661 return evolution_result;
662 }
663 inline int operator() (_UNUSED Star& star) override {
664 _svlog.critical("Evolve for star is not implemented for functor EvolveBinaryCompact",__FILE__,__LINE__,
666 return -1;
667 }
668
669 inline std::string name() override { return "EvolveBinaryCompact";}
670
671 };
672
686 public:
687 explicit EvolveBBH(SevnLogging& svlog, bool record_state=true, bool include_failed=false)
688 : EvolveStopCondition(svlog,record_state,include_failed) {};
689 inline int operator() (_UNUSED Star& star) override {
690 _svlog.critical("Evolve for star is not implemented for functor EvolveBinaryCompact",__FILE__,__LINE__,
692 return -1;
693 }
694
695 inline std::string name() override { return "EvolveBBH";}
696
697 protected:
698 inline bool stop_condition(Binstar& binstar) override {
699 return binstar.getstar(0)->amiBH() and binstar.getstar(1)->amiBH() and !binstar.broken;
700 }
701
702 };
703
704
718 class EvolveBLC : public EvolveFunctor {
719 public:
720 explicit EvolveBLC(SevnLogging& svlog, bool record_state=true, bool include_failed=false)
721 : EvolveFunctor(svlog,record_state,include_failed) {};
722 inline int operator() (Binstar& binary) override {
723
724 bool BLC_flag = false, BLC_case1=false, BLC_case2=false;
725 //The BLC counter counts the evolution steps the systems has been in the BLC configuration
726 //It is increased by one if the BLC_flag is zero, while it is reset to zero when BLC_flag is false
727 unsigned int BLC_counter=0;
728 bool evolution_result = EXIT_SUCCESS; //Reset to true
729
730 for(;;) {
731 check_stalling(binary); //Check for stalling systems
732
733 try {
734
736 binary.evolve();
737
738 BLC_case1 = binary.getstar(0)->amiCompact() and !binary.getstar(1)->amiCompact();
739 BLC_case2 = binary.getstar(1)->amiCompact() and !binary.getstar(0)->amiCompact();
740
742 BLC_flag = (BLC_case1 or BLC_case2) and !binary.broken;
743
744 //If BLC_flag is true, update the BLC counter and record state (if allowed by other flags)
745 if (BLC_flag){
746 BLC_counter++;
747 //Record only if it is time (or print only end and this is the start of the BLC conditions BLC_counter=) and if _record state is true
748 if ( ( (binary.isoutputtime() or binary.printall()) or BLC_counter==1 ) and _record_state ) {
750 }
751 }
752 //If BLC_flag is false, but BLC_counter>0 we are at the end of a BLC phase, so record state (if allowed by other flags) ans reset the BLC counter
753 //FInally break, in fact if this system was in a BLC phase and not it is not, there is no way it will enter again in a BLC phase
754 else if(BLC_counter>0){
755
756 //Always print the end
757 if (_record_state){
759 }
760 //reset the BLC counter
761 BLC_counter=0;
762 break;
763 }
764
765 if (binary.breaktrigger()) //stopping condition for evolving the star
766 break; //go to evolve the next star
767
768 }
769 catch(sevnstd::sevnerr& e){
770
771 catched_error_message(binary,e);
772
773 evolution_result=EXIT_FAILURE;
774 break;
775 }
776 }
777 /********************************/
778 final_print(binary,evolution_result); //print out all the recorded states for the star
779 return evolution_result;
780 }
781 inline int operator() (_UNUSED Star& star) override {
782 _svlog.critical("Evolve for star is not implemented for functor EvolveBinaryCompact",__FILE__,__LINE__,
784 return -1;
785 }
786 inline std::string name() override { return "EvolveBLC";}
787
788
789 };
790
791
792
793
806 public:
807 explicit EvolveBCX1(SevnLogging& svlog, bool record_state=true, bool include_failed=false)
808 : EvolveStopCondition(svlog,record_state,include_failed) {};
809 inline int operator() (_UNUSED Star& star) override {
810 _svlog.critical("Evolve for star is not implemented for functor EvolveBinaryCompact",__FILE__,__LINE__,
812 return -1;
813 }
814
815 inline std::string name() override { return "EvolveBCX1";}
816
817 protected:
818 inline bool stop_condition(Binstar& binstar) override {
819 bool BH_cond = binstar.getstar(0)->amiBH() and binstar.getstar(0)->getp(Mass::ID)>=14 and binstar.getstar(0)->getp(Mass::ID)<=24;
820 bool MS_cond = binstar.getstar(1)->getp(Phase::ID)==1 and binstar.getstar(1)->getp(Mass::ID)>=30 and binstar.getstar(1)->getp(Mass::ID)<=50;
821 bool Semimajor_cond = binstar.getp(Semimajor::ID)<=1000000000000.0;
822
823 return BH_cond and MS_cond and Semimajor_cond;
824 }
825
826 };
827
840 public:
841 explicit EvolveBHMSTarantula(SevnLogging& svlog, bool record_state=true, bool include_failed=false)
842 : EvolveStopCondition(svlog,record_state,include_failed) {};
843 inline int operator() (_UNUSED Star& star) override {
844 _svlog.critical("Evolve for star is not implemented for functor EvolveBinaryCompact",__FILE__,__LINE__,
846 return -1;
847 }
848
849 inline std::string name() override { return "EvolveBHMSTarantula";}
850
851 protected:
852 inline bool stop_condition(Binstar& binstar) override {
853
854 constexpr double yr_to_day = 265.24;
855
856 bool BH_cond = binstar.getstar(0)->amiBH() and binstar.getstar(0)->getp(Mass::ID)>=6 and binstar.getstar(0)->getp(Mass::ID)<=16;
857 bool MS_cond = binstar.getstar(1)->getp(Phase::ID)==1 and binstar.getstar(1)->getp(Mass::ID)>=15 and binstar.getstar(1)->getp(Mass::ID)<=32;
858 bool P_cond = binstar.getp(Period::ID)*yr_to_day < 100;
859 bool e_cond = binstar.getp(Eccentricity::ID) < 0.3;
860
861 return BH_cond and MS_cond and P_cond and e_cond;
862 }
863
864 };
865
881 public:
882 explicit EvolveBHMS(SevnLogging& svlog, bool record_state=true, bool include_failed=false)
883 : EvolveRecordCondition(svlog,record_state,include_failed) {};
884 inline int operator() (_UNUSED Star& star) override {
885 _svlog.critical("Evolve for star is not implemented for functor EvolveBinaryCompact",__FILE__,__LINE__,
887 return -1;
888 }
889
890 inline std::string name() override { return "EvolveBHMSTarantula";}
891
892 protected:
893 inline bool record_condition(Binstar& binstar) override {
894 bool cond_1 = binstar.getstar(0)->amiBH() and (binstar.getstar(1)->getp(Phase::ID)<7);
895 bool cond_2 = binstar.getstar(1)->amiBH() and (binstar.getstar(0)->getp(Phase::ID)<7);
896 bool binary_cond = !binstar.broken;
897
898 return (cond_1 or cond_2) and binary_cond;
899 }
900
901 };
902
903
919
920 public:
921 explicit EvolveW1(SevnLogging& svlog, bool record_state=true, bool include_failed=false)
922 : EvolveRecordCondition(svlog,record_state,include_failed) {};
923 inline int operator() (_UNUSED Star& star) override {
924 _svlog.critical("Evolve for star is not implemented for functor EvolveBinaryCompact",__FILE__,__LINE__,
926 return -1;
927 }
928
929 inline std::string name() override { return "EvolveW1";}
930
931 protected:
932 inline bool record_condition(Binstar& binstar) override {
933 bool BHNS_cond = (binstar.getstar(0)->amiCompact() and !binstar.getstar(1)->amiCompact()) or (binstar.getstar(1)->amiCompact() and !binstar.getstar(0)->amiCompact());
934 bool Period_cond = !binstar.broken and binstar.getp(Period::ID)>=Pmin and binstar.getp(Period::ID)<=Pmax;
935
936 return BHNS_cond and Period_cond;
937 }
938 private:
939 static constexpr double hours_to_yr = 1.0/8766.0; //1 hours to year, considering 1 year=365.25 days
940 static constexpr double Pmin = 1*hours_to_yr; //Minimum Period 1h
941 static constexpr double Pmax = 5*hours_to_yr; //Maximum Period 5h
942
943 };
944
945
957
958 public:
959 explicit EvolveRRL(SevnLogging& svlog, bool record_state=true, bool include_failed=false)
960 : EvolveRecordCondition(svlog,record_state,include_failed) {};
961 inline std::string name() override { return "EvolveRRL";}
962
963 protected:
964 inline bool record_condition(Binstar& binstar) override {
965 return binstar.getstar(0)->amiRRL() or binstar.getstar(1)->amiRRL();
966 }
967 inline bool record_condition(Star& star) override {
968 return star.amiRRL() or star.amiRRL();
969 }
970 };
971
972
981 inline int evolve_single(Binstar& binary, SevnLogging& svlog, bool record_state=true){
982
983 if (record_state) binary.recordstate(); //always record the initial state
984 for(;;) {
985
986 try {
987 EvolveFunctor::check_stalling(binary); //Check stalling
988 binary.evolve();
989
990
991 if (binary.breaktrigger()) //stopping condition for evolving the star
992 break; //go to evolve the next star
993
994 if ( (binary.isoutputtime() or binary.printall()) and record_state ) {
996 }
997 }
998 catch(sevnstd::sevnerr& e){
999
1000
1001 svlog.error("Evolution of binary "+binary.get_name() + "(ID "+ utilities::n2s(binary.get_ID(),__FILE__,__LINE__)+
1002 ") broken by an error: "+e.what(),__FILE__,__LINE__,false);
1003
1004 //std::cerr<< binaries[i].get_name() <<" " <<e.what()<<std::endl << std::flush;
1005 return EXIT_FAILURE;
1006 }
1007
1008 //utilities::wait();
1009
1010
1011 }
1012 if (record_state) {
1013 binary.recordstate(); //always record the final state
1014 binary.print(); //print out all the recorded states for the star
1015 }
1016 /********************************/
1017
1018 return EXIT_SUCCESS;
1019 }
1020
1029 inline int evolve_single(Star& star, SevnLogging& svlog, bool record_state=true){
1030
1031 if (record_state) star.recordstate(); //always record the initial state
1032 for(;;) {
1033
1034 try {
1035
1036 EvolveFunctor::check_stalling(star); //Check stalling
1037 star.evolve();
1038
1039
1040 if (star.breaktrigger()) //stopping condition for evolving the star
1041 break; //go to evolve the next star
1042
1043 if ( (star.isoutputtime() or star.printall()) and record_state ) {
1044
1046 }
1047 }
1048 catch(sevnstd::sevnerr& e){
1049
1050 svlog.error("Evolution of binary "+star.get_name() + "(ID "+ utilities::n2s(star.get_ID(),__FILE__,__LINE__)+
1051 ") broken by an error: "+e.what(),__FILE__,__LINE__,false);
1052
1053 return EXIT_FAILURE;
1054 }
1055
1056 //utilities::wait();
1057
1058
1059 }
1060 if (record_state) {
1061 star.recordstate(); //always record the final state
1062 star.print(); //print out all the recorded states for the star
1063 }
1064 /********************************/
1065
1066 return EXIT_SUCCESS;
1067 }
1068
1069
1079 template <typename System>
1080 inline int evolve_list(EvolveFunctor* evolve_function, std::vector<System>& systems, _UNUSED IO& sevnio, int Nevolve=-1){
1081 SevnLogging svlog;
1082
1083 if (Nevolve==-1) Nevolve=systems.size();
1084 unsigned Nfailed =0;
1085
1086#ifdef _OPENMP
1087#pragma omp parallel num_threads(sevnio.nthreads)
1088#endif
1089 {
1090
1091#ifdef _OPENMP
1092#pragma omp for schedule(static) reduction(+: Nfailed)
1093#endif
1094 for (size_t i = 0; i < (size_t)Nevolve; i++) {
1095
1096 //NOTICE: THE FOLLOWING IS EXTREMELY IMPORTANT
1097 //mtrand is a static threadlocal variaible, i.e. it is a global variable for each thread
1098 //it is "seeded" each time we initiliase a system (binary or star), but we initiliase them
1099 //before to evolve them, therefore is you not "re-seed" mtrand before the evolution
1100 //mtrand will be seeded with the last loaded system and this will make impossibile to reproduce our results
1101 //unless we use exactly the same list of systems (at the same relative position in the input file)
1102 //Notice also that it is important that mtrand IS NEVER RE-SEEDED during the stellar/binary evolution
1103 //(for this reason when we create an auxiliary star we save the current mtrand status, then we initiliase the
1104 //auxiliary star where mtrand is reseed and finally we restore the old mtrand)
1105 //TODO We should surely improve this behaviour (see issue 71 on gitlab: https://gitlab.com/sevn/SEVN/-/issues/71 )
1106 utilities::mtrand.seed(systems[i].get_rseed());//Set random state for riproducibility, DO NOT MOVE OR REMOVE THIS LINE
1107
1108
1109 /****** EVOLVE ******/
1110 if((*evolve_function)(systems[i])==EXIT_FAILURE)
1111 Nfailed++;
1112
1113
1114 }
1115
1116
1117 }
1118
1119 return Nfailed;
1120 }
1121
1122
1134 //[[deprecated("Replaced by functor evolve implementation")]] This attribute can be used only from C++14
1135 template<typename T>
1136 inline int chunk_dispatcher(unsigned int Nchunk, IO& sevnio, std::vector<T>& systems, bool record_state=true, bool progress=true){
1137
1139 //If systems is not an empty vector clear it
1140 if (!systems.empty())
1141 systems.clear();
1142
1144 //Max size
1145 unsigned int Ntot = sevnio.STARS_MATRIX.size();
1146 //Reset Nchunk if needed
1147 Nchunk = std::min(Nchunk,Ntot);
1148 //Reserve space for systems
1149 systems.reserve(Nchunk);
1150
1151
1153 if (progress)
1154 std::cout<<"Evolving systems:"<<std::endl;
1155
1156 unsigned int Ndone=0;
1157 size_t current_idx=0;
1158 int Ntodo=0;
1159 int Nfailed=0;
1160 while (Ndone<Ntot){
1161
1162
1163 //Assign Ntodo
1164 Ntodo = std::min(Ntot-Ndone, Nchunk);
1165
1166 //Fill vector
1167 for (size_t i = 0; i < Ntodo; i++){
1168 systems.emplace_back(&sevnio, sevnio.STARS_MATRIX[current_idx], current_idx);
1169 current_idx++;
1170 }
1171
1172 //MEMORY DEBUG
1173 //utilities::hardwait("After filling systems",__FILE__,__LINE__);
1174
1175 //Evolve and update Nfailed
1176 Nfailed+=evolve_list(systems,sevnio,Ntodo,record_state);
1177
1178 //MEMORY DEBUG
1179 //utilities::hardwait("After evolving systems",__FILE__,__LINE__);
1180
1181 //Clear
1182 systems.clear();
1183
1184 //MEMORY DEBUG
1185 //utilities::hardwait("After clearing systems",__FILE__,__LINE__);
1186
1187 //Update Ndone
1188 Ndone+=Ntodo;
1189
1190 //TODO 1: Here we can estimate time needed to perform a chunk run and estimate the time to the end
1191 //TODO 2: Maybe Nfailed is not needed because svlog has already static counters to warning, error and critical messages
1192 if (progress){
1193 std::cout<<"\r"<<Ndone<<"/"<<Ntot<<" (Nfailed:"<<Nfailed<<")";
1194 std::cout<<std::flush;
1195 }
1196
1197
1198
1199 }
1200
1201 if (progress)
1202 std::cout<<std::endl;
1203
1204 return EXIT_SUCCESS;
1205 }
1206
1218 template<typename T>
1219 inline int chunk_dispatcher(EvolveFunctor* evolve_function,unsigned int Nchunk, IO& sevnio, std::vector<T>& systems, bool progress=true){
1220
1222 //If systems is not an empty vector clear it
1223 if (!systems.empty())
1224 systems.clear();
1225
1226 SevnLogging sevnlog;
1227
1229 //Max size
1230 unsigned int Ntot = sevnio.STARS_MATRIX.size();
1231 //Reset Nchunk if needed
1232 Nchunk = std::min(Nchunk,Ntot);
1233 //Reserve space for systems
1234 systems.reserve(Nchunk);
1235
1237 if (progress)
1238 std::cout<<"Evolving systems:"<<std::endl;
1239
1240 unsigned int Ndone=0;
1241 size_t current_idx=0;
1242 int Ntodo=0;
1243 int Nfailed=0;
1244 while (Ndone<Ntot){
1245
1246
1247 //Assign Ntodo
1248 Ntodo = std::min(Ntot-Ndone, Nchunk);
1249
1250 //Fill vector
1251 for (size_t i = 0; i < (size_t)Ntodo; i++){
1252
1253 try{
1254 systems.emplace_back(&sevnio, sevnio.STARS_MATRIX[current_idx], current_idx);
1255 }
1256 catch(sevnstd::sevnio_error& e){ //sevnio error contains initialisation errors
1257 sevnio.print_failed_initilisation_summary(current_idx);
1258 sevnlog.error("Failed initilisation for System with ID="+utilities::n2s(current_idx,__FILE__,__LINE__)+
1259 " with message:\n"+e.what(),__FILE__,__LINE__,sevnio.svpar.get_bool("initerror_stop"));
1260 }
1261 current_idx++;
1262
1263
1264 }
1265
1266 //MEMORY DEBUG
1267 //utilities::hardwait("After filling systems",__FILE__,__LINE__);
1268 //Evolve and update Nfailed
1269 //Nfailed+=evolve_list(evolve_function,systems,sevnio,Ntodo);
1270 //We use system.size instead of Ntodo because wome of the systema could be not initliased due to initilisation errors
1271 Nfailed+=evolve_list(evolve_function,systems,sevnio,systems.size());
1272 //MEMORY DEBUG
1273 //utilities::hardwait("After evolving systems",__FILE__,__LINE__);
1274 //Clear
1275 systems.clear();
1276
1277 //MEMORY DEBUG
1278 //utilities::hardwait("After clearing systems",__FILE__,__LINE__);
1279
1280 //Update Ndone
1281 Ndone+=Ntodo;
1282
1283 //TODO 1: Here we can estimate time needed to perform a chunk run and estimate the time to the end
1284 //TODO 2: Maybe Nfailed is not needed because svlog has already static counters to warning, error and critical messages
1285 if (progress){
1286 std::cout<<"\r"<<Ndone<<"/"<<Ntot<<" (Nfailed:"<<Nfailed<<")";
1287 std::cout<<std::flush;
1288 }
1289
1290
1291
1292 }
1293
1294 if (progress)
1295 std::cout<<std::endl;
1296
1297 return EXIT_SUCCESS;
1298 }
1299
1300
1301
1302}
1303
1304
1305#endif //SEVN_EVOLVE_H
#define _UNUSED
Definition: BinaryProperty.h:20
Definition: binstar.h:26
bool broken
MEMBERS///.
Definition: binstar.h:30
std::string get_name() const
Definition: binstar.h:144
size_t get_ID() const
Definition: binstar.h:145
bool breaktrigger() const
Definition: binstar.h:444
virtual void evolve()
Definition: binstar.h:222
bool isoutputtime()
Outputs.
Definition: binstar.cpp:1030
void print()
Definition: binstar.h:557
bool printall()
Definition: binstar.h:549
void recordstate_w_timeupdate()
Definition: binstar.h:378
double getp(const size_t &id) const
METHODS///.
Definition: binstar.h:142
void recordstate()
Definition: binstar.h:390
Star * getstar(const size_t &id)
Definition: binstar.h:146
static size_t ID
Definition: BinaryProperty.h:110
Definition: IO.h:53
std::vector< std::vector< std::string > > STARS_MATRIX
Definition: IO.h:160
SEVNpar svpar
Definition: IO.h:198
void print_failed_initilisation_summary(const size_t &_ID)
Definition: IO.cpp:414
static size_t ID
Definition: property.h:785
static size_t ID
Definition: BinaryProperty.h:315
static size_t ID
Definition: property.h:890
bool get_bool(std::string name)
Definition: params.h:158
static size_t ID
Definition: BinaryProperty.h:148
Definition: star.h:39
bool amiBH() const
Definition: star.h:728
std::string & get_name()
Definition: star.h:990
bool isoutputtime()
Definition: star.h:450
size_t get_ID() const
Definition: star.h:989
bool amiCompact() const
Definition: star.h:744
void recordstate()
Definition: star.h:526
bool amiRRL(double Lmin=1.3, double Lmax=1.9, bool just_He_core=false) const
Definition: star.h:815
bool printall()
Definition: star.h:459
void recordstate_w_timeupdate()
Definition: star.h:550
utilities::evolution evolve()
Definition: star.cpp:1075
void print()
Definition: star.h:576
bool breaktrigger() const
Definition: star.h:847
double getp(const size_t &id) const
Definition: star.h:924
Definition: evolve.h:685
EvolveBBH(SevnLogging &svlog, bool record_state=true, bool include_failed=false)
Definition: evolve.h:687
int operator()(_UNUSED Star &star) override
Definition: evolve.h:689
bool stop_condition(Binstar &binstar) override
Definition: evolve.h:698
std::string name() override
Definition: evolve.h:695
Definition: evolve.h:805
EvolveBCX1(SevnLogging &svlog, bool record_state=true, bool include_failed=false)
Definition: evolve.h:807
std::string name() override
Definition: evolve.h:815
int operator()(_UNUSED Star &star) override
Definition: evolve.h:809
bool stop_condition(Binstar &binstar) override
Definition: evolve.h:818
Definition: evolve.h:880
std::string name() override
Definition: evolve.h:890
int operator()(_UNUSED Star &star) override
Definition: evolve.h:884
bool record_condition(Binstar &binstar) override
Definition: evolve.h:893
EvolveBHMS(SevnLogging &svlog, bool record_state=true, bool include_failed=false)
Definition: evolve.h:882
Definition: evolve.h:839
EvolveBHMSTarantula(SevnLogging &svlog, bool record_state=true, bool include_failed=false)
Definition: evolve.h:841
bool stop_condition(Binstar &binstar) override
Definition: evolve.h:852
int operator()(_UNUSED Star &star) override
Definition: evolve.h:843
std::string name() override
Definition: evolve.h:849
Definition: evolve.h:718
std::string name() override
Definition: evolve.h:786
EvolveBLC(SevnLogging &svlog, bool record_state=true, bool include_failed=false)
Definition: evolve.h:720
int operator()(Binstar &binary) override
Definition: evolve.h:722
Definition: evolve.h:588
std::string name() override
Definition: evolve.h:598
bool stop_condition(Binstar &binstar) override
Definition: evolve.h:601
int operator()(_UNUSED Star &star) override
Definition: evolve.h:592
EvolveBinaryCompact(SevnLogging &svlog, bool record_state=true, bool include_failed=false)
Definition: evolve.h:590
std::string name() override
Definition: evolve.h:669
EvolveBinaryCompactOld(SevnLogging &svlog, bool record_state=true, bool include_failed=false)
Definition: evolve.h:621
int operator()(Binstar &binary) override
Definition: evolve.h:623
Definition: evolve.h:264
int evolve_debug(System &system)
Definition: evolve.h:278
int operator()(Binstar &binary) override
Definition: evolve.h:268
EvolveDebug(SevnLogging &svlog, bool record_state=true, bool include_failed=false)
Definition: evolve.h:266
std::string name() override
Definition: evolve.h:274
Definition: evolve.h:300
std::string name() override
Definition: evolve.h:310
int evolve_default(System &system)
Definition: evolve.h:314
EvolveDefault(SevnLogging &svlog, bool record_state=true, bool include_failed=false)
Definition: evolve.h:302
int operator()(Binstar &binary) override
Definition: evolve.h:304
Definition: evolve.h:161
virtual ~EvolveFunctor()=default
virtual int operator()(_UNUSED Binstar &binary)
Definition: evolve.h:166
virtual std::string name()
Definition: evolve.h:168
const bool _record_state
Definition: evolve.h:216
SevnLogging & _svlog
Definition: evolve.h:215
EvolveFunctor(SevnLogging &svlog, bool record_state=true, bool include_failed=false, Options *options=nullptr)
Definition: evolve.h:163
static void catched_error_message(System &system, const sevnstd::sevnerr &e)
Definition: evolve.h:238
const Options * _options
Definition: evolve.h:218
static long elapsed_time(std::chrono::steady_clock::time_point start_clock)
Definition: evolve.h:250
static bool check_stalling(System &system)
Definition: evolve.h:182
void final_print(System &system, bool evolution_result)
Definition: evolve.h:221
const bool _include_failed
Definition: evolve.h:217
Definition: evolve.h:956
EvolveRRL(SevnLogging &svlog, bool record_state=true, bool include_failed=false)
Definition: evolve.h:959
bool record_condition(Binstar &binstar) override
Definition: evolve.h:964
std::string name() override
Definition: evolve.h:961
bool record_condition(Star &star) override
Definition: evolve.h:967
virtual bool record_condition(_UNUSED Star &star)
Definition: evolve.h:571
EvolveRecordCondition(SevnLogging &svlog, bool record_state=true, bool include_failed=false)
Definition: evolve.h:464
virtual bool record_condition(_UNUSED Binstar &binstar)
Definition: evolve.h:567
virtual int operator()(Binstar &binary) override
Definition: evolve.h:466
std::string name() override
Definition: evolve.h:563
Definition: evolve.h:363
virtual bool stop_condition(_UNUSED Star &star)
Definition: evolve.h:445
virtual bool stop_condition(_UNUSED Binstar &binstar)
Definition: evolve.h:441
std::string name() override
Definition: evolve.h:439
virtual int operator()(Binstar &binary) override
Definition: evolve.h:367
EvolveStopCondition(SevnLogging &svlog, bool record_state=true, bool include_failed=false)
Definition: evolve.h:365
Definition: evolve.h:918
static constexpr double Pmin
Definition: evolve.h:940
EvolveW1(SevnLogging &svlog, bool record_state=true, bool include_failed=false)
Definition: evolve.h:921
std::string name() override
Definition: evolve.h:929
static constexpr double Pmax
Definition: evolve.h:941
bool record_condition(Binstar &binstar) override
Definition: evolve.h:932
int operator()(_UNUSED Star &star) override
Definition: evolve.h:923
static constexpr double hours_to_yr
Definition: evolve.h:939
Definition: sevnlog.h:43
void error(std::string errstate, const char *file_input=nullptr, int line_input=-1, bool stop=true) const
Definition: sevnlog.cpp:105
void critical(std::string errstate, const char *file_input=nullptr, int line_input=-1) const
Definition: sevnlog.cpp:85
Definition: errhand.h:156
Definition: errhand.h:24
const char * what() const
Definition: errhand.h:36
Definition: errhand.h:53
Definition: evolve.h:138
int evolve_single(Binstar &binary, SevnLogging &svlog, bool record_state=true)
Definition: evolve.h:981
int chunk_dispatcher(unsigned int Nchunk, IO &sevnio, std::vector< T > &systems, bool record_state=true, bool progress=true)
Definition: evolve.h:1136
int evolve_list(EvolveFunctor *evolve_function, std::vector< System > &systems, _UNUSED IO &sevnio, int Nevolve=-1)
Definition: evolve.h:1080
const std::string n2s(T val, const char *file_input, const int line_input, const unsigned int precision=6)
Definition: utilities.h:144
std::mt19937_64 mtrand
Definition: utilities.cpp:9
Definition: evolve.h:146
std::vector< std::string > str_parameters
Definition: evolve.h:148
std::vector< double > num_parameters
Definition: evolve.h:147