Changeset 982


Ignore:
Timestamp:
10/27/16 18:14:46 (8 years ago)
Author:
mhnguyen
Message:

Implementing domain interpolation weights exportation
The option 'file' locates file to read if it exists, otherwise a new file with name = 'file' will be created

+) Write interpolation weights into a file

Test
+) On Curie
+) Work

Location:
XIOS/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • XIOS/trunk/inputs/REMAP/iodef.xml

    r978 r982  
    109109    <domain_group id="domain_dst"> 
    110110     <domain id="dst_domain"> 
    111        <interpolate_domain/> 
     111       <interpolate_domain file="weight.nc" /> 
    112112     </domain> 
    113113     <domain id="dst_domain_regular_pole" ni_glo="90" nj_glo="45" type="rectilinear"> 
  • XIOS/trunk/src/config/interpolate_domain_attribute.conf

    r846 r982  
    33DECLARE_ATTRIBUTE(int, order) 
    44DECLARE_ATTRIBUTE(bool, renormalize) 
     5 
     6/* Write interpolation weights into file */ 
     7DECLARE_ENUM2(mode,write,read) 
  • XIOS/trunk/src/node/interpolate_domain.cpp

    r836 r982  
    4747             << "Please define a correct one") ; 
    4848    } 
     49 
     50    StdString weightFile = "interpolation_weights_" + domainSrc->getDomainOutputName(); 
     51 
     52    if (!this->mode.isEmpty()) 
     53    {  
     54      if (mode_attr::read == this->mode) 
     55      { 
     56         if (this->file.isEmpty()) 
     57         { 
     58            ERROR("void CInterpolateDomain::checkValid(CDomain* domainSrc)", 
     59                 << "Read mode is activated but there is no file specified." << std::endl 
     60                 << "Please define a correct file containing interpolation weights with option 'file'. "); 
     61         } 
     62         else 
     63         { 
     64           weightFile = this->file; 
     65           ifstream f(weightFile.c_str()); 
     66           if (!f.good()) 
     67             ERROR("void CInterpolateDomain::checkValid(CDomain* domainSrc)", 
     68                   << "Read mode is activated but file "  << weightFile << " doesn't exist." << std::endl 
     69                   << "Please check this file "); 
     70         } 
     71      } 
     72      else 
     73      { 
     74        if (file.isEmpty()) 
     75          this->file.setValue(weightFile); 
     76      }    
     77    } 
     78    else 
     79    { 
     80      if (!file.isEmpty()) // set mode read 
     81      { 
     82         weightFile = this->file; 
     83         ifstream f(weightFile.c_str()); 
     84         if (!f.good()) // file doesn't exist 
     85           this->mode.setValue(mode_attr::write); 
     86         else 
     87           this->mode.setValue(mode_attr::read); 
     88      } 
     89      else // only calculate weights() Mode set write but there is no file) 
     90      { 
     91        this->mode.setValue(mode_attr::write); 
     92      } 
     93    } 
     94 
    4995  } 
    5096 
  • XIOS/trunk/src/transformation/domain_algorithm_interpolate.cpp

    r978 r982  
    4848 
    4949CDomainAlgorithmInterpolate::CDomainAlgorithmInterpolate(CDomain* domainDestination, CDomain* domainSource, CInterpolateDomain* interpDomain) 
    50 : CDomainAlgorithmTransformation(domainDestination, domainSource), interpDomain_(interpDomain) 
     50: CDomainAlgorithmTransformation(domainDestination, domainSource), interpDomain_(interpDomain), writeToFile_(false) 
    5151{ 
    5252  interpDomain_->checkValid(domainSource); 
     53  if ((CInterpolateDomain::mode_attr::write == interpDomain_->mode) && 
     54      (!interpDomain_->file.isEmpty())) 
     55    writeToFile_ = true; 
    5356} 
    5457 
     
    351354  } 
    352355 
     356  if (writeToFile_) 
     357     writeRemapInfo(interpMapValue); 
    353358  exchangeRemapInfo(interpMapValue); 
    354359 
     
    434439void CDomainAlgorithmInterpolate::computeIndexSourceMapping_(const std::vector<CArray<double,1>* >& dataAuxInputs) 
    435440{ 
    436   if (!interpDomain_->file.isEmpty()) 
     441  if (CInterpolateDomain::mode_attr::read == interpDomain_->mode)   
    437442    readRemapInfo(); 
    438443  else 
    439     computeRemap(); 
     444  { 
     445    computeRemap();  
     446  } 
     447} 
     448 
     449void CDomainAlgorithmInterpolate::writeRemapInfo(std::map<int,std::vector<std::pair<int,double> > >& interpMapValue) 
     450{ 
     451  std::string filename = interpDomain_->file.getValue(); 
     452  writeInterpolationInfo(filename, interpMapValue); 
    440453} 
    441454 
    442455void CDomainAlgorithmInterpolate::readRemapInfo() 
    443456{ 
    444   CContext* context = CContext::getCurrent(); 
    445   CContextClient* client=context->client; 
    446   int clientRank = client->clientRank; 
    447  
    448457  std::string filename = interpDomain_->file.getValue(); 
    449458  std::map<int,std::vector<std::pair<int,double> > > interpMapValue; 
     
    637646  delete [] recvBuff; 
    638647} 
     648  
     649/*! Redefined some functions of CONetCDF4 to make use of them */ 
     650CDomainAlgorithmInterpolate::WriteNetCdf::WriteNetCdf(const StdString& filename, const MPI_Comm comm) 
     651  : CNc4DataOutput(filename, false, false, true, comm, false, true) {} 
     652int CDomainAlgorithmInterpolate::WriteNetCdf::addDimensionWrite(const StdString& name,  
     653                                                                const StdSize size) 
     654{ 
     655  CONetCDF4::addDimension(name, size);   
     656} 
     657 
     658int CDomainAlgorithmInterpolate::WriteNetCdf::addVariableWrite(const StdString& name, nc_type type, 
     659                                                               const std::vector<StdString>& dim) 
     660{ 
     661  CONetCDF4::addVariable(name, type, dim); 
     662} 
     663 
     664void CDomainAlgorithmInterpolate::WriteNetCdf::writeDataIndex(const CArray<int,1>& data, const StdString& name, 
     665                                                              bool collective, StdSize record, 
     666                                                              const std::vector<StdSize>* start, 
     667                                                              const std::vector<StdSize>* count) 
     668{ 
     669  CONetCDF4::writeData<int,1>(data, name, collective, record, start, count); 
     670} 
     671 
     672void CDomainAlgorithmInterpolate::WriteNetCdf::writeDataIndex(const CArray<double,1>& data, const StdString& name, 
     673                                                              bool collective, StdSize record, 
     674                                                              const std::vector<StdSize>* start, 
     675                                                              const std::vector<StdSize>* count) 
     676{ 
     677  CONetCDF4::writeData<double,1>(data, name, collective, record, start, count); 
     678} 
     679 
     680/* 
     681   Write interpolation weights into a file 
     682   \param [in] filename name of output file 
     683   \param interpMapValue mapping of global index of domain destination and domain source as well as the corresponding weight 
     684*/ 
     685void CDomainAlgorithmInterpolate::writeInterpolationInfo(std::string& filename, 
     686                                                         std::map<int,std::vector<std::pair<int,double> > >& interpMapValue) 
     687{ 
     688  CContext* context = CContext::getCurrent(); 
     689  CContextClient* client=context->client; 
     690 
     691  size_t n_src = domainSrc_->ni_glo * domainSrc_->nj_glo; 
     692  size_t n_dst = domainDest_->ni_glo * domainDest_->nj_glo; 
     693 
     694  long localNbWeight = 0; 
     695  long globalNbWeight; 
     696  long startIndex; 
     697  typedef std::map<int,std::vector<std::pair<int,double> > > IndexRemap; 
     698  IndexRemap::iterator itb = interpMapValue.begin(), it, 
     699                       ite = interpMapValue.end(); 
     700  for (it = itb; it!=ite; ++it) 
     701  { 
     702    localNbWeight += (it->second).size(); 
     703  } 
     704 
     705  CArray<int,1> src_idx(localNbWeight); 
     706  CArray<int,1> dst_idx(localNbWeight); 
     707  CArray<double,1> weights(localNbWeight); 
     708 
     709  int index = 0; 
     710  for (it = itb; it !=ite; ++it) 
     711  { 
     712    std::vector<std::pair<int,double> >& tmp = it->second; 
     713    for (int idx = 0; idx < tmp.size(); ++idx) 
     714    { 
     715      dst_idx(index) = it->first + 1; 
     716      src_idx(index) = tmp[idx].first + 1; 
     717      weights(index) = tmp[idx].second; 
     718      ++index; 
     719    }     
     720  } 
     721 
     722  MPI_Allreduce(&localNbWeight, &globalNbWeight, 1, MPI_LONG, MPI_SUM, client->intraComm); 
     723  MPI_Scan(&localNbWeight, &startIndex, 1, MPI_LONG, MPI_SUM, client->intraComm); 
     724   
     725  std::vector<StdSize> start(1, startIndex - localNbWeight); 
     726  std::vector<StdSize> count(1, localNbWeight); 
     727 
     728  WriteNetCdf netCdfWriter(filename, client->intraComm); 
     729 
     730  // netCdfWriter = CONetCDF4(filename, false, false, true, client->intraComm, false); 
     731 
     732  // Define some dimensions 
     733  netCdfWriter.addDimensionWrite("n_src", n_src); 
     734  netCdfWriter.addDimensionWrite("n_dst", n_dst); 
     735  netCdfWriter.addDimensionWrite("n_weight", globalNbWeight); 
     736   
     737  std::vector<StdString> dims(1,"n_weight"); 
     738 
     739  // Add some variables 
     740  netCdfWriter.addVariableWrite("src_idx", NC_INT, dims); 
     741  netCdfWriter.addVariableWrite("dst_idx", NC_INT, dims); 
     742  netCdfWriter.addVariableWrite("weight", NC_DOUBLE, dims); 
     743 
     744  // // Write variables 
     745  netCdfWriter.writeDataIndex(src_idx, "src_idx", true, 0, &start, &count); 
     746  netCdfWriter.writeDataIndex(dst_idx, "dst_idx", true, 0, &start, &count); 
     747  netCdfWriter.writeDataIndex(weights, "weight", true, 0, &start, &count); 
     748 
     749  netCdfWriter.closeFile(); 
     750} 
    639751 
    640752/*! 
  • XIOS/trunk/src/transformation/domain_algorithm_interpolate.hpp

    r933 r982  
    1212#include "domain_algorithm_transformation.hpp" 
    1313#include "transformation.hpp" 
     14#include "nc4_data_output.hpp" 
    1415 
    1516namespace xios { 
     
    1718class CDomain; 
    1819class CInterpolateDomain; 
     20 
    1921 
    2022/*! 
     
    3537private: 
    3638  void readInterpolationInfo(std::string& filename, std::map<int,std::vector<std::pair<int,double> > >& interpMapValue); 
     39  void writeInterpolationInfo(std::string& filename, std::map<int,std::vector<std::pair<int,double> > >& interpMapValue); 
    3740  void processPole(std::map<int,std::vector<std::pair<int,double> > >& interMapValuePole, 
    3841                   int nbGlobalPointOnPole); 
    3942  void computeRemap(); 
    4043  void readRemapInfo(); 
     44  void writeRemapInfo(std::map<int,std::vector<std::pair<int,double> > >&); 
    4145  void exchangeRemapInfo(std::map<int,std::vector<std::pair<int,double> > >& interpMapValue); 
     46 
    4247private: 
    4348  CInterpolateDomain* interpDomain_; 
     49  bool writeToFile_; 
     50 
     51  // class WriteNetCdf; 
     52  class WriteNetCdf : public CNc4DataOutput 
     53  { 
     54  public: 
     55    WriteNetCdf(const StdString& filename, const MPI_Comm comm); 
     56    int addDimensionWrite(const StdString& name, const StdSize size = UNLIMITED_DIM); 
     57    int addVariableWrite(const StdString& name, nc_type type, 
     58                         const std::vector<StdString>& dim); 
     59    void writeDataIndex(const CArray<int,1>& data, const StdString& name, 
     60                        bool collective, StdSize record, 
     61                        const std::vector<StdSize>* start = NULL, 
     62                        const std::vector<StdSize>* count = NULL); 
     63    void writeDataIndex(const CArray<double,1>& data, const StdString& name, 
     64                        bool collective, StdSize record, 
     65                        const std::vector<StdSize>* start = NULL, 
     66                        const std::vector<StdSize>* count = NULL); 
     67  }; 
     68 
    4469 
    4570private: 
Note: See TracChangeset for help on using the changeset viewer.