/[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 3146 by caltinay, Fri Sep 3 01:29:21 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();
# Line 100  bool DataVar::initFromEscript(escript::D Line 97  bool DataVar::initFromEscript(escript::D
97          return false;          return false;
98      }      }
99    
100      finleyMesh = mesh;      domain = dom;
101      rank = escriptData.getDataPointRank();      rank = escriptData.getDataPointRank();
102      ptsPerSample = escriptData.getNumDataPointsPerSample();      ptsPerSample = escriptData.getNumDataPointsPerSample();
103      shape = escriptData.getDataPointShape();      shape = escriptData.getDataPointShape();
104      funcSpace = escriptData.getFunctionSpace().getTypeCode();      funcSpace = escriptData.getFunctionSpace().getTypeCode();
105      numSamples = escriptData.getNumSamples();      numSamples = escriptData.getNumSamples();
106        centering = domain->getCenteringForFunctionSpace(funcSpace);
     if (funcSpace == FINLEY_REDUCED_NODES || funcSpace == FINLEY_NODES) {  
         centering = NODE_CENTERED;  
     } else {  
         centering = ZONE_CENTERED;  
     }  
107    
108  #ifdef _DEBUG  #ifdef _DEBUG
109      cout << varName << ":\t" << numSamples << " samples,  "      cout << varName << ":\t" << numSamples << " samples,  "
110          << ptsPerSample << " pts/s,  rank: " << rank << endl;          << ptsPerSample << " pts/s,  rank: " << rank << endl;
111  #endif  #endif
112    
113      NodeData_ptr nodes = finleyMesh->getMeshForFinleyFS(funcSpace);      NodeData_ptr nodes = domain->getMeshForFunctionSpace(funcSpace);
114      if (nodes == NULL)      if (nodes == NULL)
115          return false;          return false;
116    
# Line 173  bool DataVar::initFromEscript(escript::D Line 165  bool DataVar::initFromEscript(escript::D
165  }  }
166    
167  //  //
168  // Initialise with mesh data  // Initialise with domain data
169  //  //
170  bool DataVar::initFromMesh(FinleyMesh_ptr mesh)  bool DataVar::initFromMeshData(const_DomainChunk_ptr dom, const IntVec& data,
171            int fsCode, Centering c, NodeData_ptr nodes, const IntVec& id)
172  {  {
173      cleanup();      cleanup();
174            
175      finleyMesh = mesh;      domain = dom;
176      rank = 0;      rank = 0;
177      ptsPerSample = 1;      ptsPerSample = 1;
178      NodeData_ptr nodes;      centering = c;
179        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;  
     }  
   
180      meshName = nodes->getName();      meshName = nodes->getName();
181      siloMeshName = nodes->getFullSiloName();      siloMeshName = nodes->getFullSiloName();
   
     const IntVec& data = mesh->getVarDataByName(varName);  
182      numSamples = data.size();      numSamples = data.size();
183    
184      if (numSamples > 0) {      if (numSamples > 0) {
# Line 234  bool DataVar::initFromMesh(FinleyMesh_pt Line 194  bool DataVar::initFromMesh(FinleyMesh_pt
194  }  }
195    
196  //  //
197  // Reads variable data from NetCDF file  // Reads variable data from dump file
198  //  //
199  bool DataVar::initFromNetCDF(const string& filename, FinleyMesh_ptr mesh)  bool DataVar::initFromFile(const string& filename, const_DomainChunk_ptr dom)
200  {  {
201      cleanup();      cleanup();
202            
# Line 269  bool DataVar::initFromNetCDF(const strin Line 229  bool DataVar::initFromNetCDF(const strin
229      att = input->get_att("function_space_type");      att = input->get_att("function_space_type");
230      funcSpace = att->as_int(0);      funcSpace = att->as_int(0);
231    
232      if (funcSpace == FINLEY_REDUCED_NODES || funcSpace == FINLEY_NODES) {      centering = domain->getCenteringForFunctionSpace(funcSpace);
         centering = NODE_CENTERED;  
     } else {  
         centering = ZONE_CENTERED;  
     }  
233    
234      dim = input->get_dim("num_samples");      dim = input->get_dim("num_samples");
235      numSamples = dim->size();      numSamples = dim->size();
# Line 283  bool DataVar::initFromNetCDF(const strin Line 239  bool DataVar::initFromNetCDF(const strin
239          << ptsPerSample << " pts/s,  rank: " << rank << endl;          << ptsPerSample << " pts/s,  rank: " << rank << endl;
240  #endif  #endif
241    
242      finleyMesh = mesh;      domain = dom;
243      NodeData_ptr nodes = finleyMesh->getMeshForFinleyFS(funcSpace);      NodeData_ptr nodes = domain->getMeshForFunctionSpace(funcSpace);
244      if (nodes == NULL) {      if (nodes == NULL) {
245          delete input;          delete input;
246          return false;          return false;
# Line 368  float* DataVar::averageData(const float* Line 324  float* DataVar::averageData(const float*
324          for (int i=0; i<numSamples; i++, src+=stride)          for (int i=0; i<numSamples; i++, src+=stride)
325              *dest++ = *src;              *dest++ = *src;
326      } else {      } else {
327          ElementData_ptr cells = finleyMesh->getElementsForFinleyFS(funcSpace);          ElementData_ptr cells = domain->getElementsForFunctionSpace(funcSpace);
328          int cellFactor = cells->getElementFactor();          int cellFactor = cells->getElementFactor();
329          res = new float[cellFactor * numSamples];          res = new float[cellFactor * numSamples];
330          float* dest = res;          float* dest = res;
331          QuadMaskInfo qmi = cells->getQuadMask(funcSpace);          QuadMaskInfo qmi = cells->getQuadMask(funcSpace);
332          if (qmi.mask.size() > 0) {          if (!qmi.mask.empty()) {
333              const float* tmpSrc = src;              const float* tmpSrc = src;
334              for (int i=0; i<numSamples; i++, tmpSrc+=stride*ptsPerSample) {              for (int i=0; i<numSamples; i++, tmpSrc+=stride*ptsPerSample) {
335                  for (int l=0; l<cellFactor; l++) {                  for (int l=0; l<cellFactor; l++) {
# Line 417  bool DataVar::reorderSamples() Line 373  bool DataVar::reorderSamples()
373      int cellFactor = 1;      int cellFactor = 1;
374    
375      if (centering == NODE_CENTERED) {      if (centering == NODE_CENTERED) {
376          NodeData_ptr nodes = finleyMesh->getMeshForFinleyFS(funcSpace);          NodeData_ptr nodes = domain->getMeshForFunctionSpace(funcSpace);
377          requiredIDs = &nodes->getNodeIDs();          requiredIDs = &nodes->getNodeIDs();
378          requiredNumSamples = nodes->getNumNodes();          requiredNumSamples = nodes->getNumNodes();
379      } else {      } else {
380          ElementData_ptr cells = finleyMesh->getElementsForFinleyFS(funcSpace);          ElementData_ptr cells = domain->getElementsForFunctionSpace(funcSpace);
381          if (cells == NULL)          if (cells == NULL)
382              return false;              return false;
383    
# Line 473  bool DataVar::reorderSamples() Line 429  bool DataVar::reorderSamples()
429  //  //
430  //  //
431  //  //
432    int DataVar::getNumberOfComponents() const
433    {
434        return (rank == 0 ? 1 : accumulate(shape.begin(), shape.end(), 0));
435    }
436    
437    //
438    //
439    //
440    float* DataVar::getDataFlat() const
441    {
442        int totalSize = numSamples * getNumberOfComponents();
443        float* res = new float[totalSize];
444        if (rank == 0) {
445            copy(dataArray[0], dataArray[0]+numSamples, res);
446        } else if (rank == 1) {
447            float *dest = res;
448            for (size_t c=0; c<numSamples; c++) {
449                for (size_t i=0; i<shape[0]; i++) {
450                    *dest++ = dataArray[i][c];
451                }
452            }
453        } else if (rank == 2) {
454            float *dest = res;
455            for (size_t c=0; c<numSamples; c++) {
456                for (int i=0; i<shape[1]; i++) {
457                    for (int j=0; j<shape[0]; j++) {
458                        *dest++ = dataArray[i*shape[0]+j][c];
459                    }
460                }
461            }
462        }
463    
464        return res;
465    }
466    
467    //
468    //
469    //
470  void DataVar::sampleToStream(ostream& os, int index)  void DataVar::sampleToStream(ostream& os, int index)
471  {  {
472      if (rank == 0) {      if (rank == 0) {
# Line 514  void DataVar::writeToVTK(ostream& os, in Line 508  void DataVar::writeToVTK(ostream& os, in
508      if (isNodeCentered()) {      if (isNodeCentered()) {
509          // data was reordered in reorderSamples() but for VTK we write the          // data was reordered in reorderSamples() but for VTK we write the
510          // original node mesh and thus need the original ordering...          // original node mesh and thus need the original ordering...
511          const IntVec& requiredIDs = finleyMesh->getNodes()->getNodeIDs();          const IntVec& requiredIDs = domain->getNodes()->getNodeIDs();
512          const IntVec& nodeGNI = finleyMesh->getNodes()->getGlobalNodeIndices();          const IntVec& nodeGNI = domain->getNodes()->getGlobalNodeIndices();
513          const IntVec& nodeDist = finleyMesh->getNodes()->getNodeDistribution();          const IntVec& nodeDist = domain->getNodes()->getNodeDistribution();
514          int firstId = nodeDist[ownIndex];          int firstId = nodeDist[ownIndex];
515          int lastId = nodeDist[ownIndex+1];          int lastId = nodeDist[ownIndex+1];
516          IndexMap sampleID2idx = buildIndexMap();          IndexMap sampleID2idx = buildIndexMap();
# Line 529  void DataVar::writeToVTK(ostream& os, in Line 523  void DataVar::writeToVTK(ostream& os, in
523      } else {      } else {
524          // cell data: ghost cells have been removed so do not write ghost          // cell data: ghost cells have been removed so do not write ghost
525          // samples (which are the last elements in the arrays)          // samples (which are the last elements in the arrays)
526          int toWrite =          int toWrite = domain->getElementsByName(meshName)->getNumElements();
             finleyMesh->getElementsByName(meshName)->getNumElements();  
527          for (int i=0; i<toWrite; i++) {          for (int i=0; i<toWrite; i++) {
528              sampleToStream(os, i);              sampleToStream(os, i);
529          }          }
# Line 587  string DataVar::getTensorDef() const Line 580  string DataVar::getTensorDef() const
580    
581  //  //
582  // Writes the data to given Silo file under the virtual path provided.  // Writes the data to given Silo file under the virtual path provided.
583  // 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().  
584  //  //
585  bool DataVar::writeToSilo(DBfile* dbfile, const string& siloPath)  bool DataVar::writeToSilo(DBfile* dbfile, const string& siloPath,
586                              const string& units)
587  {  {
588  #if USE_SILO  #if USE_SILO
589      if (!initialized)      if (!initialized)
# Line 609  bool DataVar::writeToSilo(DBfile* dbfile Line 602  bool DataVar::writeToSilo(DBfile* dbfile
602    
603      char* siloMesh = const_cast<char*>(siloMeshName.c_str());      char* siloMesh = const_cast<char*>(siloMeshName.c_str());
604      int dcenter = (centering == NODE_CENTERED ? DB_NODECENT : DB_ZONECENT);      int dcenter = (centering == NODE_CENTERED ? DB_NODECENT : DB_ZONECENT);
605        DBoptlist* optList = DBMakeOptlist(2);
606        if (units.length()>0) {
607            DBAddOption(optList, DBOPT_UNITS, (void*)units.c_str());
608        }
609    
610      if (rank == 0) {      if (rank == 0) {
611          ret = DBPutUcdvar1(dbfile, varName.c_str(), siloMesh, dataArray[0],          ret = DBPutUcdvar1(dbfile, varName.c_str(), siloMesh, dataArray[0],
612                  numSamples, NULL, 0, DB_FLOAT, dcenter, NULL);                  numSamples, NULL, 0, DB_FLOAT, dcenter, optList);
613      }      }
614      else if (rank == 1) {      else if (rank == 1) {
615          const string comps[3] = {          const string comps[3] = {
# Line 624  bool DataVar::writeToSilo(DBfile* dbfile Line 621  bool DataVar::writeToSilo(DBfile* dbfile
621    
622          ret = DBPutUcdvar(dbfile, varName.c_str(), siloMesh, shape[0],          ret = DBPutUcdvar(dbfile, varName.c_str(), siloMesh, shape[0],
623                  (char**)varnames, &dataArray[0], numSamples, NULL,                  (char**)varnames, &dataArray[0], numSamples, NULL,
624                  0, DB_FLOAT, dcenter, NULL);                  0, DB_FLOAT, dcenter, optList);
625      }      }
626      else {      else {
627          string tensorDir = varName+string("_comps/");          string tensorDir = varName+string("_comps/");
628          ret = DBMkdir(dbfile, tensorDir.c_str());          ret = DBMkdir(dbfile, tensorDir.c_str());
629          if (ret == 0) {          if (ret == 0) {
630              int one = 1;              int one = 1;
             DBoptlist* optList = DBMakeOptlist(1);  
631              DBAddOption(optList, DBOPT_HIDE_FROM_GUI, &one);              DBAddOption(optList, DBOPT_HIDE_FROM_GUI, &one);
632    
633              for (int i=0; i<shape[1]; i++) {              for (int i=0; i<shape[1]; i++) {
# Line 645  bool DataVar::writeToSilo(DBfile* dbfile Line 641  bool DataVar::writeToSilo(DBfile* dbfile
641                  }                  }
642                  if (ret != 0) break;                  if (ret != 0) break;
643              }              }
             DBFreeOptlist(optList);  
644          } // ret==0          } // ret==0
645      } // rank      } // rank
646    
647        DBFreeOptlist(optList);
648      DBSetDir(dbfile, "/");      DBSetDir(dbfile, "/");
649      return (ret == 0);      return (ret == 0);
650    
# Line 657  bool DataVar::writeToSilo(DBfile* dbfile Line 653  bool DataVar::writeToSilo(DBfile* dbfile
653  #endif  #endif
654  }  }
655    
656  } // namespace escriptexport  } // namespace weipa
657    

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

  ViewVC Help
Powered by ViewVC 1.1.26