/[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 2910 by caltinay, Wed Feb 3 03:22:31 2010 UTC trunk/weipa/src/DataVar.cpp revision 3185 by caltinay, Thu Sep 16 00:30:09 2010 UTC
# Line 11  Line 11 
11  *  *
12  *******************************************************/  *******************************************************/
13    
14  #include <escriptexport/DataVar.h>  #include <weipa/DataVar.h>
15  #include <escriptexport/ElementData.h>  #include <weipa/DomainChunk.h>
16  #include <escriptexport/FinleyMesh.h>  #include <weipa/ElementData.h>
17  #include <escriptexport/NodeData.h>  #include <weipa/NodeData.h>
18  #ifndef VISIT_PLUGIN  #ifndef VISIT_PLUGIN
19  #include <escript/Data.h>  #include <escript/Data.h>
20  #endif  #endif
# Line 27  Line 27 
27  #include <silo.h>  #include <silo.h>
28  #endif  #endif
29    
30    #include <numeric> // for accumulate
31    
32  using namespace std;  using namespace std;
33    
34  namespace escriptexport {  namespace weipa {
35            
 enum {  
     NODE_CENTERED = 1,  
     ZONE_CENTERED = 2  
 };  
   
36  //  //
37  // Constructor  // Constructor
38  //  //
39  DataVar::DataVar(const string& name) :  DataVar::DataVar(const string& name) :
40      initialized(false), varName(name),      initialized(false), varName(name),
41      numSamples(0), rank(0), ptsPerSample(0), centering(0)      numSamples(0), rank(0), ptsPerSample(0)
42  {  {
43  }  }
44    
# Line 49  DataVar::DataVar(const string& name) : Line 46  DataVar::DataVar(const string& name) :
46  // Copy constructor  // Copy constructor
47  //  //
48  DataVar::DataVar(const DataVar& d) :  DataVar::DataVar(const DataVar& d) :
49      initialized(d.initialized), finleyMesh(d.finleyMesh),      initialized(d.initialized), domain(d.domain),
50      varName(d.varName), numSamples(d.numSamples),      varName(d.varName), numSamples(d.numSamples),
51      rank(d.rank), ptsPerSample(d.ptsPerSample), centering(d.centering),      rank(d.rank), ptsPerSample(d.ptsPerSample), funcSpace(d.funcSpace),
52      funcSpace(d.funcSpace), shape(d.shape), sampleID(d.sampleID)      centering(d.centering), shape(d.shape), sampleID(d.sampleID)
53  {  {
54      if (numSamples > 0) {      if (numSamples > 0) {
55          CoordArray::const_iterator it;          CoordArray::const_iterator it;
# Line 90  void DataVar::cleanup() Line 87  void DataVar::cleanup()
87  //  //
88  //  //
89  //  //
90  bool DataVar::initFromEscript(escript::Data& escriptData, FinleyMesh_ptr mesh)  bool DataVar::initFromEscript(escript::Data& escriptData, const_DomainChunk_ptr dom)
91  {  {
92  #ifndef VISIT_PLUGIN  #ifndef VISIT_PLUGIN
93      cleanup();      cleanup();
94    
95      if (!escriptData.actsExpanded()) {      if (!escriptData.isConstant() && !escriptData.actsExpanded()) {
96          cerr << "WARNING: Only expanded data supported!" << endl;          cerr << "WARNING: Weipa only supports constant & expanded data, "
97                << "not initializing " << varName << endl;
98          return false;          return false;
99      }      }
100    
101      finleyMesh = mesh;      domain = dom;
102      rank = escriptData.getDataPointRank();      rank = escriptData.getDataPointRank();
103      ptsPerSample = escriptData.getNumDataPointsPerSample();      ptsPerSample = escriptData.getNumDataPointsPerSample();
104      shape = escriptData.getDataPointShape();      shape = escriptData.getDataPointShape();
105      funcSpace = escriptData.getFunctionSpace().getTypeCode();      funcSpace = escriptData.getFunctionSpace().getTypeCode();
106      numSamples = escriptData.getNumSamples();      numSamples = escriptData.getNumSamples();
107        centering = domain->getCenteringForFunctionSpace(funcSpace);
     if (funcSpace == FINLEY_REDUCED_NODES || funcSpace == FINLEY_NODES) {  
         centering = NODE_CENTERED;  
     } else {  
         centering = ZONE_CENTERED;  
     }  
108    
109  #ifdef _DEBUG  #ifdef _DEBUG
110      cout << varName << ":\t" << numSamples << " samples,  "      cout << varName << ":\t" << numSamples << " samples,  "
111          << ptsPerSample << " pts/s,  rank: " << rank << endl;          << ptsPerSample << " pts/s,  rank: " << rank << endl;
112  #endif  #endif
113    
114      NodeData_ptr nodes = finleyMesh->getMeshForFinleyFS(funcSpace);      NodeData_ptr nodes = domain->getMeshForFunctionSpace(funcSpace);
115      if (nodes == NULL)      if (nodes == NULL)
116          return false;          return false;
117    
# Line 148  bool DataVar::initFromEscript(escript::D Line 141  bool DataVar::initFromEscript(escript::D
141          size_t dataSize = dimSize * ptsPerSample;          size_t dataSize = dimSize * ptsPerSample;
142          float* tempData = new float[dataSize*numSamples];          float* tempData = new float[dataSize*numSamples];
143          float* destPtr = tempData;          float* destPtr = tempData;
144          for (int sampleNo=0; sampleNo<numSamples; sampleNo++) {          if (escriptData.isConstant()) {
145              const escript::DataAbstract::ValueType::value_type* values =              const escript::DataAbstract::ValueType::value_type* values =
146                  escriptData.getSampleDataRO(sampleNo);                  escriptData.getSampleDataRO(0);
147              copy(values, values+dataSize, destPtr);              for (int pointNo=0; pointNo<numSamples*ptsPerSample; pointNo++) {
148              destPtr += dataSize;                  copy(values, values+dimSize, destPtr);
149                    destPtr += dimSize;
150                }
151            } else {
152                for (int sampleNo=0; sampleNo<numSamples; sampleNo++) {
153                    const escript::DataAbstract::ValueType::value_type* values =
154                        escriptData.getSampleDataRO(sampleNo);
155                    copy(values, values+dataSize, destPtr);
156                    destPtr += dataSize;
157                }
158          }          }
159    
160          const float* srcPtr = tempData;          const float* srcPtr = tempData;
# Line 173  bool DataVar::initFromEscript(escript::D Line 175  bool DataVar::initFromEscript(escript::D
175  }  }
176    
177  //  //
178  // Initialise with mesh data  // Initialise with domain data
179  //  //
180  bool DataVar::initFromMesh(FinleyMesh_ptr mesh)  bool DataVar::initFromMeshData(const_DomainChunk_ptr dom, const IntVec& data,
181            int fsCode, Centering c, NodeData_ptr nodes, const IntVec& id)
182  {  {
183      cleanup();      cleanup();
184            
185      finleyMesh = mesh;      domain = dom;
186      rank = 0;      rank = 0;
187      ptsPerSample = 1;      ptsPerSample = 1;
188      NodeData_ptr nodes;      centering = c;
189        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;  
     }  
   
190      meshName = nodes->getName();      meshName = nodes->getName();
191      siloMeshName = nodes->getFullSiloName();      siloMeshName = nodes->getFullSiloName();
   
     const IntVec& data = mesh->getVarDataByName(varName);  
192      numSamples = data.size();      numSamples = data.size();
193    
194      if (numSamples > 0) {      if (numSamples > 0) {
# Line 234  bool DataVar::initFromMesh(FinleyMesh_pt Line 204  bool DataVar::initFromMesh(FinleyMesh_pt
204  }  }
205    
206  //  //
207  // Reads variable data from NetCDF file  // Reads variable data from dump file
208  //  //
209  bool DataVar::initFromNetCDF(const string& filename, FinleyMesh_ptr mesh)  bool DataVar::initFromFile(const string& filename, const_DomainChunk_ptr dom)
210  {  {
211      cleanup();      cleanup();
212            
# Line 269  bool DataVar::initFromNetCDF(const strin Line 239  bool DataVar::initFromNetCDF(const strin
239      att = input->get_att("function_space_type");      att = input->get_att("function_space_type");
240      funcSpace = att->as_int(0);      funcSpace = att->as_int(0);
241    
242      if (funcSpace == FINLEY_REDUCED_NODES || funcSpace == FINLEY_NODES) {      centering = domain->getCenteringForFunctionSpace(funcSpace);
         centering = NODE_CENTERED;  
     } else {  
         centering = ZONE_CENTERED;  
     }  
243    
244      dim = input->get_dim("num_samples");      dim = input->get_dim("num_samples");
245      numSamples = dim->size();      numSamples = dim->size();
# Line 283  bool DataVar::initFromNetCDF(const strin Line 249  bool DataVar::initFromNetCDF(const strin
249          << ptsPerSample << " pts/s,  rank: " << rank << endl;          << ptsPerSample << " pts/s,  rank: " << rank << endl;
250  #endif  #endif
251    
252      finleyMesh = mesh;      domain = dom;
253      NodeData_ptr nodes = finleyMesh->getMeshForFinleyFS(funcSpace);      NodeData_ptr nodes = domain->getMeshForFunctionSpace(funcSpace);
254      if (nodes == NULL) {      if (nodes == NULL) {
255          delete input;          delete input;
256          return false;          return false;
# Line 368  float* DataVar::averageData(const float* Line 334  float* DataVar::averageData(const float*
334          for (int i=0; i<numSamples; i++, src+=stride)          for (int i=0; i<numSamples; i++, src+=stride)
335              *dest++ = *src;              *dest++ = *src;
336      } else {      } else {
337          ElementData_ptr cells = finleyMesh->getElementsForFinleyFS(funcSpace);          ElementData_ptr cells = domain->getElementsForFunctionSpace(funcSpace);
338          int cellFactor = cells->getElementFactor();          int cellFactor = cells->getElementFactor();
339          res = new float[cellFactor * numSamples];          res = new float[cellFactor * numSamples];
340          float* dest = res;          float* dest = res;
341          QuadMaskInfo qmi = cells->getQuadMask(funcSpace);          QuadMaskInfo qmi = cells->getQuadMask(funcSpace);
342          if (qmi.mask.size() > 0) {          if (!qmi.mask.empty()) {
343              const float* tmpSrc = src;              const float* tmpSrc = src;
344              for (int i=0; i<numSamples; i++, tmpSrc+=stride*ptsPerSample) {              for (int i=0; i<numSamples; i++, tmpSrc+=stride*ptsPerSample) {
345                  for (int l=0; l<cellFactor; l++) {                  for (int l=0; l<cellFactor; l++) {
# Line 417  bool DataVar::reorderSamples() Line 383  bool DataVar::reorderSamples()
383      int cellFactor = 1;      int cellFactor = 1;
384    
385      if (centering == NODE_CENTERED) {      if (centering == NODE_CENTERED) {
386          NodeData_ptr nodes = finleyMesh->getMeshForFinleyFS(funcSpace);          NodeData_ptr nodes = domain->getMeshForFunctionSpace(funcSpace);
387          requiredIDs = &nodes->getNodeIDs();          requiredIDs = &nodes->getNodeIDs();
388          requiredNumSamples = nodes->getNumNodes();          requiredNumSamples = nodes->getNumNodes();
389      } else {      } else {
390          ElementData_ptr cells = finleyMesh->getElementsForFinleyFS(funcSpace);          ElementData_ptr cells = domain->getElementsForFunctionSpace(funcSpace);
391          if (cells == NULL)          if (cells == NULL)
392              return false;              return false;
393    
# Line 473  bool DataVar::reorderSamples() Line 439  bool DataVar::reorderSamples()
439  //  //
440  //  //
441  //  //
442    int DataVar::getNumberOfComponents() const
443    {
444        return (rank == 0 ? 1 : accumulate(shape.begin(), shape.end(), 0));
445    }
446    
447    //
448    //
449    //
450    float* DataVar::getDataFlat() const
451    {
452        int totalSize = numSamples * getNumberOfComponents();
453        float* res = new float[totalSize];
454        if (rank == 0) {
455            copy(dataArray[0], dataArray[0]+numSamples, res);
456        } else if (rank == 1) {
457            float *dest = res;
458            for (size_t c=0; c<numSamples; c++) {
459                for (size_t i=0; i<shape[0]; i++) {
460                    *dest++ = dataArray[i][c];
461                }
462            }
463        } else if (rank == 2) {
464            float *dest = res;
465            for (size_t c=0; c<numSamples; c++) {
466                for (int i=0; i<shape[1]; i++) {
467                    for (int j=0; j<shape[0]; j++) {
468                        *dest++ = dataArray[i*shape[0]+j][c];
469                    }
470                }
471            }
472        }
473    
474        return res;
475    }
476    
477    //
478    //
479    //
480  void DataVar::sampleToStream(ostream& os, int index)  void DataVar::sampleToStream(ostream& os, int index)
481  {  {
482      if (rank == 0) {      if (rank == 0) {
# Line 514  void DataVar::writeToVTK(ostream& os, in Line 518  void DataVar::writeToVTK(ostream& os, in
518      if (isNodeCentered()) {      if (isNodeCentered()) {
519          // data was reordered in reorderSamples() but for VTK we write the          // data was reordered in reorderSamples() but for VTK we write the
520          // original node mesh and thus need the original ordering...          // original node mesh and thus need the original ordering...
521          const IntVec& requiredIDs = finleyMesh->getNodes()->getNodeIDs();          const IntVec& requiredIDs = domain->getNodes()->getNodeIDs();
522          const IntVec& nodeGNI = finleyMesh->getNodes()->getGlobalNodeIndices();          const IntVec& nodeGNI = domain->getNodes()->getGlobalNodeIndices();
523          const IntVec& nodeDist = finleyMesh->getNodes()->getNodeDistribution();          const IntVec& nodeDist = domain->getNodes()->getNodeDistribution();
524          int firstId = nodeDist[ownIndex];          int firstId = nodeDist[ownIndex];
525          int lastId = nodeDist[ownIndex+1];          int lastId = nodeDist[ownIndex+1];
526          IndexMap sampleID2idx = buildIndexMap();          IndexMap sampleID2idx = buildIndexMap();
# Line 529  void DataVar::writeToVTK(ostream& os, in Line 533  void DataVar::writeToVTK(ostream& os, in
533      } else {      } else {
534          // cell data: ghost cells have been removed so do not write ghost          // cell data: ghost cells have been removed so do not write ghost
535          // samples (which are the last elements in the arrays)          // samples (which are the last elements in the arrays)
536          int toWrite =          int toWrite = domain->getElementsByName(meshName)->getNumElements();
             finleyMesh->getElementsByName(meshName)->getNumElements();  
537          for (int i=0; i<toWrite; i++) {          for (int i=0; i<toWrite; i++) {
538              sampleToStream(os, i);              sampleToStream(os, i);
539          }          }
# Line 587  string DataVar::getTensorDef() const Line 590  string DataVar::getTensorDef() const
590    
591  //  //
592  // Writes the data to given Silo file under the virtual path provided.  // Writes the data to given Silo file under the virtual path provided.
593  // 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().  
594  //  //
595  bool DataVar::writeToSilo(DBfile* dbfile, const string& siloPath)  bool DataVar::writeToSilo(DBfile* dbfile, const string& siloPath,
596                              const string& units)
597  {  {
598  #if USE_SILO  #if USE_SILO
599      if (!initialized)      if (!initialized)
# Line 609  bool DataVar::writeToSilo(DBfile* dbfile Line 612  bool DataVar::writeToSilo(DBfile* dbfile
612    
613      char* siloMesh = const_cast<char*>(siloMeshName.c_str());      char* siloMesh = const_cast<char*>(siloMeshName.c_str());
614      int dcenter = (centering == NODE_CENTERED ? DB_NODECENT : DB_ZONECENT);      int dcenter = (centering == NODE_CENTERED ? DB_NODECENT : DB_ZONECENT);
615        DBoptlist* optList = DBMakeOptlist(2);
616        if (units.length()>0) {
617            DBAddOption(optList, DBOPT_UNITS, (void*)units.c_str());
618        }
619    
620      if (rank == 0) {      if (rank == 0) {
621          ret = DBPutUcdvar1(dbfile, varName.c_str(), siloMesh, dataArray[0],          ret = DBPutUcdvar1(dbfile, varName.c_str(), siloMesh, dataArray[0],
622                  numSamples, NULL, 0, DB_FLOAT, dcenter, NULL);                  numSamples, NULL, 0, DB_FLOAT, dcenter, optList);
623      }      }
624      else if (rank == 1) {      else if (rank == 1) {
625          const string comps[3] = {          const string comps[3] = {
# Line 624  bool DataVar::writeToSilo(DBfile* dbfile Line 631  bool DataVar::writeToSilo(DBfile* dbfile
631    
632          ret = DBPutUcdvar(dbfile, varName.c_str(), siloMesh, shape[0],          ret = DBPutUcdvar(dbfile, varName.c_str(), siloMesh, shape[0],
633                  (char**)varnames, &dataArray[0], numSamples, NULL,                  (char**)varnames, &dataArray[0], numSamples, NULL,
634                  0, DB_FLOAT, dcenter, NULL);                  0, DB_FLOAT, dcenter, optList);
635      }      }
636      else {      else {
637          string tensorDir = varName+string("_comps/");          string tensorDir = varName+string("_comps/");
638          ret = DBMkdir(dbfile, tensorDir.c_str());          ret = DBMkdir(dbfile, tensorDir.c_str());
639          if (ret == 0) {          if (ret == 0) {
640              int one = 1;              int one = 1;
             DBoptlist* optList = DBMakeOptlist(1);  
641              DBAddOption(optList, DBOPT_HIDE_FROM_GUI, &one);              DBAddOption(optList, DBOPT_HIDE_FROM_GUI, &one);
642    
643              for (int i=0; i<shape[1]; i++) {              for (int i=0; i<shape[1]; i++) {
# Line 645  bool DataVar::writeToSilo(DBfile* dbfile Line 651  bool DataVar::writeToSilo(DBfile* dbfile
651                  }                  }
652                  if (ret != 0) break;                  if (ret != 0) break;
653              }              }
             DBFreeOptlist(optList);  
654          } // ret==0          } // ret==0
655      } // rank      } // rank
656    
657        DBFreeOptlist(optList);
658      DBSetDir(dbfile, "/");      DBSetDir(dbfile, "/");
659      return (ret == 0);      return (ret == 0);
660    
# Line 657  bool DataVar::writeToSilo(DBfile* dbfile Line 663  bool DataVar::writeToSilo(DBfile* dbfile
663  #endif  #endif
664  }  }
665    
666  } // namespace escriptexport  } // namespace weipa
667    

Legend:
Removed from v.2910  
changed lines
  Added in v.3185

  ViewVC Help
Powered by ViewVC 1.1.26