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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2834 - (show annotations)
Thu Jan 7 06:06:56 2010 UTC (10 years, 10 months ago) by caltinay
File size: 8629 byte(s)
Reduced elements are now handled separate from the main elements within the
data exporter. This simplifies usage considerably.

1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2009 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/FinleyMesh.h>
15 #include <escriptexport/ElementData.h>
16 #include <escriptexport/NodeData.h>
17
18 #include <finley/CppAdapter/MeshAdapter.h>
19 extern "C" {
20 #include <finley/Mesh.h>
21 }
22
23 #include <iostream>
24
25 #if USE_NETCDF
26 #include <netcdf.hh>
27 #endif
28
29 #if USE_SILO
30 #include <silo.h>
31 #endif
32
33 using namespace std;
34
35 namespace escriptexport {
36
37 //
38 //
39 //
40 FinleyMesh::FinleyMesh() :
41 initialized(false)
42 {
43 }
44
45 //
46 //
47 //
48 FinleyMesh::FinleyMesh(const FinleyMesh& m)
49 {
50 nodes = NodeData_ptr(new NodeData(*m.nodes));
51 cells = ElementData_ptr(new ElementData(*m.cells));
52 faces = ElementData_ptr(new ElementData(*m.faces));
53 contacts = ElementData_ptr(new ElementData(*m.contacts));
54 initialized = m.initialized;
55 }
56
57 //
58 //
59 //
60 FinleyMesh::~FinleyMesh()
61 {
62 cleanup();
63 }
64
65 //
66 //
67 //
68 void FinleyMesh::cleanup()
69 {
70 nodes.reset();
71 cells.reset();
72 faces.reset();
73 contacts.reset();
74 initialized = false;
75 }
76
77 //
78 //
79 //
80 bool FinleyMesh::initFromEscript(escript::const_Domain_ptr escriptDomain)
81 {
82 cleanup();
83
84 finleyMesh = dynamic_cast<const finley::MeshAdapter*>(escriptDomain.get())->getFinley_Mesh();
85 if (!finleyMesh) {
86 return false;
87 }
88
89 nodes = NodeData_ptr(new NodeData("Elements"));
90 cells = ElementData_ptr(new ElementData("Elements", nodes));
91 faces = ElementData_ptr(new ElementData("FaceElements", nodes));
92 contacts = ElementData_ptr(new ElementData("ContactElements", nodes));
93
94 if (nodes->initFromFinley(finleyMesh->Nodes) &&
95 cells->initFromFinley(finleyMesh->Elements) &&
96 faces->initFromFinley(finleyMesh->FaceElements) &&
97 contacts->initFromFinley(finleyMesh->ContactElements)) {
98 initialized = true;
99 }
100
101 return initialized;
102 }
103
104 //
105 // Reads mesh and element data from NetCDF file with given name
106 //
107 bool FinleyMesh::initFromNetCDF(const string& filename)
108 {
109 cleanup();
110
111 #if USE_NETCDF
112 NcError ncerr(NcError::silent_nonfatal);
113 NcFile* input;
114
115 input = new NcFile(filename.c_str());
116 if (!input->is_valid()) {
117 cerr << "Could not open input file " << filename << "." << endl;
118 delete input;
119 return false;
120 }
121
122 nodes = NodeData_ptr(new NodeData("Elements"));
123 if (!nodes->readFromNc(input))
124 return false;
125
126 // Read all element types
127 cells = ElementData_ptr(new ElementData("Elements", nodes));
128 cells->readFromNc(input);
129 faces = ElementData_ptr(new ElementData("FaceElements", nodes));
130 faces->readFromNc(input);
131 contacts = ElementData_ptr(new ElementData("ContactElements", nodes));
132 contacts->readFromNc(input);
133
134 delete input;
135 initialized = true;
136 #endif
137
138 return initialized;
139 }
140
141 //
142 //
143 //
144 NodeData_ptr FinleyMesh::getMeshForFinleyFS(int functionSpace) const
145 {
146 NodeData_ptr result;
147
148 if (!initialized)
149 return result;
150
151 ElementData_ptr elements = getElementsForFinleyFS(functionSpace);
152 if (elements != NULL)
153 result = elements->getNodeMesh();
154
155 return result;
156 }
157
158 //
159 //
160 //
161 ElementData_ptr FinleyMesh::getElementsForFinleyFS(int functionSpace) const
162 {
163 ElementData_ptr result;
164
165 if (!initialized) {
166 return result;
167 }
168
169 switch (functionSpace) {
170 case FINLEY_NODES:
171 result = cells;
172 break;
173
174 case FINLEY_REDUCED_NODES:
175 case FINLEY_REDUCED_ELEMENTS:
176 case FINLEY_ELEMENTS:
177 result = cells->getReducedElements();
178 if (!result)
179 result = cells;
180 break;
181
182 case FINLEY_REDUCED_FACE_ELEMENTS:
183 case FINLEY_FACE_ELEMENTS:
184 result = faces->getReducedElements();
185 if (!result)
186 result = faces;
187 break;
188
189 case FINLEY_REDUCED_CONTACT_ELEMENTS_1:
190 case FINLEY_REDUCED_CONTACT_ELEMENTS_2:
191 case FINLEY_CONTACT_ELEMENTS_1:
192 case FINLEY_CONTACT_ELEMENTS_2:
193 result = contacts->getReducedElements();
194 if (!result)
195 result = contacts;
196 break;
197
198 default:
199 cerr << "Unsupported function space type " << functionSpace
200 << "!" << endl;
201 }
202 return result;
203 }
204
205 //
206 // Returns a vector of strings containing Silo mesh names that have been
207 // written
208 //
209 StringVec FinleyMesh::getMeshNames() const
210 {
211 StringVec res;
212 if (initialized) {
213 StringVec tmpVec;
214 tmpVec = cells->getMeshNames();
215 res.insert(res.end(), tmpVec.begin(), tmpVec.end());
216 tmpVec = faces->getMeshNames();
217 res.insert(res.end(), tmpVec.begin(), tmpVec.end());
218 tmpVec = contacts->getMeshNames();
219 res.insert(res.end(), tmpVec.begin(), tmpVec.end());
220 }
221 return res;
222 }
223
224 //
225 // Returns a vector of strings containing Silo variable names that have
226 // been written
227 //
228 StringVec FinleyMesh::getVarNames() const
229 {
230 StringVec res;
231
232 if (initialized) {
233 res = nodes->getVarNames();
234 StringVec tmpVec = cells->getVarNames();
235 res.insert(res.end(), tmpVec.begin(), tmpVec.end());
236 tmpVec = faces->getVarNames();
237 res.insert(res.end(), tmpVec.begin(), tmpVec.end());
238 tmpVec = contacts->getVarNames();
239 res.insert(res.end(), tmpVec.begin(), tmpVec.end());
240 }
241
242 return res;
243 }
244
245 //
246 //
247 //
248 const IntVec& FinleyMesh::getVarDataByName(const string& name) const
249 {
250 if (!initialized) {
251 throw "Mesh not initialized";
252 }
253
254 if (name.find("ContactElements_") != name.npos)
255 return contacts->getVarDataByName(name);
256 else if (name.find("FaceElements_") != name.npos)
257 return faces->getVarDataByName(name);
258 else if (name.find("Elements_") != name.npos)
259 return cells->getVarDataByName(name);
260 else if (name.find("Nodes_") != name.npos)
261 return nodes->getVarDataByName(name);
262 else
263 throw "Invalid variable name";
264 }
265
266 //
267 //
268 //
269 ElementData_ptr FinleyMesh::getElementsByName(const string& name) const
270 {
271 ElementData_ptr ret;
272 if (name == "Elements")
273 ret = cells;
274 else if (name == "ReducedElements")
275 ret = cells->getReducedElements();
276 else if (name == "FaceElements")
277 ret = faces;
278 else if (name == "ReducedFaceElements")
279 ret = faces->getReducedElements();
280 else if (name == "ContactElements")
281 ret = contacts;
282 else if (name == "ReducedContactElements")
283 ret = contacts->getReducedElements();
284
285 return ret;
286 }
287
288 //
289 //
290 //
291 NodeData_ptr FinleyMesh::getMeshByName(const string& name) const
292 {
293 NodeData_ptr ret;
294 if (initialized) {
295 ElementData_ptr els = getElementsByName(name);
296 if (els)
297 ret = els->getNodeMesh();
298 }
299
300 return ret;
301 }
302
303 //
304 //
305 //
306 void FinleyMesh::reorderGhostZones(int ownIndex)
307 {
308 if (initialized) {
309 cells->reorderGhostZones(ownIndex);
310 faces->reorderGhostZones(ownIndex);
311 contacts->reorderGhostZones(ownIndex);
312 #ifdef _DEBUG
313 cout << "block " << ownIndex << " has " << cells->getGhostCount()
314 << " ghost zones," << endl;
315 cout << "\t" << faces->getGhostCount() << " ghost faces," << endl;
316 cout << "\t" << contacts->getGhostCount() << " ghost contacts." << endl;
317 #endif
318 }
319 }
320
321 //
322 //
323 //
324 void FinleyMesh::removeGhostZones(int ownIndex)
325 {
326 if (initialized) {
327 cells->removeGhostZones(ownIndex);
328 faces->removeGhostZones(ownIndex);
329 contacts->removeGhostZones(ownIndex);
330 #ifdef _DEBUG
331 cout << "After removing ghost zones there are" << endl;
332 cout << " " << nodes->getNumNodes() << " Nodes, ";
333 cout << cells->getCount() << " Elements, ";
334 cout << faces->getCount() << " Face elements, ";
335 cout << contacts->getCount() << " Contact elements left." << endl;
336 #endif
337 }
338 }
339
340 //
341 //
342 //
343 bool FinleyMesh::writeToSilo(DBfile* dbfile, const string& pathInSilo)
344 {
345 #if USE_SILO
346 // Write nodes, elements and mesh variables
347 if (!initialized ||
348 !cells->writeToSilo(dbfile, pathInSilo) ||
349 !faces->writeToSilo(dbfile, pathInSilo) ||
350 !contacts->writeToSilo(dbfile, pathInSilo))
351 return false;
352
353 siloPath = pathInSilo;
354 return true;
355
356 #else // !USE_SILO
357 return false;
358 #endif
359 }
360
361 } // namespace escriptexport
362

  ViewVC Help
Powered by ViewVC 1.1.26