/[escript]/branches/split/weipa/src/DataVar.cpp
ViewVC logotype

Diff of /branches/split/weipa/src/DataVar.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/dataexporter/src/DataVar.cpp revision 2940 by caltinay, Fri Feb 19 00:38:45 2010 UTC trunk/weipa/src/DataVar.cpp revision 4657 by jfenwick, Thu Feb 6 06:12:20 2014 UTC
# Line 1  Line 1 
1    
2  /*******************************************************  /*****************************************************************************
3  *  *
4  * Copyright (c) 2003-2010 by University of Queensland  * Copyright (c) 2003-2014 by University of Queensland
5  * Earth Systems Science Computational Center (ESSCC)  * http://www.uq.edu.au
 * http://www.uq.edu.au/esscc  
6  *  *
7  * Primary Business: Queensland, Australia  * Primary Business: Queensland, Australia
8  * Licensed under the Open Software License version 3.0  * Licensed under the Open Software License version 3.0
9  * http://www.opensource.org/licenses/osl-3.0.php  * http://www.opensource.org/licenses/osl-3.0.php
10  *  *
11  *******************************************************/  * Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12    * Development 2012-2013 by School of Earth Sciences
13    * Development from 2014 by Centre for Geoscience Computing (GeoComp)
14    *
15    *****************************************************************************/
16    
17  #include <escriptexport/DataVar.h>  #include <weipa/DataVar.h>
18  #include <escriptexport/ElementData.h>  #include <weipa/DomainChunk.h>
19  #include <escriptexport/FinleyMesh.h>  #include <weipa/ElementData.h>
20  #include <escriptexport/NodeData.h>  #include <weipa/NodeData.h>
21  #ifndef VISIT_PLUGIN  #ifndef VISIT_PLUGIN
22  #include <escript/Data.h>  #include <escript/Data.h>
23  #endif  #endif
# Line 27  Line 30 
30  #include <silo.h>  #include <silo.h>
31  #endif  #endif
32    
33    #include <numeric> // for accumulate
34    #include <iostream> // for cerr
35    #include <stdio.h>
36    
37  using namespace std;  using namespace std;
38    
39  namespace escriptexport {  namespace weipa {
40            
 enum {  
     NODE_CENTERED = 1,  
     ZONE_CENTERED = 2  
 };  
   
41  //  //
42  // Constructor  // Constructor
43  //  //
44  DataVar::DataVar(const string& name) :  DataVar::DataVar(const string& name) :
45      initialized(false), varName(name),      initialized(false), varName(name),
46      numSamples(0), rank(0), ptsPerSample(0), centering(0)      numSamples(0), rank(0), ptsPerSample(0)
47  {  {
48  }  }
49    
# Line 49  DataVar::DataVar(const string& name) : Line 51  DataVar::DataVar(const string& name) :
51  // Copy constructor  // Copy constructor
52  //  //
53  DataVar::DataVar(const DataVar& d) :  DataVar::DataVar(const DataVar& d) :
54      initialized(d.initialized), finleyMesh(d.finleyMesh),      initialized(d.initialized), domain(d.domain),
55      varName(d.varName), numSamples(d.numSamples),      varName(d.varName), numSamples(d.numSamples),
56      rank(d.rank), ptsPerSample(d.ptsPerSample), centering(d.centering),      rank(d.rank), ptsPerSample(d.ptsPerSample), funcSpace(d.funcSpace),
57      funcSpace(d.funcSpace), shape(d.shape), sampleID(d.sampleID)      centering(d.centering), shape(d.shape), sampleID(d.sampleID)
58  {  {
59      if (numSamples > 0) {      if (numSamples > 0) {
60          CoordArray::const_iterator it;          CoordArray::const_iterator it;
# Line 90  void DataVar::cleanup() Line 92  void DataVar::cleanup()
92  //  //
93  //  //
94  //  //
95  bool DataVar::initFromEscript(escript::Data& escriptData, FinleyMesh_ptr mesh)  bool DataVar::initFromEscript(escript::Data& escriptData, const_DomainChunk_ptr dom)
96  {  {
97  #ifndef VISIT_PLUGIN  #ifndef VISIT_PLUGIN
98      cleanup();      cleanup();
99    
100      if (!escriptData.actsExpanded()) {      if (!escriptData.isConstant() && !escriptData.actsExpanded()) {
101          cerr << "WARNING: Only expanded data supported!" << endl;          cerr << "WARNING: Weipa only supports constant & expanded data, "
102                << "not initializing " << varName << endl;
103          return false;          return false;
104      }      }
105    
106      finleyMesh = mesh;      domain = dom;
107      rank = escriptData.getDataPointRank();      rank = escriptData.getDataPointRank();
108      ptsPerSample = escriptData.getNumDataPointsPerSample();      ptsPerSample = escriptData.getNumDataPointsPerSample();
109      shape = escriptData.getDataPointShape();      shape = escriptData.getDataPointShape();
110      funcSpace = escriptData.getFunctionSpace().getTypeCode();      funcSpace = escriptData.getFunctionSpace().getTypeCode();
111      numSamples = escriptData.getNumSamples();      numSamples = escriptData.getNumSamples();
112        centering = domain->getCenteringForFunctionSpace(funcSpace);
     if (funcSpace == FINLEY_REDUCED_NODES || funcSpace == FINLEY_NODES) {  
         centering = NODE_CENTERED;  
     } else {  
         centering = ZONE_CENTERED;  
     }  
113    
114  #ifdef _DEBUG  #ifdef _DEBUG
115      cout << varName << ":\t" << numSamples << " samples,  "      cout << varName << ":\t" << numSamples << " samples,  "
116          << ptsPerSample << " pts/s,  rank: " << rank << endl;          << ptsPerSample << " pts/s,  rank: " << rank << endl;
117  #endif  #endif
118    
119      NodeData_ptr nodes = finleyMesh->getMeshForFinleyFS(funcSpace);      NodeData_ptr nodes = domain->getMeshForFunctionSpace(funcSpace);
120      if (nodes == NULL)      if (nodes == NULL)
121          return false;          return false;
122    
# Line 144  bool DataVar::initFromEscript(escript::D Line 142  bool DataVar::initFromEscript(escript::D
142          initialized = false;          initialized = false;
143      }      }
144    
145        // special case: shape=(1,) or shape=(1,1) -> convert to scalar
146        if (dimSize==1 && rank>0) {
147            rank=0;
148            shape.clear();
149        }
150    
151      if (initialized) {      if (initialized) {
152          size_t dataSize = dimSize * ptsPerSample;          size_t dataSize = dimSize * ptsPerSample;
153          float* tempData = new float[dataSize*numSamples];          float* tempData = new float[dataSize*numSamples];
154          float* destPtr = tempData;          float* destPtr = tempData;
155          for (int sampleNo=0; sampleNo<numSamples; sampleNo++) {          if (escriptData.isConstant()) {
156              const escript::DataAbstract::ValueType::value_type* values =              const escript::DataAbstract::ValueType::value_type* values =
157                  escriptData.getSampleDataRO(sampleNo);                  escriptData.getSampleDataRO(0);
158              copy(values, values+dataSize, destPtr);              for (int pointNo=0; pointNo<numSamples*ptsPerSample; pointNo++) {
159              destPtr += dataSize;                  copy(values, values+dimSize, destPtr);
160                    destPtr += dimSize;
161                }
162            } else {
163                for (int sampleNo=0; sampleNo<numSamples; sampleNo++) {
164                    const escript::DataAbstract::ValueType::value_type* values =
165                        escriptData.getSampleDataRO(sampleNo);
166                    copy(values, values+dataSize, destPtr);
167                    destPtr += dataSize;
168                }
169          }          }
170    
171          const float* srcPtr = tempData;          const float* srcPtr = tempData;
# Line 173  bool DataVar::initFromEscript(escript::D Line 186  bool DataVar::initFromEscript(escript::D
186  }  }
187    
188  //  //
189  // Initialise with mesh data  // Initialise with domain data
190  //  //
191  bool DataVar::initFromMesh(FinleyMesh_ptr mesh)  bool DataVar::initFromMeshData(const_DomainChunk_ptr dom, const IntVec& data,
192            int fsCode, Centering c, NodeData_ptr nodes, const IntVec& id)
193  {  {
194      cleanup();      cleanup();
195            
196      finleyMesh = mesh;      domain = dom;
197      rank = 0;      rank = 0;
198      ptsPerSample = 1;      ptsPerSample = 1;
199      NodeData_ptr nodes;      centering = c;
200        sampleID = id;
     if (varName.find("ContactElements_") != varName.npos) {  
         funcSpace = FINLEY_CONTACT_ELEMENTS_1;  
         centering = ZONE_CENTERED;  
         string elementName = varName.substr(0, varName.find('_'));  
         ElementData_ptr elements = mesh->getElementsByName(elementName);  
         nodes = elements->getNodeMesh();  
         sampleID = elements->getIDs();  
     } else if (varName.find("FaceElements_") != varName.npos) {  
         funcSpace = FINLEY_FACE_ELEMENTS;  
         centering = ZONE_CENTERED;  
         string elementName = varName.substr(0, varName.find('_'));  
         ElementData_ptr elements = mesh->getElementsByName(elementName);  
         nodes = elements->getNodeMesh();  
         sampleID = elements->getIDs();  
     } else if (varName.find("Elements_") != varName.npos) {  
         funcSpace = FINLEY_ELEMENTS;  
         centering = ZONE_CENTERED;  
         string elementName = varName.substr(0, varName.find('_'));  
         ElementData_ptr elements = mesh->getElementsByName(elementName);  
         nodes = elements->getNodeMesh();  
         sampleID = elements->getIDs();  
     } else if (varName.find("Nodes_") != varName.npos) {  
         funcSpace = FINLEY_NODES;  
         centering = NODE_CENTERED;  
         nodes = mesh->getNodes();  
         sampleID = nodes->getNodeIDs();  
     } else {  
         cerr << "WARNING: Unrecognized mesh variable '" << varName << "'\n";  
         return false;  
     }  
   
201      meshName = nodes->getName();      meshName = nodes->getName();
202      siloMeshName = nodes->getFullSiloName();      siloMeshName = nodes->getFullSiloName();
   
     const IntVec& data = mesh->getVarDataByName(varName);  
203      numSamples = data.size();      numSamples = data.size();
204    
205      if (numSamples > 0) {      if (numSamples > 0) {
# Line 234  bool DataVar::initFromMesh(FinleyMesh_pt Line 215  bool DataVar::initFromMesh(FinleyMesh_pt
215  }  }
216    
217  //  //
218  // Reads variable data from NetCDF file  // Reads variable data from dump file
219  //  //
220  bool DataVar::initFromNetCDF(const string& filename, FinleyMesh_ptr mesh)  bool DataVar::initFromFile(const string& filename, const_DomainChunk_ptr dom)
221  {  {
222      cleanup();      cleanup();
223            
# Line 269  bool DataVar::initFromNetCDF(const strin Line 250  bool DataVar::initFromNetCDF(const strin
250      att = input->get_att("function_space_type");      att = input->get_att("function_space_type");
251      funcSpace = att->as_int(0);      funcSpace = att->as_int(0);
252    
253      if (funcSpace == FINLEY_REDUCED_NODES || funcSpace == FINLEY_NODES) {      centering = dom->getCenteringForFunctionSpace(funcSpace);
         centering = NODE_CENTERED;  
     } else {  
         centering = ZONE_CENTERED;  
     }  
254    
255      dim = input->get_dim("num_samples");      dim = input->get_dim("num_samples");
256      numSamples = dim->size();      numSamples = dim->size();
# Line 283  bool DataVar::initFromNetCDF(const strin Line 260  bool DataVar::initFromNetCDF(const strin
260          << ptsPerSample << " pts/s,  rank: " << rank << endl;          << ptsPerSample << " pts/s,  rank: " << rank << endl;
261  #endif  #endif
262    
263      finleyMesh = mesh;      domain = dom;
264      NodeData_ptr nodes = finleyMesh->getMeshForFinleyFS(funcSpace);      NodeData_ptr nodes = domain->getMeshForFunctionSpace(funcSpace);
265      if (nodes == NULL) {      if (nodes == NULL) {
266          delete input;          delete input;
267          return false;          return false;
# Line 368  float* DataVar::averageData(const float* Line 345  float* DataVar::averageData(const float*
345          for (int i=0; i<numSamples; i++, src+=stride)          for (int i=0; i<numSamples; i++, src+=stride)
346              *dest++ = *src;              *dest++ = *src;
347      } else {      } else {
348          ElementData_ptr cells = finleyMesh->getElementsForFinleyFS(funcSpace);          ElementData_ptr cells = domain->getElementsForFunctionSpace(funcSpace);
349          int cellFactor = cells->getElementFactor();          int cellFactor = cells->getElementFactor();
350          res = new float[cellFactor * numSamples];          res = new float[cellFactor * numSamples];
351          float* dest = res;          float* dest = res;
# Line 417  bool DataVar::reorderSamples() Line 394  bool DataVar::reorderSamples()
394      int cellFactor = 1;      int cellFactor = 1;
395    
396      if (centering == NODE_CENTERED) {      if (centering == NODE_CENTERED) {
397          NodeData_ptr nodes = finleyMesh->getMeshForFinleyFS(funcSpace);          NodeData_ptr nodes = domain->getMeshForFunctionSpace(funcSpace);
398          requiredIDs = &nodes->getNodeIDs();          requiredIDs = &nodes->getNodeIDs();
399          requiredNumSamples = nodes->getNumNodes();          requiredNumSamples = nodes->getNumNodes();
400      } else {      } else {
401          ElementData_ptr cells = finleyMesh->getElementsForFinleyFS(funcSpace);          ElementData_ptr cells = domain->getElementsForFunctionSpace(funcSpace);
402          if (cells == NULL)          if (cells == NULL)
403              return false;              return false;
404    
# Line 473  bool DataVar::reorderSamples() Line 450  bool DataVar::reorderSamples()
450  //  //
451  //  //
452  //  //
453    int DataVar::getNumberOfComponents() const
454    {
455        return (rank == 0 ? 1 : accumulate(shape.begin(), shape.end(), 0));
456    }
457    
458    //
459    //
460    //
461    float* DataVar::getDataFlat() const
462    {
463        int totalSize = numSamples * getNumberOfComponents();
464        float* res = new float[totalSize];
465        if (rank == 0) {
466            copy(dataArray[0], dataArray[0]+numSamples, res);
467        } else if (rank == 1) {
468            float *dest = res;
469            for (size_t c=0; c<numSamples; c++) {
470                for (size_t i=0; i<shape[0]; i++) {
471                    *dest++ = dataArray[i][c];
472                }
473            }
474        } else if (rank == 2) {
475            float *dest = res;
476            for (size_t c=0; c<numSamples; c++) {
477                for (int i=0; i<shape[1]; i++) {
478                    for (int j=0; j<shape[0]; j++) {
479                        *dest++ = dataArray[i*shape[0]+j][c];
480                    }
481                }
482            }
483        }
484    
485        return res;
486    }
487    
488    //
489    //
490    //
491  void DataVar::sampleToStream(ostream& os, int index)  void DataVar::sampleToStream(ostream& os, int index)
492  {  {
493        // index is -1 for dummy samples, i.e. if writing the full mesh but
494        // only a reduced number of samples is required
495      if (rank == 0) {      if (rank == 0) {
496          os << dataArray[0][index];          if (index < 0) {
497                os << 0.;
498            } else {
499                os << dataArray[0][index];
500            }
501      } else if (rank == 1) {      } else if (rank == 1) {
502          if (shape[0] < 3)          if (index < 0) {
503                os << 0. << " " << 0.  << " " << 0.;
504            } else if (shape[0] < 3) {
505              os << dataArray[0][index] << " " << dataArray[1][index]              os << dataArray[0][index] << " " << dataArray[1][index]
506                  << " " << 0.;                  << " " << 0.;
507          else          } else {
508              os << dataArray[0][index] << " " << dataArray[1][index]              os << dataArray[0][index] << " " << dataArray[1][index]
509                  << " " << dataArray[2][index];                  << " " << dataArray[2][index];
510            }
511      } else if (rank == 2) {      } else if (rank == 2) {
512          if (shape[1] < 3) {          if (index < 0) {
513                os << 0. << " " << 0. << " " << 0. << " ";
514                os << 0. << " " << 0. << " " << 0. << " ";
515                os << 0. << " " << 0. << " " << 0.;
516            } else if (shape[1] < 3) {
517              os << dataArray[0][index] << " " << dataArray[1][index]              os << dataArray[0][index] << " " << dataArray[1][index]
518                  << " " << 0. << " ";                  << " " << 0. << " ";
519              os << dataArray[2][index] << " " << dataArray[3][index]              os << dataArray[2][index] << " " << dataArray[3][index]
# Line 513  void DataVar::writeToVTK(ostream& os, in Line 541  void DataVar::writeToVTK(ostream& os, in
541    
542      if (isNodeCentered()) {      if (isNodeCentered()) {
543          // data was reordered in reorderSamples() but for VTK we write the          // data was reordered in reorderSamples() but for VTK we write the
544          // original node mesh and thus need the original ordering...          // original node mesh and thus need the original ordering.
545          const IntVec& requiredIDs = finleyMesh->getNodes()->getNodeIDs();          // Note, that this also means we may not have samples for all nodes
546          const IntVec& nodeGNI = finleyMesh->getNodes()->getGlobalNodeIndices();          // in which case we set idx to -1 and write a dummy sample
547          const IntVec& nodeDist = finleyMesh->getNodes()->getNodeDistribution();          const IntVec& requiredIDs = domain->getNodes()->getNodeIDs();
548            const IntVec& nodeGNI = domain->getNodes()->getGlobalNodeIndices();
549            const IntVec& nodeDist = domain->getNodes()->getNodeDistribution();
550          int firstId = nodeDist[ownIndex];          int firstId = nodeDist[ownIndex];
551          int lastId = nodeDist[ownIndex+1];          int lastId = nodeDist[ownIndex+1];
552          IndexMap sampleID2idx = buildIndexMap();          IndexMap sampleID2idx = buildIndexMap();
553          for (int i=0; i<nodeGNI.size(); i++) {          for (int i=0; i<nodeGNI.size(); i++) {
554              if (firstId <= nodeGNI[i] && nodeGNI[i] < lastId) {              if (firstId <= nodeGNI[i] && nodeGNI[i] < lastId) {
555                  int idx = sampleID2idx[requiredIDs[i]];                  IndexMap::const_iterator it = sampleID2idx.find(requiredIDs[i]);
556                    int idx = (it==sampleID2idx.end() ? -1 : (int)it->second);
557                  sampleToStream(os, idx);                  sampleToStream(os, idx);
558              }              }
559          }          }
560      } else {      } else {
561          // cell data: ghost cells have been removed so do not write ghost          // cell data: ghost cells have been removed so do not write ghost
562          // samples (which are the last elements in the arrays)          // samples (which are the last elements in the arrays)
563          int toWrite =          int toWrite = domain->getElementsByName(meshName)->getNumElements();
             finleyMesh->getElementsByName(meshName)->getNumElements();  
564          for (int i=0; i<toWrite; i++) {          for (int i=0; i<toWrite; i++) {
565              sampleToStream(os, i);              sampleToStream(os, i);
566          }          }
# Line 587  string DataVar::getTensorDef() const Line 617  string DataVar::getTensorDef() const
617    
618  //  //
619  // Writes the data to given Silo file under the virtual path provided.  // Writes the data to given Silo file under the virtual path provided.
620  // The corresponding mesh must have been written already and made known  // The corresponding mesh must have been written already.
 // to this variable by a call to setMesh().  
621  //  //
622  bool DataVar::writeToSilo(DBfile* dbfile, const string& siloPath)  bool DataVar::writeToSilo(DBfile* dbfile, const string& siloPath,
623                              const string& units)
624  {  {
625  #if USE_SILO  #if USE_SILO
626      if (!initialized)      if (!initialized)
# Line 609  bool DataVar::writeToSilo(DBfile* dbfile Line 639  bool DataVar::writeToSilo(DBfile* dbfile
639    
640      char* siloMesh = const_cast<char*>(siloMeshName.c_str());      char* siloMesh = const_cast<char*>(siloMeshName.c_str());
641      int dcenter = (centering == NODE_CENTERED ? DB_NODECENT : DB_ZONECENT);      int dcenter = (centering == NODE_CENTERED ? DB_NODECENT : DB_ZONECENT);
642        DBoptlist* optList = DBMakeOptlist(2);
643        if (units.length()>0) {
644            DBAddOption(optList, DBOPT_UNITS, (void*)units.c_str());
645        }
646    
647      if (rank == 0) {      if (rank == 0) {
648          ret = DBPutUcdvar1(dbfile, varName.c_str(), siloMesh, dataArray[0],          ret = DBPutUcdvar1(dbfile, varName.c_str(), siloMesh, dataArray[0],
649                  numSamples, NULL, 0, DB_FLOAT, dcenter, NULL);                  numSamples, NULL, 0, DB_FLOAT, dcenter, optList);
650      }      }
651      else if (rank == 1) {      else if (rank == 1) {
652          const string comps[3] = {          const string comps[3] = {
# Line 624  bool DataVar::writeToSilo(DBfile* dbfile Line 658  bool DataVar::writeToSilo(DBfile* dbfile
658    
659          ret = DBPutUcdvar(dbfile, varName.c_str(), siloMesh, shape[0],          ret = DBPutUcdvar(dbfile, varName.c_str(), siloMesh, shape[0],
660                  (char**)varnames, &dataArray[0], numSamples, NULL,                  (char**)varnames, &dataArray[0], numSamples, NULL,
661                  0, DB_FLOAT, dcenter, NULL);                  0, DB_FLOAT, dcenter, optList);
662      }      }
663      else {      else {
664          string tensorDir = varName+string("_comps/");          string tensorDir = varName+string("_comps/");
665          ret = DBMkdir(dbfile, tensorDir.c_str());          ret = DBMkdir(dbfile, tensorDir.c_str());
666          if (ret == 0) {          if (ret == 0) {
667              int one = 1;              int one = 1;
             DBoptlist* optList = DBMakeOptlist(1);  
668              DBAddOption(optList, DBOPT_HIDE_FROM_GUI, &one);              DBAddOption(optList, DBOPT_HIDE_FROM_GUI, &one);
669    
670              for (int i=0; i<shape[1]; i++) {              for (int i=0; i<shape[1]; i++) {
# Line 645  bool DataVar::writeToSilo(DBfile* dbfile Line 678  bool DataVar::writeToSilo(DBfile* dbfile
678                  }                  }
679                  if (ret != 0) break;                  if (ret != 0) break;
680              }              }
             DBFreeOptlist(optList);  
681          } // ret==0          } // ret==0
682      } // rank      } // rank
683    
684        DBFreeOptlist(optList);
685      DBSetDir(dbfile, "/");      DBSetDir(dbfile, "/");
686      return (ret == 0);      return (ret == 0);
687    
# Line 657  bool DataVar::writeToSilo(DBfile* dbfile Line 690  bool DataVar::writeToSilo(DBfile* dbfile
690  #endif  #endif
691  }  }
692    
693  } // namespace escriptexport  } // namespace weipa
694    

Legend:
Removed from v.2940  
changed lines
  Added in v.4657

  ViewVC Help
Powered by ViewVC 1.1.26