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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2834 - (hide 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 caltinay 2183
2     /*******************************************************
3     *
4 jfenwick 2548 * Copyright (c) 2003-2009 by University of Queensland
5 caltinay 2183 * 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 caltinay 2810 #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 caltinay 2183 #include <netcdf.hh>
27 caltinay 2810 #endif
28    
29     #if USE_SILO
30 caltinay 2183 #include <silo.h>
31     #endif
32    
33     using namespace std;
34    
35 caltinay 2810 namespace escriptexport {
36 caltinay 2187
37 caltinay 2183 //
38     //
39     //
40 caltinay 2810 FinleyMesh::FinleyMesh() :
41     initialized(false)
42 caltinay 2183 {
43     }
44    
45     //
46     //
47     //
48 caltinay 2810 FinleyMesh::FinleyMesh(const FinleyMesh& m)
49 caltinay 2183 {
50 caltinay 2810 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 caltinay 2183 }
56    
57     //
58     //
59     //
60 caltinay 2810 FinleyMesh::~FinleyMesh()
61 caltinay 2183 {
62 caltinay 2810 cleanup();
63 caltinay 2183 }
64    
65     //
66 caltinay 2810 //
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 caltinay 2834 if (elements != NULL)
153     result = elements->getNodeMesh();
154 caltinay 2810
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 caltinay 2834 result = cells;
172     break;
173    
174 caltinay 2810 case FINLEY_REDUCED_NODES:
175     case FINLEY_REDUCED_ELEMENTS:
176     case FINLEY_ELEMENTS:
177 caltinay 2834 result = cells->getReducedElements();
178     if (!result)
179     result = cells;
180 caltinay 2810 break;
181    
182     case FINLEY_REDUCED_FACE_ELEMENTS:
183     case FINLEY_FACE_ELEMENTS:
184 caltinay 2834 result = faces->getReducedElements();
185     if (!result)
186     result = faces;
187 caltinay 2810 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 caltinay 2834 result = contacts->getReducedElements();
194     if (!result)
195     result = contacts;
196 caltinay 2810 break;
197    
198     default:
199     cerr << "Unsupported function space type " << functionSpace
200     << "!" << endl;
201     }
202     return result;
203     }
204    
205     //
206 caltinay 2183 // Returns a vector of strings containing Silo mesh names that have been
207     // written
208     //
209 caltinay 2810 StringVec FinleyMesh::getMeshNames() const
210 caltinay 2183 {
211     StringVec res;
212 caltinay 2810 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 caltinay 2183 return res;
222     }
223    
224 caltinay 2810 //
225 caltinay 2183 // Returns a vector of strings containing Silo variable names that have
226     // been written
227     //
228 caltinay 2810 StringVec FinleyMesh::getVarNames() const
229 caltinay 2183 {
230     StringVec res;
231 caltinay 2810
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 caltinay 2183
242     return res;
243     }
244    
245 caltinay 2810 //
246     //
247     //
248     const IntVec& FinleyMesh::getVarDataByName(const string& name) const
249 caltinay 2183 {
250 caltinay 2810 if (!initialized) {
251     throw "Mesh not initialized";
252     }
253 caltinay 2834
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 caltinay 2810 return nodes->getVarDataByName(name);
262 caltinay 2183 else
263 caltinay 2201 throw "Invalid variable name";
264 caltinay 2183 }
265    
266 caltinay 2810 //
267     //
268     //
269     ElementData_ptr FinleyMesh::getElementsByName(const string& name) const
270 caltinay 2183 {
271 caltinay 2810 ElementData_ptr ret;
272 caltinay 2834 if (name == "Elements")
273 caltinay 2183 ret = cells;
274 caltinay 2834 else if (name == "ReducedElements")
275     ret = cells->getReducedElements();
276     else if (name == "FaceElements")
277 caltinay 2183 ret = faces;
278 caltinay 2834 else if (name == "ReducedFaceElements")
279     ret = faces->getReducedElements();
280     else if (name == "ContactElements")
281 caltinay 2183 ret = contacts;
282 caltinay 2834 else if (name == "ReducedContactElements")
283     ret = contacts->getReducedElements();
284 caltinay 2183
285     return ret;
286     }
287    
288     //
289     //
290     //
291 caltinay 2834 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 caltinay 2810 void FinleyMesh::reorderGhostZones(int ownIndex)
307 caltinay 2183 {
308 caltinay 2810 if (initialized) {
309     cells->reorderGhostZones(ownIndex);
310     faces->reorderGhostZones(ownIndex);
311     contacts->reorderGhostZones(ownIndex);
312 caltinay 2183 #ifdef _DEBUG
313 caltinay 2810 cout << "block " << ownIndex << " has " << cells->getGhostCount()
314 caltinay 2834 << " ghost zones," << endl;
315     cout << "\t" << faces->getGhostCount() << " ghost faces," << endl;
316     cout << "\t" << contacts->getGhostCount() << " ghost contacts." << endl;
317 caltinay 2183 #endif
318 caltinay 2810 }
319 caltinay 2183 }
320    
321     //
322     //
323     //
324 caltinay 2810 void FinleyMesh::removeGhostZones(int ownIndex)
325 caltinay 2183 {
326 caltinay 2810 if (initialized) {
327     cells->removeGhostZones(ownIndex);
328     faces->removeGhostZones(ownIndex);
329     contacts->removeGhostZones(ownIndex);
330 caltinay 2183 #ifdef _DEBUG
331 caltinay 2810 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 caltinay 2183 #endif
337 caltinay 2810 }
338 caltinay 2183 }
339    
340     //
341     //
342     //
343 caltinay 2810 bool FinleyMesh::writeToSilo(DBfile* dbfile, const string& pathInSilo)
344 caltinay 2183 {
345 caltinay 2810 #if USE_SILO
346     // Write nodes, elements and mesh variables
347     if (!initialized ||
348     !cells->writeToSilo(dbfile, pathInSilo) ||
349 caltinay 2183 !faces->writeToSilo(dbfile, pathInSilo) ||
350 caltinay 2810 !contacts->writeToSilo(dbfile, pathInSilo))
351 caltinay 2183 return false;
352    
353 caltinay 2810 siloPath = pathInSilo;
354     return true;
355 caltinay 2183
356 caltinay 2810 #else // !USE_SILO
357 caltinay 2183 return false;
358     #endif
359     }
360    
361 caltinay 2810 } // namespace escriptexport
362 caltinay 2187

  ViewVC Help
Powered by ViewVC 1.1.26