/[escript]/trunk/dataexporter/src/NodeData.cpp
ViewVC logotype

Contents of /trunk/dataexporter/src/NodeData.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2910 - (show annotations)
Wed Feb 3 03:22:31 2010 UTC (9 years, 6 months ago) by caltinay
File size: 10033 byte(s)
Added a libescriptreader.a target which does not depend on escript or finley
at runtime. This will be used for the VisIt plugin so is not built by default.

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 <escriptexport/NodeData.h>
15
16 #ifndef VISIT_PLUGIN
17 extern "C" {
18 #include <finley/Mesh.h>
19 #include <finley/NodeFile.h>
20 }
21 #endif
22
23 #if USE_NETCDF
24 #include <netcdfcpp.h>
25 #endif
26
27 #if USE_SILO
28 #include <silo.h>
29 #endif
30
31 using namespace std;
32
33 namespace escriptexport {
34
35 //
36 // Constructor with name
37 //
38 NodeData::NodeData(const string& meshName) :
39 numDims(0), numNodes(0), name(meshName)
40 {
41 }
42
43 //
44 //
45 //
46 NodeData::NodeData(NodeData_ptr fullNodes, IntVec& requiredNodes,
47 const string& meshName) :
48 name(meshName)
49 {
50 numDims = fullNodes->numDims;
51 nodeDist = fullNodes->nodeDist;
52
53 // first: find the unique set of required nodes and their IDs while
54 // updating the contents of requiredNodes at the same time
55 // requiredNodes contains node indices (not IDs!)
56 IntVec::iterator it;
57 IndexMap indexMap; // maps old index to new index
58 size_t newIndex = 0;
59
60 for (it = requiredNodes.begin(); it != requiredNodes.end(); it++) {
61 IndexMap::iterator res = indexMap.find(*it);
62 if (res == indexMap.end()) {
63 nodeID.push_back(fullNodes->nodeID[*it]);
64 nodeTag.push_back(fullNodes->nodeTag[*it]);
65 nodeGDOF.push_back(fullNodes->nodeGDOF[*it]);
66 nodeGNI.push_back(fullNodes->nodeGNI[*it]);
67 nodeGRDFI.push_back(fullNodes->nodeGRDFI[*it]);
68 nodeGRNI.push_back(fullNodes->nodeGRNI[*it]);
69 indexMap[*it] = newIndex;
70 *it = newIndex++;
71 } else {
72 *it = res->second;
73 }
74 }
75
76 // second: now that we know how many nodes we need use the map to fill
77 // the coordinates
78 numNodes = newIndex;
79 for (int dim=0; dim<numDims; dim++) {
80 const float* origC = fullNodes->coords[dim];
81 float* c = new float[numNodes];
82 coords.push_back(c);
83 IndexMap::const_iterator mIt;
84 for (mIt = indexMap.begin(); mIt != indexMap.end(); mIt++) {
85 c[mIt->second] = origC[mIt->first];
86 }
87 }
88 }
89
90 //
91 // Copy constructor
92 //
93 NodeData::NodeData(const NodeData& m)
94 {
95 numDims = m.numDims;
96 numNodes = m.numNodes;
97 nodeID = m.nodeID;
98 nodeTag = m.nodeTag;
99 nodeGDOF = m.nodeGDOF;
100 nodeGNI = m.nodeGNI;
101 nodeGRDFI = m.nodeGRDFI;
102 nodeGRNI = m.nodeGRNI;
103 nodeDist = m.nodeDist;
104 name = m.name;
105 for (int i=0; i<numDims; i++) {
106 float* c = new float[numNodes];
107 copy(m.coords[i], m.coords[i]+numNodes, c);
108 coords.push_back(c);
109 }
110 }
111
112 //
113 //
114 //
115 NodeData::~NodeData()
116 {
117 CoordArray::iterator it;
118 for (it = coords.begin(); it != coords.end(); it++)
119 delete[] *it;
120 }
121
122 //
123 //
124 //
125 bool NodeData::initFromFinley(const Finley_NodeFile* finleyFile)
126 {
127 #ifndef VISIT_PLUGIN
128 numDims = finleyFile->numDim;
129 numNodes = finleyFile->numNodes;
130
131 int mpisize = finleyFile->MPIInfo->size;
132 int* iPtr = finleyFile->nodesDistribution->first_component;
133 nodeDist.clear();
134 nodeDist.insert(nodeDist.end(), mpisize+1, 0);
135 copy(iPtr, iPtr+mpisize+1, nodeDist.begin());
136
137 CoordArray::iterator it;
138 for (it = coords.begin(); it != coords.end(); it++)
139 delete[] *it;
140 coords.clear();
141 nodeID.clear();
142 nodeTag.clear();
143 nodeGDOF.clear();
144 nodeGNI.clear();
145 nodeGRDFI.clear();
146 nodeGRNI.clear();
147
148 if (numNodes > 0) {
149 for (int i=0; i<numDims; i++) {
150 double* srcPtr = finleyFile->Coordinates + i;
151 float* c = new float[numNodes];
152 coords.push_back(c);
153 for (int j=0; j<numNodes; j++, srcPtr+=numDims) {
154 *c++ = (float) *srcPtr;
155 }
156 }
157
158 iPtr = finleyFile->Id;
159 nodeID.insert(nodeID.end(), numNodes, 0);
160 copy(iPtr, iPtr+numNodes, nodeID.begin());
161
162 iPtr = finleyFile->Tag;
163 nodeTag.insert(nodeTag.end(), numNodes, 0);
164 copy(iPtr, iPtr+numNodes, nodeTag.begin());
165
166 iPtr = finleyFile->globalDegreesOfFreedom;
167 nodeGDOF.insert(nodeGDOF.end(), numNodes, 0);
168 copy(iPtr, iPtr+numNodes, nodeGDOF.begin());
169
170 iPtr = finleyFile->globalNodesIndex;
171 nodeGNI.insert(nodeGNI.end(), numNodes, 0);
172 copy(iPtr, iPtr+numNodes, nodeGNI.begin());
173
174 iPtr = finleyFile->globalReducedDOFIndex;
175 nodeGRDFI.insert(nodeGRDFI.end(), numNodes, 0);
176 copy(iPtr, iPtr+numNodes, nodeGRDFI.begin());
177
178 iPtr = finleyFile->globalReducedNodesIndex;
179 nodeGRNI.insert(nodeGRNI.end(), numNodes, 0);
180 copy(iPtr, iPtr+numNodes, nodeGRNI.begin());
181
182 }
183 return true;
184 #else // VISIT_PLUGIN
185 return false;
186 #endif
187 }
188
189 //
190 //
191 //
192 bool NodeData::readFromNc(NcFile* ncFile)
193 {
194 #if USE_NETCDF
195 NcAtt* att;
196 NcVar* var;
197
198 att = ncFile->get_att("numDim");
199 numDims = att->as_int(0);
200
201 att = ncFile->get_att("numNodes");
202 numNodes = att->as_int(0);
203
204 att = ncFile->get_att("mpi_size");
205 int mpisize = att->as_int(0);
206
207 nodeDist.clear();
208 nodeDist.insert(nodeDist.end(), mpisize+1, 0);
209 var = ncFile->get_var("Nodes_NodeDistribution");
210 var->get(&nodeDist[0], mpisize+1);
211
212 CoordArray::iterator it;
213 for (it = coords.begin(); it != coords.end(); it++)
214 delete[] *it;
215 coords.clear();
216 nodeID.clear();
217 nodeTag.clear();
218 nodeGDOF.clear();
219 nodeGNI.clear();
220 nodeGRDFI.clear();
221 nodeGRNI.clear();
222
223 // Only attempt to read further if there are any nodes.
224 // Having no nodes is not an error.
225 if (numNodes > 0) {
226 var = ncFile->get_var("Nodes_Coordinates");
227 for (int i=0; i<numDims; i++) {
228 float* c = new float[numNodes];
229 var->set_cur(0, i);
230 var->get(c, numNodes, 1);
231 coords.push_back(c);
232 }
233
234 nodeID.insert(nodeID.end(), numNodes, 0);
235 var = ncFile->get_var("Nodes_Id");
236 var->get(&nodeID[0], numNodes);
237
238 nodeTag.insert(nodeTag.end(), numNodes, 0);
239 var = ncFile->get_var("Nodes_Tag");
240 var->get(&nodeTag[0], numNodes);
241
242 nodeGDOF.insert(nodeGDOF.end(), numNodes, 0);
243 var = ncFile->get_var("Nodes_gDOF");
244 var->get(&nodeGDOF[0], numNodes);
245
246 nodeGNI.insert(nodeGNI.end(), numNodes, 0);
247 var = ncFile->get_var("Nodes_gNI");
248 var->get(&nodeGNI[0], numNodes);
249
250 nodeGRDFI.insert(nodeGRDFI.end(), numNodes, 0);
251 var = ncFile->get_var("Nodes_grDfI");
252 var->get(&nodeGRDFI[0], numNodes);
253
254 nodeGRNI.insert(nodeGRNI.end(), numNodes, 0);
255 var = ncFile->get_var("Nodes_grNI");
256 var->get(&nodeGRNI[0], numNodes);
257 }
258
259 return true;
260 #else // !USE_NETCDF
261 return false;
262 #endif
263 }
264
265 //
266 //
267 //
268 const IntVec& NodeData::getVarDataByName(const string& name) const
269 {
270 if (name == "Nodes_Id")
271 return nodeID;
272 else if (name == "Nodes_Tag")
273 return nodeTag;
274 else if (name == "Nodes_gDOF")
275 return nodeGDOF;
276 else if (name == "Nodes_gNI")
277 return nodeGNI;
278 else if (name == "Nodes_grDfI")
279 return nodeGRDFI;
280 else if (name == "Nodes_grNI")
281 return nodeGRNI;
282 else
283 throw "Invalid variable name";
284 }
285
286 //
287 //
288 //
289 StringVec NodeData::getVarNames() const
290 {
291 StringVec res;
292 res.push_back("Nodes_Id");
293 res.push_back("Nodes_Tag");
294 res.push_back("Nodes_gDOF");
295 res.push_back("Nodes_gNI");
296 res.push_back("Nodes_grDfI");
297 res.push_back("Nodes_grNI");
298 return res;
299 }
300
301 //
302 //
303 //
304 int NodeData::getGlobalNumNodes() const
305 {
306 int ret=0;
307 if (!nodeDist.empty())
308 ret = nodeDist[nodeDist.size()-1];
309 return ret;
310 }
311
312 //
313 //
314 //
315 void NodeData::writeCoordinatesVTK(ostream& os, int ownIndex)
316 {
317 if (numNodes > 0) {
318 int firstId = nodeDist[ownIndex];
319 int lastId = nodeDist[ownIndex+1];
320 for (size_t i=0; i<numNodes; i++) {
321 if (firstId <= nodeGNI[i] && nodeGNI[i] < lastId) {
322 os << coords[0][i] << " " << coords[1][i] << " ";
323 if (numDims == 3)
324 os << coords[2][i];
325 else
326 os << 0.;
327 os << endl;
328 }
329 }
330 }
331 }
332
333 //
334 //
335 //
336 bool NodeData::writeToSilo(DBfile* dbfile)
337 {
338 #if USE_SILO
339 if (numNodes == 0)
340 return true;
341
342 int ret;
343
344 if (siloPath != "") {
345 ret = DBSetDir(dbfile, siloPath.c_str());
346 if (ret != 0)
347 return false;
348 }
349 string siloMeshName = getFullSiloName();
350
351 // Write node-centered variables
352 ret = DBPutUcdvar1(dbfile, "Nodes_Id", siloMeshName.c_str(),
353 (float*)&nodeID[0], numNodes, NULL, 0, DB_INT, DB_NODECENT, NULL);
354
355 if (ret == 0)
356 ret = DBPutUcdvar1(dbfile, "Nodes_Tag", siloMeshName.c_str(),
357 (float*)&nodeTag[0], numNodes, NULL, 0, DB_INT,
358 DB_NODECENT, NULL);
359 if (ret == 0)
360 ret = DBPutUcdvar1(dbfile, "Nodes_gDOF", siloMeshName.c_str(),
361 (float*)&nodeGDOF[0], numNodes, NULL, 0, DB_INT,
362 DB_NODECENT, NULL);
363 if (ret == 0)
364 ret = DBPutUcdvar1(dbfile, "Nodes_gNI", siloMeshName.c_str(),
365 (float*)&nodeGNI[0], numNodes, NULL, 0, DB_INT,
366 DB_NODECENT, NULL);
367 if (ret == 0)
368 ret = DBPutUcdvar1(dbfile, "Nodes_grDfI", siloMeshName.c_str(),
369 (float*)&nodeGRDFI[0], numNodes, NULL, 0, DB_INT,
370 DB_NODECENT, NULL);
371 if (ret == 0)
372 ret = DBPutUcdvar1(dbfile, "Nodes_grNI", siloMeshName.c_str(),
373 (float*)&nodeGRNI[0], numNodes, NULL, 0, DB_INT,
374 DB_NODECENT, NULL);
375
376 DBSetDir(dbfile, "/");
377 return (ret == 0);
378
379 #else // !USE_SILO
380 return false;
381 #endif
382 }
383
384 } // namespace escriptexport
385

  ViewVC Help
Powered by ViewVC 1.1.26