Back to home page

EIC code displayed by LXR

 
 

    


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

0001 #pragma once
0002 /**
0003 U4MaterialPropertyVector.h
0004 ============================
0005 
0006 NB there is also a sysrap clone of this  S4MaterialPropertyVector.h
0007 that is convenient to allow only depending on sysrap rather than u4 
0008 
0009 TODO: migrate to using this clone and remove X4MaterialPropertyVector.h 
0010 
0011 After X4MaterialPropertyVector.hh
0012 
0013 **/
0014 
0015 
0016 #include <csignal>
0017 #include "G4MaterialPropertyVector.hh"
0018 #include "G4SystemOfUnits.hh"
0019 
0020 #include "NP.hh"
0021 #include "NPFold.h"
0022 
0023 struct U4MaterialPropertyVector
0024 {
0025     static NP* ConvertToArray(const G4MaterialPropertyVector* vec);
0026     static G4MaterialPropertyVector* FromArray(const NP* prop);
0027     static G4MaterialPropertyVector* Make_V(double value); 
0028     static std::string Desc_V(const G4MaterialPropertyVector* v); 
0029 
0030     static void    Import_MSV(          std::map<std::string,G4MaterialPropertyVector*>& msv, const NPFold* sub);
0031     static NPFold* Serialize_MSV( const std::map<std::string,G4MaterialPropertyVector*>& msv );
0032     static std::string Desc_MSV(  const std::map<std::string,G4MaterialPropertyVector*>& msv ) ;
0033 
0034     static void Import_MIMSV(            std::map<int,std::map<std::string,G4MaterialPropertyVector*>>& mimsv, const NPFold* f );
0035     static NPFold* Serialize_MIMSV(const std::map<int,std::map<std::string,G4MaterialPropertyVector*>>& mimsv ) ;
0036     static std::string Desc_MIMSV( const std::map<int,std::map<std::string,G4MaterialPropertyVector*>>& mimsv ) ;
0037 };
0038 
0039 
0040 inline NP* U4MaterialPropertyVector::ConvertToArray(const G4MaterialPropertyVector* prop)
0041 {
0042     size_t num_val = prop->GetVectorLength() ; 
0043     NP* a = NP::Make<double>( num_val, 2 );  
0044     double* a_v = a->values<double>(); 
0045     for(size_t i=0 ; i < num_val ; i++)
0046     {   
0047         G4double energy = prop->Energy(i); 
0048         G4double value = (*prop)[i] ;
0049         a_v[2*i+0] = energy ; 
0050         a_v[2*i+1] = value ; 
0051     }   
0052     return a ;   
0053 }
0054 
0055 
0056 inline G4MaterialPropertyVector* U4MaterialPropertyVector::FromArray(const NP* a ) // static 
0057 {   
0058     assert( a->uifc == 'f' && a->ebyte == 8 );
0059     
0060     size_t ni = a->shape[0] ;
0061     size_t nj = a->shape[1] ;
0062     bool nj_expect = nj == 2 ; 
0063     assert( nj_expect );
0064     if(!nj_expect) std::raise(SIGINT); 
0065     
0066     G4double* energy = new G4double[ni] ;
0067     G4double* value = new G4double[ni] ;
0068     
0069     for(int i=0 ; i < int(ni) ; i++)
0070     {   
0071         energy[i] = a->get<double>(i,0) ;
0072         value[i] = a->get<double>(i,1) ;
0073     }
0074     G4MaterialPropertyVector* vec = new G4MaterialPropertyVector(energy, value, ni);
0075     return vec ;
0076 }
0077 
0078 
0079 inline G4MaterialPropertyVector* U4MaterialPropertyVector::Make_V(double value) // static
0080 {
0081     int n = 2 ;
0082     G4double* e = new G4double[n] ;
0083     G4double* v = new G4double[n] ;
0084 
0085     e[0] = 1.55*eV ;
0086     e[1] = 15.5*eV ;
0087 
0088     v[0] = value ;
0089     v[1] = value ;
0090 
0091     G4MaterialPropertyVector* mpt = new G4MaterialPropertyVector(e, v, n);
0092     return mpt ;
0093 }
0094 
0095 inline std::string U4MaterialPropertyVector::Desc_V(const G4MaterialPropertyVector* v)
0096 {
0097     size_t len = v->GetVectorLength() ;  
0098     std::stringstream ss ; 
0099     ss << " Desc_V" 
0100        << " len " << len
0101        << std::endl 
0102        ; 
0103     std::string s = ss.str(); 
0104     return s ; 
0105 }
0106 
0107 
0108 inline void U4MaterialPropertyVector::Import_MSV( std::map<std::string, G4MaterialPropertyVector*>& msv, const NPFold* sub)  // static
0109 {
0110     int num_sub = sub->get_num_subfold();
0111     unsigned num_items = sub->num_items();
0112     bool num_sub_expect = num_sub == 0  ;
0113     assert( num_sub_expect );
0114     if(!num_sub_expect) std::raise(SIGINT); 
0115 
0116     for(unsigned idx=0 ; idx < num_items ; idx++)
0117     {
0118         const char* key_ = sub->get_key(idx);
0119         const char* key = NPFold::BareKey(key_);
0120         const NP* a = sub->get_array(idx);
0121         G4MaterialPropertyVector* mpv = FromArray(a) ;
0122         msv[key] = mpv ;
0123     }
0124 }
0125 
0126 inline NPFold* U4MaterialPropertyVector::Serialize_MSV( const std::map<std::string, G4MaterialPropertyVector*>& msv ) // static
0127 {
0128     NPFold* f = new NPFold ; 
0129     typedef std::map<std::string, G4MaterialPropertyVector*> MSV ; 
0130     MSV::const_iterator it = msv.begin(); 
0131 
0132     for(unsigned i=0 ; i < msv.size() ; i++)
0133     { 
0134         const std::string& k = it->first ; 
0135         const G4MaterialPropertyVector* v = it->second ; 
0136         NP* a = ConvertToArray( v ); 
0137         f->add( k.c_str(), a );     
0138         std::advance(it, 1);  
0139     }
0140     return f ; 
0141 } 
0142 
0143 inline std::string U4MaterialPropertyVector::Desc_MSV(const std::map<std::string, G4MaterialPropertyVector*>& msv ) 
0144 {
0145     typedef std::map<std::string, G4MaterialPropertyVector*> MSV ; 
0146     MSV::const_iterator it = msv.begin(); 
0147     std::stringstream ss ; 
0148     ss << "U4MaterialPropertyVector::Desc_MSV" << std::endl ; 
0149 
0150     for(unsigned i=0 ; i < msv.size() ; i++)
0151     { 
0152         const std::string& key = it->first ; 
0153         const G4MaterialPropertyVector* v = it->second ; 
0154         ss
0155            << " key " << key 
0156            << Desc_V(v) 
0157            << std::endl
0158            ;  
0159         std::advance(it, 1);  
0160     }
0161     std::string s = ss.str(); 
0162     return s ; 
0163 }
0164 
0165 
0166 
0167 
0168 inline void U4MaterialPropertyVector::Import_MIMSV( std::map<int, std::map<std::string, G4MaterialPropertyVector*>>& mimsv, const NPFold* f )
0169 {
0170     typedef std::map<std::string, G4MaterialPropertyVector*> MSV ; 
0171     int num_sub = f->get_num_subfold();    
0172 
0173     for(int idx=0 ; idx < num_sub ; idx++)
0174     {
0175         const char* cat = f->get_subfold_key(idx); 
0176         int icat = U::To<int>(cat); 
0177 
0178         NPFold* sub = f->get_subfold(idx); 
0179 
0180         MSV& msv = mimsv[icat] ; 
0181 
0182         Import_MSV( msv, sub );         
0183     }
0184 }
0185 
0186 
0187 inline NPFold* U4MaterialPropertyVector::Serialize_MIMSV( const std::map<int, std::map<std::string, G4MaterialPropertyVector*>>& mimsv)  
0188 {
0189     NPFold* f = new NPFold ; 
0190 
0191     typedef std::map<std::string, G4MaterialPropertyVector*> MSV ; 
0192     typedef std::map<int, MSV> MIMSV ; 
0193 
0194     MIMSV::const_iterator it = mimsv.begin(); 
0195 
0196     for(unsigned i=0 ; i < mimsv.size() ; i++)
0197     {
0198         int icat = it->first ; 
0199         const char* cat = U::FormName(icat) ; 
0200 
0201         const MSV& msv = it->second ; 
0202         NPFold* sub = Serialize_MSV( msv ); 
0203 
0204         f->add_subfold(cat, sub);         
0205 
0206         std::advance(it, 1); 
0207     }
0208     return f; 
0209 }
0210 
0211 inline std::string U4MaterialPropertyVector::Desc_MIMSV(const std::map<int,std::map<std::string, G4MaterialPropertyVector*>>& mimsv ) 
0212 {
0213     std::stringstream ss ; 
0214     ss << "U4MaterialPropertyVector::Desc_MIMSV" << std::endl ; 
0215 
0216     typedef std::map<std::string, G4MaterialPropertyVector*> MSV ; 
0217     typedef std::map<int, MSV> MIMSV ; 
0218     MIMSV::const_iterator it = mimsv.begin(); 
0219 
0220     for(unsigned i=0 ; i < mimsv.size() ; i++)
0221     {
0222         int cat = it->first ; 
0223         const MSV& msv = it->second ; 
0224         ss
0225             << " cat " << cat 
0226             << " msv.size " << msv.size()
0227             << std::endl 
0228             << Desc_MSV(msv)
0229             << std::endl 
0230             ;
0231           
0232         std::advance(it, 1); 
0233     }
0234     std::string s = ss.str(); 
0235     return s ; 
0236 }
0237 
0238 
0239