@@ -30,6 +30,30 @@ namespace CTF_int {
30
30
#endif
31
31
}
32
32
33
+ void load_all_models (std::string file_name){
34
+ #ifdef TUNE
35
+ for (int i=0 ; i<get_all_models ().size (); i++){
36
+ get_all_models ()[i]->load_coeff (file_name);
37
+ }
38
+ #endif
39
+ }
40
+
41
+ void write_all_models (std::string file_name){
42
+ #ifdef TUNE
43
+ for (int i=0 ; i<get_all_models ().size (); i++){
44
+ get_all_models ()[i]->write_coeff (file_name);
45
+ }
46
+ #endif
47
+ }
48
+
49
+ void dump_all_models (std::string path){
50
+ #ifdef TUNE
51
+ for (int i=0 ; i<get_all_models ().size (); i++){
52
+ get_all_models ()[i]->dump_data (path);
53
+ }
54
+ #endif
55
+ }
56
+
33
57
34
58
#define SPLINE_CHUNK_SZ = 8
35
59
@@ -463,26 +487,26 @@ namespace CTF_int {
463
487
}
464
488
465
489
template <int nparam>
466
- void LinModel<nparam>::write_coeff(){
490
+ void LinModel<nparam>::write_coeff(std::string file_name ){
467
491
468
492
// Generate the model name
469
- std::string model_name = std::string (name)+ " _init[] " ;
493
+ std::string model_name = std::string (name);
470
494
// Generate the new line in the file
471
- std::string new_coeff_str = " double " + model_name+ " = { " ;
495
+ std::string new_coeff_str = model_name+" " ;
472
496
char buffer[64 ];
473
497
for (int i =0 ; i<nparam; i++){
474
498
buffer[0 ] = ' \0 ' ;
475
499
std::sprintf (buffer," %1.4E" , coeff_guess[i]);
476
500
std::string s (buffer);
477
501
new_coeff_str += s;
478
502
if (i != nparam - 1 ){
479
- new_coeff_str += " , " ;
503
+ new_coeff_str += " " ;
480
504
}
481
505
}
482
- new_coeff_str += " }; " ;
506
+
483
507
// Open the file that stores the model info
484
508
std::vector<std::string> file_content;
485
- std::ifstream infile (" model_init.cxx " );
509
+ std::ifstream infile (file_name );
486
510
if (!infile){
487
511
std::cout<<" Error opening file" <<std::endl;
488
512
return ;
@@ -496,7 +520,7 @@ namespace CTF_int {
496
520
// Get the model name from the line
497
521
std::string s;
498
522
std::getline (f,s,' ' );
499
- std::getline (f,s, ' ' ) ;
523
+ std::cout<<s<< " , " <<model_name<<std::endl ;
500
524
if (s == model_name){
501
525
line = new_coeff_str;
502
526
found_line = true ;
@@ -505,82 +529,93 @@ namespace CTF_int {
505
529
file_content.push_back (line);
506
530
}
507
531
532
+ // Append the string to the file if no match is found
533
+ if (!found_line){
534
+ new_coeff_str += " \n " ;
535
+ file_content.push_back (new_coeff_str);
536
+ }
508
537
std::ofstream ofs;
509
- ofs.open (" model_init.cxx " , std::ofstream::out | std::ofstream::trunc);
538
+ ofs.open (file_name , std::ofstream::out | std::ofstream::trunc);
510
539
for (int i=0 ; i<file_content.size (); i++){
511
540
ofs<<file_content[i];
512
541
}
513
542
ofs.close ();
514
-
515
- // Check if the file is successfully updated
516
- if (!found_line){
517
- std::cout<<" Error! No model declared in model_init.cxx and model_init.h. Please declare the model first!" <<std::endl;
518
- }
519
543
}
520
544
521
545
546
+
522
547
template <int nparam>
523
- void LinModel<nparam>::load_coeff(){
548
+ void LinModel<nparam>::load_coeff(std::string file_name ){
524
549
// Generate the model name
525
- std::string model_name = std::string (name)+ " _init[] " ;
550
+ std::string model_name = std::string (name);
526
551
527
552
// Open the file that stores the model info
528
553
std::vector<std::string> file_content;
529
- std::ifstream infile (" init_models.cxx " );
554
+ std::ifstream infile (file_name );
530
555
if (!infile){
531
556
std::cout<<" Error opening file" <<std::endl;
532
557
return ;
533
558
}
534
559
535
- // Scan the file to find the line and replace with the new model coeffs
536
- std::string line;
560
+ // Flag boolean denotes whether the model is found in the file
537
561
bool found_line = false ;
538
- bool succeed = false ;
562
+ // Flag boolean denotes whether the number of coefficients in the file matches with what the model expects
563
+ bool right_num_coeff = true ;
564
+
565
+ // Scan the file to find the model coefficients
566
+ std::string line;
539
567
while (std::getline (infile,line)){
540
568
std::istringstream f (line);
541
569
// Get the model name from the line
542
570
std::string s;
543
571
std::getline (f,s,' ' );
544
- std::getline (f,s,' ' );
545
572
if (s == model_name){
546
573
found_line = true ;
547
- // Get rid of the '='
548
- std::getline (f,s, ' ' );
549
- // Get the n coeffs
574
+
575
+ // Get the nparam coeffs
576
+ double coeff_from_file [nparam];
550
577
for (int i=0 ; i<nparam; i++){
551
578
if (!std::getline (f,s,' ' )){
552
- break ;
579
+ right_num_coeff = false ;
580
+ break ;
553
581
}
554
- char buffer[64 ];
555
- int index = 0 ;
556
- for (int i = 0 ; i<s.size (); i++){
557
- if (s[i] != ' {' && s[i] != ' }' && s[i] != ' ,' && s[i] != ' ;' ){
558
- buffer[index] = s[i];
559
- index++;
560
- }
582
+
583
+ // Convert the string to char* and update the model coefficients
584
+ char buf[s.length ()+1 ];
585
+ for (int i=0 ;i<s.length ();i++){
586
+ buf[i] = s[i];
561
587
}
562
- buffer[index] = ' \0 ' ;
563
- coeff_guess[i] = std::atof (buffer);
588
+ buf[s.length ()] = ' \0 ' ;
589
+ coeff_guess[i] = std::atof (buf);
590
+ }
591
+ // Check if there are more coefficients in the file
592
+ if (right_num_coeff && std::getline (f,s,' ' )){
593
+ right_num_coeff = false ;
564
594
}
565
- succeed = true ;
566
595
break ;
567
596
}
568
597
}
569
598
// If the model is not found
570
599
if (!found_line){
571
- std::cout<<" Error! Not model found in the file!" <<std::endl;
600
+ std::cout<<" Error! No model found in the file!" <<std::endl;
572
601
}
573
- // If there is not enough parameters
574
- if (!succeed){
575
- std::cout<<" Error! Not enough number of paramters in file!" <<std::endl;
602
+ else if (!right_num_coeff){
603
+ std::cout<<" Error! Number of coefficients in file does not match with the model" <<std::endl;
604
+ // Initialize model coeff to be all 0s
605
+ for (int i = 0 ; i < nparam;i++){
606
+ coeff_guess[i] = 0.0 ;
607
+ }
576
608
}
577
609
}
578
610
611
+
579
612
template <int nparam>
580
- void LinModel<nparam>::dump_data(std::string file_name ){
613
+ void LinModel<nparam>::dump_data(std::string path ){
581
614
// Open the file
615
+ std::string model_name = std::string (name);
582
616
std::ofstream ofs;
583
- ofs.open (" ./data/" +file_name, std::ofstream::out | std::ofstream::trunc);
617
+ ofs.open (" path/" +model_name, std::ofstream::out | std::ofstream::trunc);
618
+
584
619
// Dump the model coeffs
585
620
for (int i=0 ; i<nparam; i++){
586
621
ofs<<coeff_guess[i]<<" " ;
@@ -682,14 +717,20 @@ namespace CTF_int {
682
717
}
683
718
684
719
template <int nparam>
685
- void CubicModel<nparam>::load_coeff(){
686
- lmdl.load_coeff ();
720
+ void CubicModel<nparam>::load_coeff(std::string file_name ){
721
+ lmdl.load_coeff (file_name );
687
722
}
688
723
689
724
template <int nparam>
690
- void CubicModel<nparam>::write_coeff(){
691
- lmdl.write_coeff ();
725
+ void CubicModel<nparam>::write_coeff(std::string file_name ){
726
+ lmdl.write_coeff (file_name );
692
727
}
728
+
729
+ template <int nparam>
730
+ void CubicModel<nparam>::dump_data(std::string path){
731
+ lmdl.dump_data (path);
732
+ }
733
+
693
734
template class CubicModel <1 >;
694
735
template class CubicModel <2 >;
695
736
template class CubicModel <3 >;
0 commit comments