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/NodeData.h> |
18 |
|
19 |
#include <VisItDataInterface_V2.h> |
20 |
|
21 |
#include <boost/python/dict.hpp> |
22 |
#include <boost/python/extract.hpp> |
23 |
|
24 |
#include <iostream> |
25 |
#include <map> |
26 |
#include <numeric> |
27 |
#include <set> |
28 |
#include <sstream> |
29 |
|
30 |
using std::map; |
31 |
using std::set; |
32 |
using std::string; |
33 |
using std::vector; |
34 |
|
35 |
namespace weipa { |
36 |
|
37 |
// |
38 |
// Returns simulation metadata from this dataset. |
39 |
// |
40 |
visit_handle VisItData::getSimMetaData() |
41 |
{ |
42 |
visit_handle mdh = VISIT_INVALID_HANDLE; |
43 |
VisIt_SimulationMetaData_alloc(&mdh); |
44 |
vector<string>::const_iterator it; |
45 |
for (it=cmdNames.begin(); it!=cmdNames.end(); it++) { |
46 |
visit_handle cmd = VISIT_INVALID_HANDLE; |
47 |
if (VisIt_CommandMetaData_alloc(&cmd) == VISIT_OKAY) { |
48 |
VisIt_CommandMetaData_setName(cmd, it->c_str()); |
49 |
VisIt_SimulationMetaData_addGenericCommand(mdh, cmd); |
50 |
} |
51 |
} |
52 |
VisIt_SimulationMetaData_setMode(mdh, runFlag ? |
53 |
VISIT_SIMMODE_RUNNING : VISIT_SIMMODE_STOPPED); |
54 |
|
55 |
if (dataset.get() == NULL) { |
56 |
return mdh; |
57 |
} |
58 |
|
59 |
VisIt_SimulationMetaData_setCycleTime( |
60 |
mdh, dataset->getCycle(), dataset->getTime()); |
61 |
|
62 |
set<string> usedMeshes; |
63 |
|
64 |
// add "special" mesh variable metadata |
65 |
const VarVector& meshVars = dataset->getMeshVariables(); |
66 |
VarVector::const_iterator vIt; |
67 |
for (vIt = meshVars.begin(); vIt != meshVars.end(); vIt++) { |
68 |
// skip empty variable |
69 |
int numSamples = accumulate(vIt->sampleDistribution.begin(), |
70 |
vIt->sampleDistribution.end(), 0); |
71 |
if (numSamples > 0 && vIt->valid) { |
72 |
DataVar_ptr readerVar = vIt->dataChunks[0]; |
73 |
string meshName = readerVar->getMeshName(); |
74 |
usedMeshes.insert(meshName); |
75 |
int centering = readerVar->isNodeCentered() ? |
76 |
VISIT_VARCENTERING_NODE : VISIT_VARCENTERING_ZONE; |
77 |
string varPath = "mesh_vars/"+vIt->varName; |
78 |
variables[varPath] = readerVar; |
79 |
addVariableMetadata(mdh, varPath, meshName, centering, 0); |
80 |
} |
81 |
} |
82 |
|
83 |
// add other variables |
84 |
const VarVector& vars = dataset->getVariables(); |
85 |
for (vIt = vars.begin(); vIt != vars.end(); vIt++) { |
86 |
// skip empty variable |
87 |
int numSamples = accumulate(vIt->sampleDistribution.begin(), |
88 |
vIt->sampleDistribution.end(), 0); |
89 |
if (numSamples == 0 || !vIt->valid) { |
90 |
continue; |
91 |
} |
92 |
|
93 |
const string& varName = vIt->varName; |
94 |
DataVar_ptr readerVar = vIt->dataChunks[0]; |
95 |
string meshName = readerVar->getMeshName(); |
96 |
usedMeshes.insert(meshName); |
97 |
int centering = readerVar->isNodeCentered() ? |
98 |
VISIT_VARCENTERING_NODE : VISIT_VARCENTERING_ZONE; |
99 |
int rank = readerVar->getRank(); |
100 |
addVariableMetadata(mdh, varName, meshName, centering, rank); |
101 |
variables[varName] = readerVar; |
102 |
} |
103 |
|
104 |
// add all meshes |
105 |
int mpiSize=1; |
106 |
#ifdef HAVE_MPI |
107 |
MPI_Comm comm = dataset->getMPIComm(); |
108 |
MPI_Comm_size(comm, &mpiSize); |
109 |
#endif |
110 |
set<string>::const_iterator sIt; |
111 |
int dim = dataset->getConvertedDomain()[0]->getNodes()->getNumDims(); |
112 |
for (sIt = usedMeshes.begin(); sIt != usedMeshes.end(); sIt++) { |
113 |
addMeshMetadata(mdh, *sIt, dim, mpiSize); |
114 |
} |
115 |
|
116 |
return mdh; |
117 |
} |
118 |
|
119 |
/// |
120 |
/// Returns the domain list |
121 |
/// |
122 |
visit_handle VisItData::getDomainList() |
123 |
{ |
124 |
// each MPI rank serves exactly one domain (chunk) of the data |
125 |
visit_handle domainList = VISIT_INVALID_HANDLE; |
126 |
if (VisIt_DomainList_alloc(&domainList) == VISIT_OKAY) { |
127 |
int mpiRank=0, mpiSize=1; |
128 |
#ifdef HAVE_MPI |
129 |
MPI_Comm comm = dataset->getMPIComm(); |
130 |
MPI_Comm_rank(comm, &mpiRank); |
131 |
MPI_Comm_size(comm, &mpiSize); |
132 |
#endif |
133 |
visit_handle hdl; |
134 |
int* rank = (int*)malloc(sizeof(int)); |
135 |
*rank = mpiRank; |
136 |
VisIt_VariableData_alloc(&hdl); |
137 |
VisIt_VariableData_setDataI(hdl, VISIT_OWNER_VISIT, 1, 1, rank); |
138 |
VisIt_DomainList_setDomains(domainList, mpiSize, hdl); |
139 |
} |
140 |
return domainList; |
141 |
} |
142 |
|
143 |
/// |
144 |
/// Returns mesh data for the specified mesh |
145 |
/// |
146 |
visit_handle VisItData::getMesh(const char* name) |
147 |
{ |
148 |
visit_handle hmesh = VISIT_INVALID_HANDLE; |
149 |
DomainChunk_ptr dom = dataset->getConvertedDomain()[0]; |
150 |
NodeData_ptr nodes = dom->getMeshByName(name); |
151 |
ElementData_ptr elements = dom->getElementsByName(name); |
152 |
|
153 |
if (nodes && elements |
154 |
&& VisIt_UnstructuredMesh_alloc(&hmesh)) { |
155 |
|
156 |
// set node coordinates |
157 |
visit_handle hx, hy; |
158 |
VisIt_VariableData_alloc(&hx); |
159 |
VisIt_VariableData_alloc(&hy); |
160 |
VisIt_VariableData_setDataF(hx, VISIT_OWNER_SIM, 1, |
161 |
nodes->getNumNodes(), nodes->getCoords()[0]); |
162 |
VisIt_VariableData_setDataF(hy, VISIT_OWNER_SIM, 1, |
163 |
nodes->getNumNodes(), nodes->getCoords()[1]); |
164 |
|
165 |
if (nodes->getNumDims() > 2) { |
166 |
visit_handle hz; |
167 |
VisIt_VariableData_alloc(&hz); |
168 |
VisIt_VariableData_setDataF(hz, VISIT_OWNER_SIM, 1, |
169 |
nodes->getNumNodes(), nodes->getCoords()[2]); |
170 |
VisIt_UnstructuredMesh_setCoordsXYZ(hmesh, hx, hy, hz); |
171 |
} else { |
172 |
VisIt_UnstructuredMesh_setCoordsXY(hmesh, hx, hy); |
173 |
} |
174 |
|
175 |
// set connectivity |
176 |
size_t sz = (elements->getNodesPerElement()+1) * |
177 |
elements->getNumElements() * sizeof(int); |
178 |
int* conn = (int*)malloc(sz); |
179 |
int* cptr = conn; |
180 |
const int* nodeList = &elements->getNodeList()[0]; |
181 |
int type = 0; |
182 |
memset(conn, 0, sz); |
183 |
switch (elements->getType()) { |
184 |
case weipa::ZONETYPE_BEAM: type = VISIT_CELL_BEAM; break; |
185 |
case weipa::ZONETYPE_HEX: type = VISIT_CELL_HEX; break; |
186 |
case weipa::ZONETYPE_POLYGON: type = VISIT_CELL_POINT; break; |
187 |
case weipa::ZONETYPE_QUAD: type = VISIT_CELL_QUAD; break; |
188 |
case weipa::ZONETYPE_TET: type = VISIT_CELL_TET; break; |
189 |
case weipa::ZONETYPE_TRIANGLE: type = VISIT_CELL_TRI; break; |
190 |
default: |
191 |
std::cerr << "Unhandled zone type " << elements->getType() |
192 |
<< std::endl; |
193 |
break; |
194 |
} |
195 |
for (size_t i=0; i<elements->getNumElements(); i++) { |
196 |
*cptr++ = type; |
197 |
for (size_t j=0; j<elements->getNodesPerElement(); j++) { |
198 |
*cptr++ = *nodeList++; |
199 |
} |
200 |
} |
201 |
|
202 |
visit_handle hc; |
203 |
VisIt_VariableData_alloc(&hc); |
204 |
VisIt_VariableData_setDataI(hc, VISIT_OWNER_VISIT, 1, |
205 |
sz / sizeof(int), conn); |
206 |
VisIt_UnstructuredMesh_setConnectivity(hmesh, |
207 |
elements->getNumElements(), hc); |
208 |
|
209 |
// set ghost information |
210 |
VisIt_UnstructuredMesh_setRealIndices(hmesh, 0, |
211 |
elements->getNumElements()-elements->getGhostCount()-1); |
212 |
} |
213 |
|
214 |
return hmesh; |
215 |
} |
216 |
|
217 |
/// |
218 |
/// Returns variable data for the specified variable |
219 |
/// |
220 |
visit_handle VisItData::getVariable(const char* name) |
221 |
{ |
222 |
visit_handle hvar = VISIT_INVALID_HANDLE; |
223 |
map<string, DataVar_ptr>::const_iterator it; |
224 |
it = variables.find(name); |
225 |
if (it != variables.end()) { |
226 |
DataVar_ptr var = it->second; |
227 |
VisIt_VariableData_alloc(&hvar); |
228 |
VisIt_VariableData_setDataF(hvar, VISIT_OWNER_VISIT, |
229 |
var->getNumberOfComponents(), |
230 |
var->getNumberOfSamples(), |
231 |
var->getDataFlat()); |
232 |
} |
233 |
|
234 |
return hvar; |
235 |
} |
236 |
|
237 |
/// |
238 |
/// Adds an expression to the simulation metadata smd |
239 |
/// |
240 |
void VisItData::addExpressionMetadata(visit_handle smd, const string& name, |
241 |
const string& def, int type) |
242 |
{ |
243 |
visit_handle emd; |
244 |
if (VisIt_ExpressionMetaData_alloc(&emd) == VISIT_OKAY) { |
245 |
VisIt_ExpressionMetaData_setName(emd, name.c_str()); |
246 |
VisIt_ExpressionMetaData_setDefinition(emd, def.c_str()); |
247 |
VisIt_ExpressionMetaData_setType(emd, type); |
248 |
VisIt_SimulationMetaData_addExpression(smd, emd); |
249 |
} |
250 |
} |
251 |
|
252 |
/// |
253 |
/// Adds a mesh to the simulation metadata smd |
254 |
/// |
255 |
void VisItData::addMeshMetadata(visit_handle smd, const string& name, int dim, |
256 |
int numDoms) |
257 |
{ |
258 |
visit_handle mmd; |
259 |
if (VisIt_MeshMetaData_alloc(&mmd) == VISIT_OKAY) { |
260 |
VisIt_MeshMetaData_setName(mmd, name.c_str()); |
261 |
VisIt_MeshMetaData_setMeshType(mmd, VISIT_MESHTYPE_UNSTRUCTURED); |
262 |
VisIt_MeshMetaData_setTopologicalDimension(mmd, dim); |
263 |
VisIt_MeshMetaData_setSpatialDimension(mmd, dim); |
264 |
VisIt_MeshMetaData_setNumDomains(mmd, numDoms); |
265 |
VisIt_MeshMetaData_setDomainTitle(mmd, "domains"); |
266 |
VisIt_SimulationMetaData_addMesh(smd, mmd); |
267 |
} |
268 |
} |
269 |
|
270 |
/// |
271 |
/// Adds a variable to the simulation metadata smd |
272 |
/// |
273 |
void VisItData::addVariableMetadata(visit_handle smd, const string& name, |
274 |
const string& meshName, int centering, |
275 |
int rank) |
276 |
{ |
277 |
visit_handle vmd; |
278 |
if (VisIt_VariableMetaData_alloc(&vmd) == VISIT_OKAY) { |
279 |
VisIt_VariableMetaData_setName(vmd, name.c_str()); |
280 |
VisIt_VariableMetaData_setMeshName(vmd, meshName.c_str()); |
281 |
if (rank == 0) { |
282 |
VisIt_VariableMetaData_setType(vmd, VISIT_VARTYPE_SCALAR); |
283 |
} else if (rank == 1) { |
284 |
VisIt_VariableMetaData_setType(vmd, VISIT_VARTYPE_VECTOR); |
285 |
} else { |
286 |
VisIt_VariableMetaData_setType(vmd, VISIT_VARTYPE_TENSOR); |
287 |
} |
288 |
VisIt_VariableMetaData_setCentering(vmd, centering); |
289 |
VisIt_SimulationMetaData_addVariable(smd, vmd); |
290 |
} |
291 |
} |
292 |
|
293 |
} // namespace weipa |
294 |
|