/[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 3911 by jfenwick, Thu Jun 14 01:01:03 2012 UTC
# Line 1  Line 1 
1    
2  /*******************************************************  /*******************************************************
3  *  *
4  * Copyright (c) 2003-2010 by University of Queensland  * Copyright (c) 2003-2012 by University of Queensland
5  * Earth Systems Science Computational Center (ESSCC)  * Earth Systems Science Computational Center (ESSCC)
6  * http://www.uq.edu.au/esscc  * http://www.uq.edu.au/esscc
7  *  *
# 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 144  bool DataVar::initFromEscript(escript::D Line 139  bool DataVar::initFromEscript(escript::D
139          initialized = false;          initialized = false;
140      }      }
141    
142        // special case: shape=(1,) or shape=(1,1) -> convert to scalar
143        if (dimSize==1 && rank>0) {
144            rank=0;
145            shape.clear();
146        }
147    
148      if (initialized) {      if (initialized) {
149          size_t dataSize = dimSize * ptsPerSample;          size_t dataSize = dimSize * ptsPerSample;
150          float* tempData = new float[dataSize*numSamples];          float* tempData = new float[dataSize*numSamples];
151          float* destPtr = tempData;          float* destPtr = tempData;
152          for (int sampleNo=0; sampleNo<numSamples; sampleNo++) {          if (escriptData.isConstant()) {
153              const escript::DataAbstract::ValueType::value_type* values =              const escript::DataAbstract::ValueType::value_type* values =
154                  escriptData.getSampleDataRO(sampleNo);                  escriptData.getSampleDataRO(0);
155              copy(values, values+dataSize, destPtr);              for (int pointNo=0; pointNo<numSamples*ptsPerSample; pointNo++) {
156              destPtr += dataSize;                  copy(values, values+dimSize, destPtr);
157                    destPtr += dimSize;
158                }
159            } else {
160                for (int sampleNo=0; sampleNo<numSamples; sampleNo++) {
161                    const escript::DataAbstract::ValueType::value_type* values =
162                        escriptData.getSampleDataRO(sampleNo);
163                    copy(values, values+dataSize, destPtr);
164                    destPtr += dataSize;
165                }
166          }          }
167    
168          const float* srcPtr = tempData;          const float* srcPtr = tempData;
# Line 173  bool DataVar::initFromEscript(escript::D Line 183  bool DataVar::initFromEscript(escript::D
183  }  }
184    
185  //  //
186  // Initialise with mesh data  // Initialise with domain data
187  //  //
188  bool DataVar::initFromMesh(FinleyMesh_ptr mesh)  bool DataVar::initFromMeshData(const_DomainChunk_ptr dom, const IntVec& data,
189            int fsCode, Centering c, NodeData_ptr nodes, const IntVec& id)
190  {  {
191      cleanup();      cleanup();
192            
193      finleyMesh = mesh;      domain = dom;
194      rank = 0;      rank = 0;
195      ptsPerSample = 1;      ptsPerSample = 1;
196      NodeData_ptr nodes;      centering = c;
197        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;  
     }  
   
198      meshName = nodes->getName();      meshName = nodes->getName();
199      siloMeshName = nodes->getFullSiloName();      siloMeshName = nodes->getFullSiloName();
   
     const IntVec& data = mesh->getVarDataByName(varName);  
200      numSamples = data.size();      numSamples = data.size();
201    
202      if (numSamples > 0) {      if (numSamples > 0) {
# Line 234  bool DataVar::initFromMesh(FinleyMesh_pt Line 212  bool DataVar::initFromMesh(FinleyMesh_pt
212  }  }
213    
214  //  //
215  // Reads variable data from NetCDF file  // Reads variable data from dump file
216  //  //
217  bool DataVar::initFromNetCDF(const string& filename, FinleyMesh_ptr mesh)  bool DataVar::initFromFile(const string& filename, const_DomainChunk_ptr dom)
218  {  {
219      cleanup();      cleanup();
220            
# Line 269  bool DataVar::initFromNetCDF(const strin Line 247  bool DataVar::initFromNetCDF(const strin
247      att = input->get_att("function_space_type");      att = input->get_att("function_space_type");
248      funcSpace = att->as_int(0);      funcSpace = att->as_int(0);
249    
250      if (funcSpace == FINLEY_REDUCED_NODES || funcSpace == FINLEY_NODES) {      centering = dom->getCenteringForFunctionSpace(funcSpace);
         centering = NODE_CENTERED;  
     } else {  
         centering = ZONE_CENTERED;  
     }  
251    
252      dim = input->get_dim("num_samples");      dim = input->get_dim("num_samples");
253      numSamples = dim->size();      numSamples = dim->size();
# Line 283  bool DataVar::initFromNetCDF(const strin Line 257  bool DataVar::initFromNetCDF(const strin
257          << ptsPerSample << " pts/s,  rank: " << rank << endl;          << ptsPerSample << " pts/s,  rank: " << rank << endl;
258  #endif  #endif
259    
260      finleyMesh = mesh;      domain = dom;
261      NodeData_ptr nodes = finleyMesh->getMeshForFinleyFS(funcSpace);      NodeData_ptr nodes = domain->getMeshForFunctionSpace(funcSpace);
262      if (nodes == NULL) {      if (nodes == NULL) {
263          delete input;          delete input;
264          return false;          return false;
# Line 368  float* DataVar::averageData(const float* Line 342  float* DataVar::averageData(const float*
342          for (int i=0; i<numSamples; i++, src+=stride)          for (int i=0; i<numSamples; i++, src+=stride)
343              *dest++ = *src;              *dest++ = *src;
344      } else {      } else {
345          ElementData_ptr cells = finleyMesh->getElementsForFinleyFS(funcSpace);          ElementData_ptr cells = domain->getElementsForFunctionSpace(funcSpace);
346          int cellFactor = cells->getElementFactor();          int cellFactor = cells->getElementFactor();
347          res = new float[cellFactor * numSamples];          res = new float[cellFactor * numSamples];
348          float* dest = res;          float* dest = res;
349          QuadMaskInfo qmi = cells->getQuadMask(funcSpace);          QuadMaskInfo qmi = cells->getQuadMask(funcSpace);
350          if (qmi.mask.size() > 0) {          if (!qmi.mask.empty()) {
351              const float* tmpSrc = src;              const float* tmpSrc = src;
352              for (int i=0; i<numSamples; i++, tmpSrc+=stride*ptsPerSample) {              for (int i=0; i<numSamples; i++, tmpSrc+=stride*ptsPerSample) {
353                  for (int l=0; l<cellFactor; l++) {                  for (int l=0; l<cellFactor; l++) {
# Line 417  bool DataVar::reorderSamples() Line 391  bool DataVar::reorderSamples()
391      int cellFactor = 1;      int cellFactor = 1;
392    
393      if (centering == NODE_CENTERED) {      if (centering == NODE_CENTERED) {
394          NodeData_ptr nodes = finleyMesh->getMeshForFinleyFS(funcSpace);          NodeData_ptr nodes = domain->getMeshForFunctionSpace(funcSpace);
395          requiredIDs = &nodes->getNodeIDs();          requiredIDs = &nodes->getNodeIDs();
396          requiredNumSamples = nodes->getNumNodes();          requiredNumSamples = nodes->getNumNodes();
397      } else {      } else {
398          ElementData_ptr cells = finleyMesh->getElementsForFinleyFS(funcSpace);          ElementData_ptr cells = domain->getElementsForFunctionSpace(funcSpace);
399          if (cells == NULL)          if (cells == NULL)
400              return false;              return false;
401    
# Line 473  bool DataVar::reorderSamples() Line 447  bool DataVar::reorderSamples()
447  //  //
448  //  //
449  //  //
450    int DataVar::getNumberOfComponents() const
451    {
452        return (rank == 0 ? 1 : accumulate(shape.begin(), shape.end(), 0));
453    }
454    
455    //
456    //
457    //
458    float* DataVar::getDataFlat() const
459    {
460        int totalSize = numSamples * getNumberOfComponents();
461        float* res = new float[totalSize];
462        if (rank == 0) {
463            copy(dataArray[0], dataArray[0]+numSamples, res);
464        } else if (rank == 1) {
465            float *dest = res;
466            for (size_t c=0; c<numSamples; c++) {
467                for (size_t i=0; i<shape[0]; i++) {
468                    *dest++ = dataArray[i][c];
469                }
470            }
471        } else if (rank == 2) {
472            float *dest = res;
473            for (size_t c=0; c<numSamples; c++) {
474                for (int i=0; i<shape[1]; i++) {
475                    for (int j=0; j<shape[0]; j++) {
476                        *dest++ = dataArray[i*shape[0]+j][c];
477                    }
478                }
479            }
480        }
481    
482        return res;
483    }
484    
485    //
486    //
487    //
488  void DataVar::sampleToStream(ostream& os, int index)  void DataVar::sampleToStream(ostream& os, int index)
489  {  {
490        // index is -1 for dummy samples, i.e. if writing the full mesh but
491        // only a reduced number of samples is required
492      if (rank == 0) {      if (rank == 0) {
493          os << dataArray[0][index];          if (index < 0) {
494                os << 0.;
495            } else {
496                os << dataArray[0][index];
497            }
498      } else if (rank == 1) {      } else if (rank == 1) {
499          if (shape[0] < 3)          if (index < 0) {
500                os << 0. << " " << 0.  << " " << 0.;
501            } else if (shape[0] < 3) {
502              os << dataArray[0][index] << " " << dataArray[1][index]              os << dataArray[0][index] << " " << dataArray[1][index]
503                  << " " << 0.;                  << " " << 0.;
504          else          } else {
505              os << dataArray[0][index] << " " << dataArray[1][index]              os << dataArray[0][index] << " " << dataArray[1][index]
506                  << " " << dataArray[2][index];                  << " " << dataArray[2][index];
507            }
508      } else if (rank == 2) {      } else if (rank == 2) {
509          if (shape[1] < 3) {          if (index < 0) {
510                os << 0. << " " << 0. << " " << 0. << " ";
511                os << 0. << " " << 0. << " " << 0. << " ";
512                os << 0. << " " << 0. << " " << 0.;
513            } else if (shape[1] < 3) {
514              os << dataArray[0][index] << " " << dataArray[1][index]              os << dataArray[0][index] << " " << dataArray[1][index]
515                  << " " << 0. << " ";                  << " " << 0. << " ";
516              os << dataArray[2][index] << " " << dataArray[3][index]              os << dataArray[2][index] << " " << dataArray[3][index]
# Line 513  void DataVar::writeToVTK(ostream& os, in Line 538  void DataVar::writeToVTK(ostream& os, in
538    
539      if (isNodeCentered()) {      if (isNodeCentered()) {
540          // data was reordered in reorderSamples() but for VTK we write the          // data was reordered in reorderSamples() but for VTK we write the
541          // original node mesh and thus need the original ordering...          // original node mesh and thus need the original ordering.
542          const IntVec& requiredIDs = finleyMesh->getNodes()->getNodeIDs();          // Note, that this also means we may not have samples for all nodes
543          const IntVec& nodeGNI = finleyMesh->getNodes()->getGlobalNodeIndices();          // in which case we set idx to -1 and write a dummy sample
544          const IntVec& nodeDist = finleyMesh->getNodes()->getNodeDistribution();          const IntVec& requiredIDs = domain->getNodes()->getNodeIDs();
545            const IntVec& nodeGNI = domain->getNodes()->getGlobalNodeIndices();
546            const IntVec& nodeDist = domain->getNodes()->getNodeDistribution();
547          int firstId = nodeDist[ownIndex];          int firstId = nodeDist[ownIndex];
548          int lastId = nodeDist[ownIndex+1];          int lastId = nodeDist[ownIndex+1];
549          IndexMap sampleID2idx = buildIndexMap();          IndexMap sampleID2idx = buildIndexMap();
550          for (int i=0; i<nodeGNI.size(); i++) {          for (int i=0; i<nodeGNI.size(); i++) {
551              if (firstId <= nodeGNI[i] && nodeGNI[i] < lastId) {              if (firstId <= nodeGNI[i] && nodeGNI[i] < lastId) {
552                  int idx = sampleID2idx[requiredIDs[i]];                  IndexMap::const_iterator it = sampleID2idx.find(requiredIDs[i]);
553                    int idx = (it==sampleID2idx.end() ? -1 : (int)it->second);
554                  sampleToStream(os, idx);                  sampleToStream(os, idx);
555              }              }
556          }          }
557      } else {      } else {
558          // cell data: ghost cells have been removed so do not write ghost          // cell data: ghost cells have been removed so do not write ghost
559          // samples (which are the last elements in the arrays)          // samples (which are the last elements in the arrays)
560          int toWrite =          int toWrite = domain->getElementsByName(meshName)->getNumElements();
             finleyMesh->getElementsByName(meshName)->getNumElements();  
561          for (int i=0; i<toWrite; i++) {          for (int i=0; i<toWrite; i++) {
562              sampleToStream(os, i);              sampleToStream(os, i);
563          }          }
# Line 587  string DataVar::getTensorDef() const Line 614  string DataVar::getTensorDef() const
614    
615  //  //
616  // Writes the data to given Silo file under the virtual path provided.  // Writes the data to given Silo file under the virtual path provided.
617  // 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().  
618  //  //
619  bool DataVar::writeToSilo(DBfile* dbfile, const string& siloPath)  bool DataVar::writeToSilo(DBfile* dbfile, const string& siloPath,
620                              const string& units)
621  {  {
622  #if USE_SILO  #if USE_SILO
623      if (!initialized)      if (!initialized)
# Line 609  bool DataVar::writeToSilo(DBfile* dbfile Line 636  bool DataVar::writeToSilo(DBfile* dbfile
636    
637      char* siloMesh = const_cast<char*>(siloMeshName.c_str());      char* siloMesh = const_cast<char*>(siloMeshName.c_str());
638      int dcenter = (centering == NODE_CENTERED ? DB_NODECENT : DB_ZONECENT);      int dcenter = (centering == NODE_CENTERED ? DB_NODECENT : DB_ZONECENT);
639        DBoptlist* optList = DBMakeOptlist(2);
640        if (units.length()>0) {
641            DBAddOption(optList, DBOPT_UNITS, (void*)units.c_str());
642        }
643    
644      if (rank == 0) {      if (rank == 0) {
645          ret = DBPutUcdvar1(dbfile, varName.c_str(), siloMesh, dataArray[0],          ret = DBPutUcdvar1(dbfile, varName.c_str(), siloMesh, dataArray[0],
646                  numSamples, NULL, 0, DB_FLOAT, dcenter, NULL);                  numSamples, NULL, 0, DB_FLOAT, dcenter, optList);
647      }      }
648      else if (rank == 1) {      else if (rank == 1) {
649          const string comps[3] = {          const string comps[3] = {
# Line 624  bool DataVar::writeToSilo(DBfile* dbfile Line 655  bool DataVar::writeToSilo(DBfile* dbfile
655    
656          ret = DBPutUcdvar(dbfile, varName.c_str(), siloMesh, shape[0],          ret = DBPutUcdvar(dbfile, varName.c_str(), siloMesh, shape[0],
657                  (char**)varnames, &dataArray[0], numSamples, NULL,                  (char**)varnames, &dataArray[0], numSamples, NULL,
658                  0, DB_FLOAT, dcenter, NULL);                  0, DB_FLOAT, dcenter, optList);
659      }      }
660      else {      else {
661          string tensorDir = varName+string("_comps/");          string tensorDir = varName+string("_comps/");
662          ret = DBMkdir(dbfile, tensorDir.c_str());          ret = DBMkdir(dbfile, tensorDir.c_str());
663          if (ret == 0) {          if (ret == 0) {
664              int one = 1;              int one = 1;
             DBoptlist* optList = DBMakeOptlist(1);  
665              DBAddOption(optList, DBOPT_HIDE_FROM_GUI, &one);              DBAddOption(optList, DBOPT_HIDE_FROM_GUI, &one);
666    
667              for (int i=0; i<shape[1]; i++) {              for (int i=0; i<shape[1]; i++) {
# Line 645  bool DataVar::writeToSilo(DBfile* dbfile Line 675  bool DataVar::writeToSilo(DBfile* dbfile
675                  }                  }
676                  if (ret != 0) break;                  if (ret != 0) break;
677              }              }
             DBFreeOptlist(optList);  
678          } // ret==0          } // ret==0
679      } // rank      } // rank
680    
681        DBFreeOptlist(optList);
682      DBSetDir(dbfile, "/");      DBSetDir(dbfile, "/");
683      return (ret == 0);      return (ret == 0);
684    
# Line 657  bool DataVar::writeToSilo(DBfile* dbfile Line 687  bool DataVar::writeToSilo(DBfile* dbfile
687  #endif  #endif
688  }  }
689    
690  } // namespace escriptexport  } // namespace weipa
691    

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

  ViewVC Help
Powered by ViewVC 1.1.26