Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-10 07:50:27

0001 /**
0002 U4EngineTest.cc
0003 ==================
0004 
0005 ::
0006 
0007     epsilon:tests blyth$ U4EngineTest 
0008      mode:0 saving engine states 
0009     U4Engine::SaveState /tmp/U4EngineTest/s0.conf
0010      label s0.conf
0011         0 :     0.1305
0012         1 :     0.6178
0013         2 :     0.9959
0014         3 :     0.4959
0015         4 :     0.1129
0016         5 :     0.2899
0017         6 :     0.4730
0018         7 :     0.8376
0019         8 :     0.3594
0020         9 :     0.9269
0021     U4Engine::SaveState /tmp/U4EngineTest/s1.conf
0022      label s1.conf
0023         0 :     0.5480
0024         1 :     0.1372
0025         2 :     0.1190
0026         3 :     0.6559
0027         4 :     0.0721
0028         5 :     0.9804
0029         6 :     0.7405
0030         7 :     0.9630
0031         8 :     0.0069
0032         9 :     0.3705
0033     U4Engine::SaveState /tmp/U4EngineTest/s2.conf
0034      label s2.conf
0035         0 :     0.5728
0036         1 :     0.6244
0037         2 :     0.4719
0038         3 :     0.2626
0039         4 :     0.5345
0040         5 :     0.9096
0041         6 :     0.8778
0042         7 :     0.6699
0043         8 :     0.0987
0044         9 :     0.3317
0045     U4Engine::Desc engine Y engine.name MixMaxRng
0046     epsilon:tests blyth$ 
0047     epsilon:tests blyth$ U4EngineTest 1
0048      mode:1 restore engine state 
0049     U4Engine::RestoreState /tmp/U4EngineTest/s2.conf
0050      label s2.conf Restored
0051         0 :     0.5728
0052         1 :     0.6244
0053         2 :     0.4719
0054         3 :     0.2626
0055         4 :     0.5345
0056         5 :     0.9096
0057         6 :     0.8778
0058         7 :     0.6699
0059         8 :     0.0987
0060         9 :     0.3317
0061     U4Engine::Desc engine Y engine.name MixMaxRng
0062 
0063 
0064 
0065     epsilon:tests blyth$ U4EngineTest 2
0066      mode:2 restore engine state from array 
0067      states (10, 38, )
0068      label U4Engine::RestoreState(states, 2)
0069         0 :     0.5728
0070         1 :     0.6244
0071         2 :     0.4719
0072         3 :     0.2626
0073         4 :     0.5345
0074         5 :     0.9096
0075         6 :     0.8778
0076         7 :     0.6699
0077         8 :     0.0987
0078         9 :     0.3317
0079     U4Engine::Desc engine Y engine.name MixMaxRng
0080 
0081 
0082 
0083 
0084 **/
0085 
0086 
0087 
0088 #include <iostream>
0089 #include "ssys.h"
0090 #include "spath.h"
0091 #include "NP.hh"
0092 #include "U4Engine.h"
0093 #include "G4Types.hh"
0094 #include "U4UniformRand.h"
0095 
0096 #include "Randomize.hh"
0097 
0098 const char* FOLD = spath::Resolve("$TMP/U4EngineTest" ); 
0099 
0100 void dump(int idx, double u)
0101 {
0102     std::cout 
0103         << std::setw(5) << idx 
0104         << " : " 
0105         << std::fixed << std::setw(10) << std::setprecision(4) << u 
0106         << std::endl 
0107         ; 
0108 }
0109 
0110 void dump(const char* label, int n)
0111 {
0112     std::cout << " label " << label << std::endl ; 
0113     for(int i=0 ; i < n ; i++) dump(i, G4UniformRand() ) ; 
0114 }
0115 
0116 void test_SaveState_RestoreState(int argc, char** argv)
0117 {
0118     int mode = argc > 1 ? std::atoi(argv[1]) : 0 ; 
0119     int N = 10 ; 
0120     bool show = false ; 
0121 
0122     if( mode == 0 )
0123     {
0124         NP* states = NP::Make<unsigned long>(10, 2*17+4 ) ; 
0125         std::cout << " mode:0 saving engine states " << std::endl ; 
0126         U4Engine::SaveState(FOLD, "s0.conf") ;   
0127         U4Engine::SaveState( states, 0 ); 
0128         if(show) U4Engine::ShowState(); 
0129         dump("s0.conf", N ); 
0130 
0131         U4Engine::SaveState(FOLD, "s1.conf") ;   
0132         U4Engine::SaveState( states, 1 ); 
0133         if(show) U4Engine::ShowState(); 
0134         dump("s1.conf", N); 
0135 
0136         U4Engine::SaveState(FOLD, "s2.conf") ;   
0137         U4Engine::SaveState( states, 2 ); 
0138         if(show) U4Engine::ShowState(); 
0139         dump("s2.conf", N); 
0140 
0141         states->save(FOLD, "states.npy"); 
0142     }
0143     else if( mode == 1 )
0144     {
0145         std::cout << " mode:1 restore engine state from file " << std::endl ; 
0146         U4Engine::RestoreState(FOLD, "s2.conf") ;   
0147         if(show) U4Engine::ShowState(); 
0148         dump("U4Engine::RestoreState(FOLD, \"s2.conf\")", N ); 
0149     }
0150     else if( mode == 2 )
0151     {
0152         std::cout << " mode:2 restore engine state from array " << std::endl ; 
0153         NP* states = NP::Load(FOLD, "states.npy"); 
0154         std::cout << " states " << states->sstr() << std::endl; 
0155         U4Engine::RestoreState( states, 2 ); 
0156         if(show) U4Engine::ShowState(); 
0157         dump("U4Engine::RestoreState(states, 2)", N ); 
0158     }
0159 
0160     std::cout << U4Engine::Desc() << std::endl ; 
0161 }
0162 
0163 
0164 void test_Desc()
0165 {
0166     std::cout << U4Engine::DescState() << std::endl ; 
0167     std::cout << U4Engine::DescStateArray() << std::endl ; 
0168 }
0169 
0170 void test_U4UniformRand()
0171 {
0172 
0173     NP* states = NP::Make<unsigned long>(10, 2*17+4 ) ; 
0174 
0175     U4Engine::SaveState( states, 0 ); 
0176     NP* u0 = U4UniformRand::Get(100); 
0177 
0178     U4Engine::RestoreState( states, 0 ); 
0179     NP* u1 = U4UniformRand::Get(100); 
0180 
0181     U4Engine::RestoreState( states, 0 ); 
0182     NP* u2 = U4UniformRand::Get(100); 
0183 
0184     std::cout << "u0 " << u0->repr<double>() << std::endl; 
0185     std::cout << "u1 " << u1->repr<double>() << std::endl; 
0186     std::cout << "u2 " << u2->repr<double>() << std::endl; 
0187 
0188     u0->save(FOLD, "test_U4UniformRand", "u0.npy"); 
0189     u1->save(FOLD, "test_U4UniformRand", "u1.npy"); 
0190     u2->save(FOLD, "test_U4UniformRand", "u2.npy"); 
0191 }
0192 
0193 
0194 int main(int argc, char** argv)
0195 {
0196     /*
0197     test_SaveState_RestoreState(argc, argv); 
0198     test_Desc(); 
0199     */
0200 
0201     test_U4UniformRand();     
0202 
0203     return 0 ; 
0204 }