Line data Source code
1 : /*
2 : * Copyright (c) 2010-2015: G-CSC, Goethe University Frankfurt
3 : * Author: Sebastian Reiter
4 : *
5 : * This file is part of UG4.
6 : *
7 : * UG4 is free software: you can redistribute it and/or modify it under the
8 : * terms of the GNU Lesser General Public License version 3 (as published by the
9 : * Free Software Foundation) with the following additional attribution
10 : * requirements (according to LGPL/GPL v3 §7):
11 : *
12 : * (1) The following notice must be displayed in the Appropriate Legal Notices
13 : * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 : *
15 : * (2) The following notice must be displayed at a prominent place in the
16 : * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 : *
18 : * (3) The following bibliography is recommended for citation and must be
19 : * preserved in all covered files:
20 : * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 : * parallel geometric multigrid solver on hierarchically distributed grids.
22 : * Computing and visualization in science 16, 4 (2013), 151-164"
23 : * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 : * flexible software system for simulating pde based models on high performance
25 : * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 : *
27 : * This program is distributed in the hope that it will be useful,
28 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 : * GNU Lesser General Public License for more details.
31 : */
32 :
33 : #include <fstream>
34 : #include <vector>
35 : #include <string>
36 : #include <cctype>
37 : #include <algorithm>
38 : #include "file_io_msh.h"
39 : #include "../lg_base.h"
40 :
41 : using namespace std;
42 :
43 : namespace ug
44 : {
45 0 : bool LoadGridFromMSH(Grid& grid, const char* filename,
46 : ISubsetHandler* psh, AVector3& aPos)
47 : {
48 : // the position attachment
49 0 : if(!grid.has_vertex_attachment(aPos))
50 0 : grid.attach_to_vertices(aPos);
51 : Grid::VertexAttachmentAccessor<APosition> aaPos(grid, aPos);
52 :
53 : // open the file
54 0 : ifstream in(filename);
55 0 : if(!in){
56 0 : UG_LOG("File not found: " << filename << "\n");
57 : return false;
58 : }
59 : // this buffer is used to find the commands
60 : string buffer;
61 :
62 : // here we'll store all nodes, so that we can access them by index
63 : vector<Vertex*> vrts;
64 :
65 : // iterate through the lines
66 0 : while(!in.eof())
67 : {
68 : // check whether any commands can be found
69 0 : in >> buffer;
70 : // make all entries upper case
71 : transform(buffer.begin(), buffer.end(), buffer.begin(), (int(*)(int)) toupper);
72 : //UG_LOG(buffer);
73 :
74 : // compare with known commands
75 0 : if(buffer.find("$NODES") != string::npos){
76 : // read the nodes array
77 : int numNodes;
78 0 : in >> numNodes;
79 0 : if(in.fail()){
80 : UG_LOG("LoadGridFromMSH: bad format in $NODES - numNodes\n");
81 0 : return false;
82 : }
83 :
84 : // iterate through the nodes. Create a new vertex for each
85 : // and assign the position
86 : int ind;
87 : vector3 p;
88 0 : for(int i = 0; i < numNodes; ++i){
89 0 : in >> ind >> p.x() >> p.y() >> p.z();
90 :
91 0 : if(in.fail()){
92 : UG_LOG("LoadGridFromMSH: bad format in $NODES\n");
93 : return false;
94 : }
95 :
96 0 : RegularVertex* vrt = *grid.create<RegularVertex>();
97 : aaPos[vrt] = p;
98 0 : vrts.push_back(vrt);
99 : }
100 : }
101 0 : else if(buffer.find("$ELEMENTS") != string::npos)
102 : {
103 0 : int numVrts = (int)vrts.size();
104 : // read the number of elements
105 : int numElems;
106 0 : in >> numElems;
107 0 : if(in.fail()){
108 : UG_LOG("LoadGridFromMSH: bad format in $ELEMENTS - numElems\n");
109 0 : return false;
110 : }
111 :
112 : // iterate through the entries and create the elements
113 : int ind, type, numTags;
114 : vector<int> tags;
115 :
116 0 : for(int i = 0; i < numElems; ++i){
117 0 : in >> ind >> type >> numTags;
118 0 : if(in.fail()){
119 : UG_LOG("LoadGridFromMSH: bad format in $ELEMENTS\n");
120 : return false;
121 : }
122 :
123 : // read the tags (tags are ignored in the moment)
124 : tags.clear();
125 0 : for(int j = 0; j < numTags; ++j){
126 : int tag;
127 0 : in >> tag;
128 0 : tags.push_back(tag);
129 : }
130 :
131 0 : if(in.fail()){
132 : UG_LOG("LoadGridFromMSH: bad tags in $ELEMENTS\n");
133 : return false;
134 : }
135 :
136 : Face* f = NULL;
137 0 : switch(type){
138 0 : case 2://triangles
139 : {
140 : int i1, i2, i3;
141 0 : in >> i1 >> i2 >> i3;
142 0 : if(in.fail()){
143 : UG_LOG("LoadGridFromMSH: bad format in triangle\n");
144 0 : return false;
145 : }
146 :
147 : // make sure that indices are fine
148 0 : if((i1 < 0 || i1 >= numVrts) ||
149 0 : (i2 < 0 || i2 >= numVrts) ||
150 0 : (i3 < 0 || i3 >= numVrts))
151 : {
152 : UG_LOG("LoadGridFromMSH: bad vertex indices in triangle. ignoring triangle "
153 : << grid.num<Triangle>() << ".\n");
154 0 : continue;
155 : }
156 : else{
157 0 : f = *grid.create<Triangle>(TriangleDescriptor(vrts[i1], vrts[i2], vrts[i3]));
158 : }
159 :
160 : }break;
161 :
162 : default:
163 : {
164 0 : UG_LOG("ERROR in LoadGridFromMSH: element type " << type << " not supported. aborting...\n");
165 : return false;
166 : }
167 : }// end of switch
168 : // assign subset index from tag
169 0 : if(psh){
170 0 : if(numTags < 3){
171 : // since there are no tags, we'll assign all elements to subset 0
172 0 : psh->assign_subset(f, 0);
173 : }
174 : else{
175 : // tag 3 holds the mesh-partition. assign the index
176 0 : psh->assign_subset(f, tags[2]);
177 : }
178 : }
179 : }
180 0 : }
181 : }
182 :
183 : // done.
184 : return true;
185 0 : }
186 : /*
187 : bool LoadGridFromTXT(Grid& grid, const char* filename, AVector3& aPos)
188 : {
189 : ifstream in(filename);
190 :
191 : if(!in)
192 : return false;
193 :
194 : //grid.clear();
195 :
196 : int numVrts, numTris;
197 :
198 : in >> numVrts;
199 : in >> numTris;
200 :
201 : // create points
202 : // store pointers to the vertices on the fly in a vector.
203 : vector<Vertex*> vVrts;
204 : vVrts.reserve(numVrts);
205 :
206 : for(int i = 0; i < numVrts; ++i)
207 : vVrts[i] = *grid.create<RegularVertex>();
208 :
209 : if(!grid.has_vertex_attachment(aPos))
210 : grid.attach_to_vertices(aPos);
211 : Grid::VertexAttachmentAccessor<APosition> aaPos(grid, aPos);
212 :
213 : // read the points
214 : {
215 : for(VertexIterator iter = grid.vertices_begin(); iter != grid.vertices_end(); ++iter)
216 : {
217 : int Index;
218 : in >> Index;
219 : in >> aaPos[*iter].x();
220 : in >> aaPos[*iter].y();
221 : in >> aaPos[*iter].z();
222 : }
223 : }
224 :
225 : // read the triangles
226 : {
227 : for(int i = 0; i < numTris; ++i)
228 : {
229 : int Index, i1, i2, i3;
230 : in >> Index;
231 : in >> i1;
232 : in >> i2;
233 : in >> i3;
234 : grid.create<Triangle>(TriangleDescriptor(vVrts[i1], vVrts[i2], vVrts[i3]));
235 : }
236 : }
237 :
238 : in.close();
239 : return true;
240 : }
241 : */
242 : }// end of namespace
|