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

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

  ViewVC Help
Powered by ViewVC 1.1.26