/[escript]/trunk/weipa/src/VisItData.cpp
ViewVC logotype

Contents of /trunk/weipa/src/VisItData.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3096 - (show annotations)
Thu Aug 19 01:40:52 2010 UTC (9 years, 11 months ago) by caltinay
File size: 11467 byte(s)
-New VisIt simulation control interface in weipa (uses VisIt's simv2)
-Rewrote restarts.py to be a more generic data manager for restarts and exports
-Removed weipa python file since the functionality is now handled by restarts.py

1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2010 by University of Queensland
5 * Earth Systems Science Computational Center (ESSCC)
6 * http://www.uq.edu.au/esscc
7 *
8 * Primary Business: Queensland, Australia
9 * Licensed under the Open Software License version 3.0
10 * http://www.opensource.org/licenses/osl-3.0.php
11 *
12 *******************************************************/
13
14 #include <weipa/VisItData.h>
15 #include <weipa/DataVar.h>
16 #include <weipa/ElementData.h>
17 #include <weipa/FinleyMesh.h>
18 #include <weipa/NodeData.h>
19
20 #include <VisItDataInterface_V2.h>
21
22 #include <boost/python/dict.hpp>
23 #include <boost/python/extract.hpp>
24
25 #include <iostream>
26 #include <map>
27 #include <numeric>
28 #include <set>
29 #include <sstream>
30
31 using escript::const_Domain_ptr;
32 using std::map;
33 using std::set;
34 using std::string;
35 using std::vector;
36
37 namespace weipa {
38
39 ///
40 /// Constructor
41 ///
42 VisItData::VisItData() :
43 mpiRank(0),
44 mpiSize(1),
45 cycle(0),
46 time(0.),
47 runFlag(false)
48 {
49 }
50
51 ///
52 /// Updates the domain and variables
53 ///
54 void VisItData::publishData(const_Domain_ptr domain,
55 const boost::python::dict& datavars)
56 {
57 dataVec.clear();
58 varNames.clear();
59 escriptDomain = domain;
60
61 #ifdef PASO_MPI
62 MPI_Comm comm = escriptDomain->getMPIComm();
63 MPI_Comm_rank(comm, &mpiRank);
64 MPI_Comm_size(comm, &mpiSize);
65 #endif
66
67 int numData = boost::python::extract<int>(datavars.attr("__len__")());
68 if (numData > 0) {
69 boost::python::list keys = datavars.keys();
70
71 for (int i=0; i<numData; i++) {
72 string varName = boost::python::extract<string>(keys[i]);
73 varNames.push_back(varName);
74 escript::Data varData = boost::python::extract<escript::Data>(
75 datavars[keys[i]]);
76 if (varData.getDomain() != domain) {
77 throw escript::DataException(
78 "publishData: all data must be on the same domain.");
79 }
80 dataVec.push_back(varData);
81 }
82 }
83 }
84
85 //
86 // Returns simulation metadata after converting the dataset
87 //
88 visit_handle VisItData::getSimMetaData()
89 {
90 visit_handle mdh = VISIT_INVALID_HANDLE;
91 VisIt_SimulationMetaData_alloc(&mdh);
92 vector<string>::const_iterator it;
93 for (it=cmdNames.begin(); it!=cmdNames.end(); it++) {
94 visit_handle cmd = VISIT_INVALID_HANDLE;
95 if (VisIt_CommandMetaData_alloc(&cmd) == VISIT_OKAY) {
96 VisIt_CommandMetaData_setName(cmd, it->c_str());
97 VisIt_SimulationMetaData_addGenericCommand(mdh, cmd);
98 }
99 }
100 VisIt_SimulationMetaData_setMode(mdh, runFlag ?
101 VISIT_SIMMODE_RUNNING : VISIT_SIMMODE_STOPPED);
102 VisIt_SimulationMetaData_setCycleTime(mdh, cycle, time);
103
104 if (escriptDomain == NULL) {
105 return mdh;
106 }
107
108 #ifdef PASO_MPI
109 MPI_Comm comm = escriptDomain->getMPIComm();
110 dataset.reset(new EscriptDataset(comm));
111 #else
112 dataset.reset(new EscriptDataset());
113 #endif
114
115 if (!dataset->initFromEscript(escriptDomain.get(), dataVec, varNames)) {
116 throw escript::DataException("weipa: error initialising dataset.");
117 }
118
119 set<string> usedMeshes;
120
121 // add "special" mesh variable metadata
122 const VarVector& meshVars = dataset->getMeshVariables();
123 VarVector::const_iterator vIt;
124 for (vIt = meshVars.begin(); vIt != meshVars.end(); vIt++) {
125 // skip empty variable
126 int numSamples = accumulate(vIt->sampleDistribution.begin(),
127 vIt->sampleDistribution.end(), 0);
128 if (numSamples > 0 && vIt->valid) {
129 DataVar_ptr readerVar = vIt->dataBlocks[0];
130 string meshName = readerVar->getMeshName();
131 usedMeshes.insert(meshName);
132 int centering = readerVar->isNodeCentered() ?
133 VISIT_VARCENTERING_NODE : VISIT_VARCENTERING_ZONE;
134 string varPath = "mesh_vars/"+vIt->varName;
135 variables[varPath] = readerVar;
136 addVariableMetadata(mdh, varPath, meshName, centering, 0);
137 }
138 }
139
140 // add other variables
141 const VarVector& vars = dataset->getVariables();
142 for (vIt = vars.begin(); vIt != vars.end(); vIt++) {
143 // skip empty variable
144 int numSamples = accumulate(vIt->sampleDistribution.begin(),
145 vIt->sampleDistribution.end(), 0);
146 if (numSamples == 0 || !vIt->valid) {
147 continue;
148 }
149
150 const string& varName = vIt->varName;
151 DataVar_ptr readerVar = vIt->dataBlocks[0];
152 string meshName = readerVar->getMeshName();
153 usedMeshes.insert(meshName);
154 int centering = readerVar->isNodeCentered() ?
155 VISIT_VARCENTERING_NODE : VISIT_VARCENTERING_ZONE;
156 int rank = readerVar->getRank();
157 addVariableMetadata(mdh, varName, meshName, centering, rank);
158 variables[varName] = readerVar;
159 }
160
161 // add all meshes
162 set<string>::const_iterator sIt;
163 int dim = dataset->getConvertedDomain()[0]->getNodes()->getNumDims();
164 for (sIt = usedMeshes.begin(); sIt != usedMeshes.end(); sIt++) {
165 addMeshMetadata(mdh, *sIt, dim, mpiSize);
166 }
167
168 return mdh;
169 }
170
171 ///
172 /// Returns the domain list
173 ///
174 visit_handle VisItData::getDomainList()
175 {
176 // each MPI rank serves exactly one domain (chunk) of the data
177 visit_handle domainList = VISIT_INVALID_HANDLE;
178 if (VisIt_DomainList_alloc(&domainList) == VISIT_OKAY) {
179 visit_handle hdl;
180 int* rank = (int*)malloc(sizeof(int));
181 *rank = mpiRank;
182 VisIt_VariableData_alloc(&hdl);
183 VisIt_VariableData_setDataI(hdl, VISIT_OWNER_VISIT, 1, 1, rank);
184 VisIt_DomainList_setDomains(domainList, mpiSize, hdl);
185 }
186 return domainList;
187 }
188
189 ///
190 /// Returns mesh data for the specified mesh
191 ///
192 visit_handle VisItData::getMesh(const char* name)
193 {
194 visit_handle hmesh = VISIT_INVALID_HANDLE;
195 FinleyMesh_ptr dom = dataset->getConvertedDomain()[0];
196 NodeData_ptr nodes = dom->getMeshByName(name);
197 ElementData_ptr elements = dom->getElementsByName(name);
198
199 if (nodes && elements
200 && VisIt_UnstructuredMesh_alloc(&hmesh)) {
201
202 // set node coordinates
203 visit_handle hx, hy;
204 VisIt_VariableData_alloc(&hx);
205 VisIt_VariableData_alloc(&hy);
206 VisIt_VariableData_setDataF(hx, VISIT_OWNER_SIM, 1,
207 nodes->getNumNodes(), nodes->getCoords()[0]);
208 VisIt_VariableData_setDataF(hy, VISIT_OWNER_SIM, 1,
209 nodes->getNumNodes(), nodes->getCoords()[1]);
210
211 if (nodes->getNumDims() > 2) {
212 visit_handle hz;
213 VisIt_VariableData_alloc(&hz);
214 VisIt_VariableData_setDataF(hz, VISIT_OWNER_SIM, 1,
215 nodes->getNumNodes(), nodes->getCoords()[2]);
216 VisIt_UnstructuredMesh_setCoordsXYZ(hmesh, hx, hy, hz);
217 } else {
218 VisIt_UnstructuredMesh_setCoordsXY(hmesh, hx, hy);
219 }
220
221 // set connectivity
222 size_t sz = (elements->getNodesPerElement()+1) *
223 elements->getNumElements() * sizeof(int);
224 int* conn = (int*)malloc(sz);
225 int* cptr = conn;
226 const int* nodeList = &elements->getNodeList()[0];
227 int type = 0;
228 memset(conn, 0, sz);
229 switch (elements->getType()) {
230 case weipa::ZONETYPE_BEAM: type = VISIT_CELL_BEAM; break;
231 case weipa::ZONETYPE_HEX: type = VISIT_CELL_HEX; break;
232 case weipa::ZONETYPE_POLYGON: type = VISIT_CELL_POINT; break;
233 case weipa::ZONETYPE_QUAD: type = VISIT_CELL_QUAD; break;
234 case weipa::ZONETYPE_TET: type = VISIT_CELL_TET; break;
235 case weipa::ZONETYPE_TRIANGLE: type = VISIT_CELL_TRI; break;
236 default:
237 std::cerr << "Unhandled zone type " << elements->getType()
238 << std::endl;
239 break;
240 }
241 for (size_t i=0; i<elements->getNumElements(); i++) {
242 *cptr++ = type;
243 for (size_t j=0; j<elements->getNodesPerElement(); j++) {
244 *cptr++ = *nodeList++;
245 }
246 }
247
248 visit_handle hc;
249 VisIt_VariableData_alloc(&hc);
250 VisIt_VariableData_setDataI(hc, VISIT_OWNER_VISIT, 1,
251 sz / sizeof(int), conn);
252 VisIt_UnstructuredMesh_setConnectivity(hmesh,
253 elements->getNumElements(), hc);
254
255 // set ghost information
256 VisIt_UnstructuredMesh_setRealIndices(hmesh, 0,
257 elements->getNumElements() - elements->getGhostCount());
258 }
259
260 return hmesh;
261 }
262
263 ///
264 /// Returns variable data for the specified variable
265 ///
266 visit_handle VisItData::getVariable(const char* name)
267 {
268 visit_handle hvar = VISIT_INVALID_HANDLE;
269 map<string, DataVar_ptr>::const_iterator it;
270 it = variables.find(name);
271 if (it != variables.end()) {
272 DataVar_ptr var = it->second;
273 VisIt_VariableData_alloc(&hvar);
274 VisIt_VariableData_setDataF(hvar, VISIT_OWNER_VISIT,
275 var->getNumberOfComponents(),
276 var->getNumberOfSamples(),
277 var->getDataFlat());
278 }
279
280 return hvar;
281 }
282
283 //
284 // Sets the list of available command names
285 //
286 void VisItData::setCommandNames(vector<string> commandNames)
287 {
288 cmdNames = commandNames;
289 }
290
291 //
292 // Sets the simulation status
293 //
294 void VisItData::setSimulationStatus(bool running, double t, int c)
295 {
296 runFlag = running;
297 time = t;
298 cycle = c;
299 }
300
301 ///
302 /// Adds an expression to the simulation metadata smd
303 ///
304 void VisItData::addExpressionMetadata(visit_handle smd, const string& name,
305 const string& def, int type)
306 {
307 visit_handle emd;
308 if (VisIt_ExpressionMetaData_alloc(&emd) == VISIT_OKAY) {
309 VisIt_ExpressionMetaData_setName(emd, name.c_str());
310 VisIt_ExpressionMetaData_setDefinition(emd, def.c_str());
311 VisIt_ExpressionMetaData_setType(emd, type);
312 VisIt_SimulationMetaData_addExpression(smd, emd);
313 }
314 }
315
316 ///
317 /// Adds a mesh to the simulation metadata smd
318 ///
319 void VisItData::addMeshMetadata(visit_handle smd, const string& name, int dim,
320 int numDoms)
321 {
322 visit_handle mmd;
323 if (VisIt_MeshMetaData_alloc(&mmd) == VISIT_OKAY) {
324 VisIt_MeshMetaData_setName(mmd, name.c_str());
325 VisIt_MeshMetaData_setMeshType(mmd, VISIT_MESHTYPE_UNSTRUCTURED);
326 VisIt_MeshMetaData_setTopologicalDimension(mmd, dim);
327 VisIt_MeshMetaData_setSpatialDimension(mmd, dim);
328 VisIt_MeshMetaData_setNumDomains(mmd, numDoms);
329 VisIt_SimulationMetaData_addMesh(smd, mmd);
330 }
331 }
332
333 ///
334 /// Adds a variable to the simulation metadata smd
335 ///
336 void VisItData::addVariableMetadata(visit_handle smd, const string& name,
337 const string& meshName, int centering,
338 int rank)
339 {
340 visit_handle vmd;
341 if (VisIt_VariableMetaData_alloc(&vmd) == VISIT_OKAY) {
342 VisIt_VariableMetaData_setName(vmd, name.c_str());
343 VisIt_VariableMetaData_setMeshName(vmd, meshName.c_str());
344 if (rank == 0) {
345 VisIt_VariableMetaData_setType(vmd, VISIT_VARTYPE_SCALAR);
346 } else if (rank == 1) {
347 VisIt_VariableMetaData_setType(vmd, VISIT_VARTYPE_VECTOR);
348 } else {
349 VisIt_VariableMetaData_setType(vmd, VISIT_VARTYPE_TENSOR);
350 }
351 VisIt_VariableMetaData_setCentering(vmd, centering);
352 VisIt_SimulationMetaData_addVariable(smd, vmd);
353 }
354 }
355
356 } // namespace weipa
357

  ViewVC Help
Powered by ViewVC 1.1.26