Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:29

0001 #pragma once
0002 /**
0003 S4MaterialPropertyVector.h
0004 ============================
0005 
0006 This provides serialization of int, string keyed maps of
0007 G4MaterialPropertyVector into NPFold as well as the
0008 import of the NPFold back into maps.
0009 
0010 Hence this facilitates the saving and loading of maps of
0011 material properties to/from directory trees using NPFold persistency.
0012 
0013 It simplifies dependencies for this functionality to be available
0014 from sysrap rather than from u4, so this struct clones u4/U4MaterialPropertyVector.h
0015 
0016 **/
0017 
0018 #include "G4MaterialPropertyVector.hh"
0019 #include "G4SystemOfUnits.hh"
0020 
0021 #include "NP.hh"
0022 #include "NPFold.h"
0023 
0024 struct S4MaterialPropertyVector
0025 {
0026     static NP* ConvertToArray(const G4MaterialPropertyVector* vec);
0027     static G4MaterialPropertyVector* Load(const char* path);
0028 
0029     static G4MaterialPropertyVector* FromArrayData(const double* aa, int ni, int nj  );
0030     static G4MaterialPropertyVector* FromArray(const NP* prop);
0031     static G4MaterialPropertyVector* Make_V(double value);
0032     static std::string Desc_V(const G4MaterialPropertyVector* v);
0033 
0034 
0035     static void    Import_VV(         std::vector<G4MaterialPropertyVector*>& vv, const NPFold* f ) ;
0036     static NPFold* Serialize_VV(const std::vector<G4MaterialPropertyVector*>& vv ) ;
0037     static std::string  Desc_VV(const std::vector<G4MaterialPropertyVector*>& vv ) ;
0038 
0039     static void  Import_VV_CombinedArray(          std::vector<G4MaterialPropertyVector*>& vv, const NP* vvcom );
0040     static NP* Serialize_VV_CombinedArray(   const std::vector<G4MaterialPropertyVector*>& vv ) ;
0041     static std::string Desc_VV_CombinedArray(const std::vector<G4MaterialPropertyVector*>& vv );
0042 
0043 
0044     static void    Import_MSV(          std::map<std::string,G4MaterialPropertyVector*>& msv, const NPFold* sub);
0045     static NPFold* Serialize_MSV( const std::map<std::string,G4MaterialPropertyVector*>& msv );
0046     static std::string Desc_MSV(  const std::map<std::string,G4MaterialPropertyVector*>& msv ) ;
0047 
0048     static void Import_MIMSV(            std::map<int,std::map<std::string,G4MaterialPropertyVector*>>& mimsv, const NPFold* f );
0049     static NPFold* Serialize_MIMSV(const std::map<int,std::map<std::string,G4MaterialPropertyVector*>>& mimsv ) ;
0050     static std::string Desc_MIMSV( const std::map<int,std::map<std::string,G4MaterialPropertyVector*>>& mimsv ) ;
0051 
0052 };
0053 
0054 
0055 inline NP* S4MaterialPropertyVector::ConvertToArray(const G4MaterialPropertyVector* prop)
0056 {
0057     size_t num_val = prop->GetVectorLength() ;
0058     NP* a = NP::Make<double>( num_val, 2 );
0059     double* a_v = a->values<double>();
0060     for(size_t i=0 ; i < num_val ; i++)
0061     {
0062         G4double domain = prop->Energy(i);
0063         G4double value = (*prop)[i] ;
0064         a_v[2*i+0] = domain ;
0065         a_v[2*i+1] = value ;
0066     }
0067     return a ;
0068 }
0069 
0070 inline G4MaterialPropertyVector* S4MaterialPropertyVector::Load(const char* path ) // static
0071 {
0072     const NP* a = NP::Load(path);
0073     return a ? FromArray(a) : nullptr ;
0074 }
0075 
0076 
0077 inline G4MaterialPropertyVector* S4MaterialPropertyVector::FromArrayData(const double* aa, int ni, int nj  ) // static
0078 {
0079     assert( ni >= 2 );
0080     assert( nj == 2 );
0081 
0082     G4double* energy = new G4double[ni] ;
0083     G4double* value = new G4double[ni] ;
0084 
0085     for(int i=0 ; i < int(ni) ; i++)
0086     {
0087         energy[i] = aa[i*nj+0] ;
0088         value[i] = aa[i*nj+1] ;
0089     }
0090     G4MaterialPropertyVector* vec = new G4MaterialPropertyVector(energy, value, ni);
0091     return vec ;
0092 }
0093 
0094 
0095 
0096 
0097 #ifdef OLD
0098 inline G4MaterialPropertyVector* S4MaterialPropertyVector::FromArray(const NP* a ) // static
0099 {
0100     assert( a->uifc == 'f' && a->ebyte == 8 );
0101 
0102     size_t ni = a->shape[0] ;
0103     size_t nj = a->shape[1] ;
0104     assert( nj == 2 );
0105 
0106     G4double* energy = new G4double[ni] ;
0107     G4double* value = new G4double[ni] ;
0108 
0109     for(int i=0 ; i < int(ni) ; i++)
0110     {
0111         energy[i] = a->get<double>(i,0) ;
0112         value[i] = a->get<double>(i,1) ;
0113     }
0114     G4MaterialPropertyVector* vec = new G4MaterialPropertyVector(energy, value, ni);
0115     return vec ;
0116 }
0117 #else
0118 inline G4MaterialPropertyVector* S4MaterialPropertyVector::FromArray(const NP* a ) // static
0119 {
0120     size_t ni = a->shape[0] ;
0121     size_t nj = a->shape[1] ;
0122     assert( nj == 2 );
0123     assert( a->uifc == 'f' && a->ebyte == 8 );
0124     const double* aa = a->cvalues<double>();
0125     G4MaterialPropertyVector* vec = FromArrayData(aa, ni, nj );
0126     return vec ;
0127 }
0128 #endif
0129 
0130 
0131 
0132 
0133 
0134 
0135 inline G4MaterialPropertyVector* S4MaterialPropertyVector::Make_V(double value) // static
0136 {
0137     int n = 2 ;
0138     G4double* e = new G4double[n] ;
0139     G4double* v = new G4double[n] ;
0140 
0141     e[0] = 1.55*eV ;
0142     e[1] = 15.5*eV ;
0143 
0144     v[0] = value ;
0145     v[1] = value ;
0146 
0147     G4MaterialPropertyVector* mpt = new G4MaterialPropertyVector(e, v, n);
0148     return mpt ;
0149 }
0150 
0151 inline std::string S4MaterialPropertyVector::Desc_V(const G4MaterialPropertyVector* v)
0152 {
0153     size_t len = v->GetVectorLength() ;
0154     std::stringstream ss ;
0155     ss << " Desc_V"
0156        << " len " << len
0157        << std::endl
0158        ;
0159     std::string s = ss.str();
0160     return s ;
0161 }
0162 
0163 
0164 
0165 
0166 
0167 
0168 inline void  S4MaterialPropertyVector::Import_VV(         std::vector<G4MaterialPropertyVector*>& vv, const NPFold* f ) // static
0169 {
0170     [[maybe_unused]] int num_sub = f->get_num_subfold();
0171     int num_items = f->num_items();
0172     assert( num_sub == 0 );
0173 
0174     vv.resize(num_items);
0175 
0176     for(int i=0 ; i < num_items ; i++)
0177     {
0178         const char* key_ = f->get_key(i);
0179         const char* key = NPFold::BareKey(key_);
0180         int idx = std::atoi(key);
0181         assert( idx == i );
0182         const NP* a = f->get_array(idx);
0183         G4MaterialPropertyVector* mpv = FromArray(a) ;
0184         vv[i] = mpv ;
0185     }
0186 }
0187 
0188 inline NPFold* S4MaterialPropertyVector::Serialize_VV(const std::vector<G4MaterialPropertyVector*>& vv )
0189 {
0190     NPFold* f = new NPFold ;
0191     int num_v = vv.size();
0192     for(int i=0 ; i < num_v ; i++)
0193     {
0194         std::string k = std::to_string(i);
0195         const G4MaterialPropertyVector* v = vv[i] ;
0196         NP* a = ConvertToArray( v );
0197         f->add( k.c_str(), a );
0198     }
0199     return f ;
0200 }
0201 
0202 inline std::string S4MaterialPropertyVector::Desc_VV(     const std::vector<G4MaterialPropertyVector*>& vv )
0203 {
0204     int num_v = vv.size();
0205     std::stringstream ss ;
0206     ss << "S4MaterialPropertyVector::Desc_VV num_v:" << num_v << std::endl ;
0207     std::string str = ss.str() ;
0208     return str ;
0209 }
0210 
0211 
0212 
0213 inline void  S4MaterialPropertyVector::Import_VV_CombinedArray(         std::vector<G4MaterialPropertyVector*>& vv, const NP* vvcom ) // static
0214 {
0215     assert( vvcom && vvcom->shape.size() == 3 );
0216     int ni = vvcom->shape[0] ;
0217     [[maybe_unused]] int nj = vvcom->shape[1] ;
0218     [[maybe_unused]] int nk = vvcom->shape[2] ;
0219     assert( nj > 1 );
0220     assert( nk == 2 );
0221 
0222     vv.resize(ni);
0223 
0224     const double* aa0 = vvcom->cvalues<double>() ;
0225 
0226     for(int i=0 ; i < ni ; i++)
0227     {
0228         const double* aa = aa0 + nj*nk*i ;
0229         G4MaterialPropertyVector* mpv = FromArrayData(aa, nj, nk) ;
0230         vv[i] = mpv ;
0231     }
0232 }
0233 
0234 
0235 
0236 /**
0237 S4MaterialPropertyVector::Serialize_VV_CombinedArray
0238 ---------------------------------------------------------
0239 
0240 This alternative to Serialize_VV provides a more efficient serialization
0241 for vectors with large numbers of entries.
0242 
0243 **/
0244 
0245 inline NP* S4MaterialPropertyVector::Serialize_VV_CombinedArray(const std::vector<G4MaterialPropertyVector*>& vv )
0246 {
0247     int num_v = vv.size();
0248     std::vector<const NP*> aa ;
0249     aa.resize(num_v);
0250     std::set<int> u_ni ;
0251 
0252     for(int i=0 ; i < num_v ; i++)
0253     {
0254         const G4MaterialPropertyVector* v = vv[i] ;
0255         const NP* a = ConvertToArray( v );
0256 
0257         assert( a->shape.size() == 2 );
0258         int ni = a->shape[0] ;
0259         [[maybe_unused]] int nj = a->shape[1] ;
0260         assert( nj == 2 ) ;
0261         u_ni.insert(ni) ;
0262         aa[i] = a ;
0263     }
0264     bool all_same_shape = u_ni.size() == 1 ;
0265     bool annotate = !all_same_shape ;
0266     const NP* parasite = nullptr ;
0267 
0268     NP* vvcom = NP::Combine(aa, annotate, parasite);
0269     return vvcom ;
0270 }
0271 
0272 inline std::string S4MaterialPropertyVector::Desc_VV_CombinedArray(const std::vector<G4MaterialPropertyVector*>& vv )
0273 {
0274     int num_v = vv.size();
0275     std::set<size_t> ulen ;
0276     for(int i=0 ; i < num_v ; i++)
0277     {
0278         const G4MaterialPropertyVector* v = vv[i] ;
0279         size_t len = v->GetVectorLength() ;
0280         ulen.insert(len);
0281     }
0282 
0283     size_t len = ulen.size() == 1 ? *ulen.begin() : 0 ;
0284     std::stringstream ss ;
0285     ss << "S4MaterialPropertyVector::Desc_VV_CombinedArray num_v " << num_v << " len " << len << std::endl;
0286     std::string str = ss.str();
0287     return str ;
0288 }
0289 
0290 
0291 inline void S4MaterialPropertyVector::Import_MSV( std::map<std::string, G4MaterialPropertyVector*>& msv, const NPFold* sub)  // static
0292 {
0293     int num_sub = sub->get_num_subfold();
0294     unsigned num_items = sub->num_items();
0295     assert( num_sub == 0 );
0296 
0297     for(unsigned idx=0 ; idx < num_items ; idx++)
0298     {
0299         const char* key_ = sub->get_key(idx);
0300         const char* key = NPFold::BareKey(key_);
0301         const NP* a = sub->get_array(idx);
0302         G4MaterialPropertyVector* mpv = FromArray(a) ;
0303         msv[key] = mpv ;
0304     }
0305 }
0306 
0307 inline NPFold* S4MaterialPropertyVector::Serialize_MSV( const std::map<std::string, G4MaterialPropertyVector*>& msv ) // static
0308 {
0309     NPFold* f = new NPFold ;
0310     typedef std::map<std::string, G4MaterialPropertyVector*> MSV ;
0311     MSV::const_iterator it = msv.begin();
0312 
0313     for(unsigned i=0 ; i < msv.size() ; i++)
0314     {
0315         const std::string& k = it->first ;
0316         const G4MaterialPropertyVector* v = it->second ;
0317         NP* a = ConvertToArray( v );
0318         f->add( k.c_str(), a );
0319         std::advance(it, 1);
0320     }
0321     return f ;
0322 }
0323 
0324 inline std::string S4MaterialPropertyVector::Desc_MSV(const std::map<std::string, G4MaterialPropertyVector*>& msv )
0325 {
0326     typedef std::map<std::string, G4MaterialPropertyVector*> MSV ;
0327     MSV::const_iterator it = msv.begin();
0328     std::stringstream ss ;
0329     ss << "S4MaterialPropertyVector::Desc_MSV" << std::endl ;
0330 
0331     for(unsigned i=0 ; i < msv.size() ; i++)
0332     {
0333         const std::string& key = it->first ;
0334         const G4MaterialPropertyVector* v = it->second ;
0335         ss
0336            << " key " << key
0337            << Desc_V(v)
0338            << std::endl
0339            ;
0340         std::advance(it, 1);
0341     }
0342     std::string str = ss.str();
0343     return str ;
0344 }
0345 
0346 
0347 
0348 
0349 inline void S4MaterialPropertyVector::Import_MIMSV( std::map<int, std::map<std::string, G4MaterialPropertyVector*>>& mimsv, const NPFold* f )
0350 {
0351     typedef std::map<std::string, G4MaterialPropertyVector*> MSV ;
0352     int num_sub = f->get_num_subfold();
0353 
0354     for(int idx=0 ; idx < num_sub ; idx++)
0355     {
0356         const char* cat = f->get_subfold_key(idx);
0357         int icat = U::To<int>(cat);
0358 
0359         NPFold* sub = f->get_subfold(idx);
0360 
0361         MSV& msv = mimsv[icat] ;
0362 
0363         Import_MSV( msv, sub );
0364     }
0365 }
0366 
0367 
0368 inline NPFold* S4MaterialPropertyVector::Serialize_MIMSV( const std::map<int, std::map<std::string, G4MaterialPropertyVector*>>& mimsv)
0369 {
0370     NPFold* f = new NPFold ;
0371 
0372     typedef std::map<std::string, G4MaterialPropertyVector*> MSV ;
0373     typedef std::map<int, MSV> MIMSV ;
0374 
0375     MIMSV::const_iterator it = mimsv.begin();
0376 
0377     for(unsigned i=0 ; i < mimsv.size() ; i++)
0378     {
0379         int icat = it->first ;
0380         const char* cat = U::FormName(icat) ;
0381 
0382         const MSV& msv = it->second ;
0383         NPFold* sub = Serialize_MSV( msv );
0384 
0385         f->add_subfold(cat, sub);
0386 
0387         std::advance(it, 1);
0388     }
0389     return f;
0390 }
0391 
0392 inline std::string S4MaterialPropertyVector::Desc_MIMSV(const std::map<int,std::map<std::string, G4MaterialPropertyVector*>>& mimsv )
0393 {
0394     std::stringstream ss ;
0395     ss << "S4MaterialPropertyVector::Desc_MIMSV" << std::endl ;
0396 
0397     typedef std::map<std::string, G4MaterialPropertyVector*> MSV ;
0398     typedef std::map<int, MSV> MIMSV ;
0399     MIMSV::const_iterator it = mimsv.begin();
0400 
0401     for(unsigned i=0 ; i < mimsv.size() ; i++)
0402     {
0403         int cat = it->first ;
0404         const MSV& msv = it->second ;
0405         ss
0406             << " cat " << cat
0407             << " msv.size " << msv.size()
0408             << std::endl
0409             << Desc_MSV(msv)
0410             << std::endl
0411             ;
0412 
0413         std::advance(it, 1);
0414     }
0415     std::string s = ss.str();
0416     return s ;
0417 }
0418 
0419 
0420