Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:58:04

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 // G4ConvergenceTester
0027 //
0028 // Class description:
0029 //
0030 // Convergence Tests for Monte Carlo results.
0031 //
0032 // Reference
0033 // MCNP(TM) -A General Monte Carlo N-Particle Transport Code
0034 // Version 4B
0035 // Judith F. Briesmeister, Editor
0036 // LA-12625-M, Issued: March 1997, UC 705 and UC 700
0037 // CHAPTER 2. GEOMETRY, DATA, PHYSICS, AND MATHEMATICS
0038 //        VI. ESTIMATION OF THE MONTE CARLO PRECISION
0039 //
0040 // Positive numbers are assumed for input values
0041 
0042 // Author: Tatsumi Koi (SLAC/SCCS)
0043 // --------------------------------------------------------------------
0044 #ifndef G4ConvergenceTester_hh
0045 #define G4ConvergenceTester_hh 1
0046 
0047 #include "G4SimplexDownhill.hh"
0048 #include "G4Timer.hh"
0049 #include "globals.hh"
0050 
0051 #include <map>
0052 #include <vector>
0053 
0054 class G4ConvergenceTester
0055 {
0056   public:
0057 
0058     G4ConvergenceTester(const G4String& theName = "NONAME");
0059     ~G4ConvergenceTester();
0060     G4ConvergenceTester(G4double);
0061 
0062     void AddScore(G4double);
0063 
0064     inline G4ConvergenceTester& operator+=(G4double val)
0065     {
0066       this->AddScore(val);
0067       return *this;
0068     }
0069 
0070     void ShowHistory(std::ostream& out = G4cout);
0071     void ShowResult(std::ostream& out = G4cout);
0072       // Default to G4cout but can be redirected to another ostream
0073 
0074     inline G4double GetValueOfMinimizingFunction(std::vector<G4double> x)
0075     {
0076       return slope_fitting_function(x);
0077     }
0078 
0079     void ComputeStatistics() { calStat(); }
0080       // Explicitly calculate statistics
0081 
0082     // All accessors check to make sure value is current before returning
0083 
0084     inline G4double GetMean() { CheckIsUpdated(); return mean; }
0085     inline G4double GetStandardDeviation() { CheckIsUpdated(); return sd; }
0086     inline G4double GetVariance() { CheckIsUpdated(); return var; }
0087     inline G4double GetR() { CheckIsUpdated(); return r; }
0088     inline G4double GetEfficiency() { CheckIsUpdated(); return efficiency; }
0089     inline G4double GetR2eff() { CheckIsUpdated(); return r2eff; }
0090     inline G4double GetR2int() { CheckIsUpdated(); return r2int; }
0091     inline G4double GetShift() { CheckIsUpdated(); return shift; }
0092     inline G4double GetVOV() { CheckIsUpdated(); return vov; }
0093     inline G4double GetFOM() { CheckIsUpdated(); return fom; }
0094 
0095   private:
0096 
0097     void calStat();
0098       // Boolean value of 'statsAreUpdated' is set to TRUE at end of calStat
0099       // and set to FALSE at end of AddScore().
0100       // NOTE: A thread lock needs to be put in AddScore() so calStat()
0101       // is not executed in one thread while AddScore() is adding data
0102 
0103     inline void CheckIsUpdated()
0104     {
0105       if(!statsAreUpdated) { calStat(); }
0106     }
0107 
0108     void calc_grid_point_of_history();
0109     void calc_stat_history();
0110     void check_stat_history(std::ostream& out = G4cout);
0111     G4double calc_Pearson_r(G4int,std::vector<G4double>,std::vector<G4double>);
0112     G4bool is_monotonically_decrease(const std::vector<G4double>&);
0113     void calc_slope_fit(const std::vector<G4double>&);
0114     G4double slope_fitting_function(std::vector<G4double>);
0115 
0116   private:
0117 
0118     G4String name;
0119     std::map<G4int, G4double> nonzero_histories;  // (ith-history, score value)
0120     G4int n = 0;        // number of history
0121     G4double sum = 0.0; // sum of scores;
0122 
0123     G4Timer* timer = nullptr;
0124     std::vector<G4double> cpu_time;
0125 
0126     G4double mean       = 0.0;
0127     G4double var        = 0.0;
0128     G4double sd         = 0.0;
0129     G4double r          = 0.0;  // relative err sd/mean/sqrt(n)
0130     G4double efficiency = 0.0;  // rate of non zero score
0131     G4double r2eff      = 0.0;
0132     G4double r2int      = 0.0;
0133     G4double shift      = 0.0;
0134     G4double vov        = 0.0;
0135     G4double fom        = 0.0;
0136 
0137     G4double largest             = 0.0;
0138     G4int largest_score_happened = 0;
0139 
0140     G4double mean_1  = 0.0;
0141     G4double var_1   = 0.0;
0142     G4double sd_1    = 0.0;
0143     G4double r_1     = 0.0;  // relative err sd/mean/sqrt(n)
0144     G4double shift_1 = 0.0;
0145     G4double vov_1   = 0.0;
0146     G4double fom_1   = 0.0;
0147 
0148     G4int noBinOfHistory = 16;
0149     std::vector<G4int> history_grid;
0150     std::vector<G4double> mean_history;
0151     std::vector<G4double> var_history;
0152     std::vector<G4double> sd_history;
0153     std::vector<G4double> r_history;
0154     std::vector<G4double> vov_history;
0155     std::vector<G4double> fom_history;
0156     std::vector<G4double> shift_history;
0157     std::vector<G4double> e_history;
0158     std::vector<G4double> r2eff_history;
0159     std::vector<G4double> r2int_history;
0160 
0161     G4double slope = 0.0;
0162     std::vector<G4double> largest_scores;
0163     std::vector<G4double> f_xi;
0164     std::vector<G4double> f_yi;
0165     G4int noBinOfPDF = 10;
0166     G4SimplexDownhill<G4ConvergenceTester>* minimizer = nullptr;
0167 
0168     G4int noPass  = 0;
0169     G4int noTotal = 8;  // Total number of tests
0170 
0171     G4bool statsAreUpdated = true;
0172     G4bool showHistory     = true;
0173     G4bool calcSLOPE       = true;
0174 };
0175 
0176 #endif