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

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

  ViewVC Help
Powered by ViewVC 1.1.26