Line data Source code
1 : /*
2 : * Copyright (c) 2009-2015: G-CSC, Goethe University Frankfurt
3 : * Authors: Sebastian Reiter, Nicolas Tessore
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 "file_io_ng.h"
34 :
35 : #include <vector>
36 :
37 : #include "lib_grid/lg_base.h"
38 :
39 : extern "C" {
40 : #include <lib_grid/file_io/externals/include/ng/ng.h>
41 : }
42 :
43 : using namespace std;
44 :
45 : enum ng_face_type
46 : {
47 : ng_triangle = 3,
48 : ng_quadrilateral = 4,
49 : };
50 :
51 : enum ng_volume_type
52 : {
53 : ng_tetrahedra = 4,
54 : ng_pyramid = 5,
55 : ng_prism = 6,
56 : ng_hexahedra = 8
57 : };
58 :
59 : namespace ug
60 : {
61 :
62 : ////////////////////////////////////////////////////////////////////////
63 : // ImportGridFromNG
64 0 : bool ImportGridFromNG(Grid& grid,
65 : const char* filename,
66 : AVector3& aPos,
67 : ISubsetHandler* pSubdomainHandler)
68 : {
69 : // create ng object
70 0 : ng* n = ng_new();
71 :
72 : // read ng file
73 0 : ng_info* ninfo = ng_info_new();
74 0 : if(ng_read(filename, n, ninfo))
75 : {
76 0 : ng_info_delete(ninfo);
77 0 : ng_delete(n);
78 0 : n = ng_new();
79 0 : n->dim = 2;
80 0 : ninfo = ng_info_new();
81 :
82 : //TODO: 2d parsing fails, since it is not fully supported.
83 : // There are problems in the element description (awaits F).
84 0 : if(ng_read(filename, n, ninfo)){
85 0 : LOG("WARNING in ImportGridFromNG: " << ninfo->err_msg << endl);
86 0 : ng_info_delete(ninfo);
87 0 : ng_delete(n);
88 0 : return false;
89 : }
90 : }
91 0 : ng_info_delete(ninfo);
92 :
93 : // set up vertex attachment
94 0 : if(!grid.has_vertex_attachment(aPos))
95 0 : grid.attach_to_vertices(aPos);
96 :
97 : // set up vertex attachment accessor
98 : Grid::VertexAttachmentAccessor<AVector3> aaPosition(grid, aPos);
99 :
100 : // read nodes and store them in an array for index access
101 : vector<RegularVertex*> vVertices;
102 0 : vVertices.reserve(n->num_bnodes + n->num_inodes);
103 :
104 : // read boundary nodes
105 0 : for(int i = 0; i < n->num_bnodes; ++i)
106 : {
107 : // get boundary node
108 0 : ng_bnode* node = &n->bnodes[i];
109 :
110 : // create and store vertex
111 0 : RegularVertex* vert = *grid.create<RegularVertex>();
112 0 : vVertices.push_back(vert);
113 :
114 : // set vertex coordinates
115 : aaPosition[vert] = vector3(
116 0 : (number)node->coords[0],
117 0 : (number)node->coords[1],
118 0 : (number)node->coords[2]
119 : );
120 : }
121 :
122 : // read interior nodes
123 0 : for(int i = 0; i < n->num_inodes; ++i)
124 : {
125 : // get interior node
126 0 : ng_inode* node = &n->inodes[i];
127 :
128 : // create and store vertex
129 0 : RegularVertex* vert = *grid.create<RegularVertex>();
130 0 : vVertices.push_back(vert);
131 :
132 : // set vertex coordinates
133 : aaPosition[vert] = vector3(
134 0 : (number)node->coords[0],
135 0 : (number)node->coords[1],
136 0 : (number)node->coords[2]
137 : );
138 : }
139 :
140 : // if we're in 2d, set all z-coords to 0
141 0 : if(n->dim == 2){
142 0 : for(size_t i = 0; i < vVertices.size(); ++i)
143 0 : aaPosition[vVertices[i]].z() = 0;
144 : }
145 :
146 : // create the elements
147 0 : if(n->dim == 2){
148 : // read faces
149 0 : for(int i = 0; i < n->num_elements; ++i)
150 : {
151 0 : ng_element* elem = &n->elements[i];
152 :
153 : Face* face = NULL;
154 :
155 : // create face
156 0 : switch(elem->num_nodes)
157 : {
158 0 : case ng_triangle:
159 0 : face = *grid.create<Triangle>(TriangleDescriptor(
160 0 : vVertices[elem->nodes[0]],
161 0 : vVertices[elem->nodes[1]],
162 0 : vVertices[elem->nodes[2]]));
163 0 : break;
164 :
165 0 : case ng_quadrilateral:
166 0 : face = *grid.create<Quadrilateral>(QuadrilateralDescriptor(
167 0 : vVertices[elem->nodes[0]],
168 0 : vVertices[elem->nodes[1]],
169 0 : vVertices[elem->nodes[2]],
170 0 : vVertices[elem->nodes[3]]));
171 0 : break;
172 : default:
173 : LOG("WARNING in ImportGridFromNG: Face type not implemented!" << endl);
174 : break;
175 : }
176 :
177 : // add face to subset
178 0 : if(face != NULL && pSubdomainHandler != NULL)
179 0 : pSubdomainHandler->assign_subset(face, elem->subdomain - 1);
180 : }
181 : }
182 : else{
183 : // read volumes
184 0 : for(int i = 0; i < n->num_elements; ++i)
185 : {
186 0 : ng_element* elem = &n->elements[i];
187 :
188 : Volume* vol = NULL;
189 :
190 : // create volume
191 0 : switch(elem->num_nodes)
192 : {
193 0 : case ng_tetrahedra:
194 0 : vol = *grid.create<Tetrahedron>(TetrahedronDescriptor(
195 0 : vVertices[elem->nodes[0]],
196 0 : vVertices[elem->nodes[1]],
197 0 : vVertices[elem->nodes[2]],
198 0 : vVertices[elem->nodes[3]]
199 : ));
200 0 : break;
201 :
202 0 : case ng_pyramid:
203 0 : vol = *grid.create<Pyramid>(PyramidDescriptor(
204 0 : vVertices[elem->nodes[0]],
205 0 : vVertices[elem->nodes[1]],
206 0 : vVertices[elem->nodes[2]],
207 0 : vVertices[elem->nodes[3]],
208 0 : vVertices[elem->nodes[4]]));
209 0 : break;
210 0 : case ng_prism:
211 0 : vol = *grid.create<Prism>(PrismDescriptor(
212 0 : vVertices[elem->nodes[0]],
213 0 : vVertices[elem->nodes[1]],
214 0 : vVertices[elem->nodes[2]],
215 0 : vVertices[elem->nodes[3]],
216 0 : vVertices[elem->nodes[4]],
217 0 : vVertices[elem->nodes[5]]));
218 0 : break;
219 0 : case ng_hexahedra:
220 0 : vol = *grid.create<Hexahedron>(HexahedronDescriptor(
221 0 : vVertices[elem->nodes[0]],
222 0 : vVertices[elem->nodes[1]],
223 0 : vVertices[elem->nodes[2]],
224 0 : vVertices[elem->nodes[3]],
225 0 : vVertices[elem->nodes[4]],
226 0 : vVertices[elem->nodes[5]],
227 0 : vVertices[elem->nodes[6]],
228 0 : vVertices[elem->nodes[7]]));
229 0 : break;
230 : default:
231 : LOG("WARNING in ImportGridFromNG: Volume type not implemented!" << endl);
232 : break;
233 : }
234 :
235 : // add volume to subset
236 0 : if(vol != NULL && pSubdomainHandler != NULL)
237 0 : pSubdomainHandler->assign_subset(vol, elem->subdomain - 1);
238 : }
239 : }
240 : // done importing!
241 :
242 : // delete ng object
243 0 : ng_delete(n);
244 :
245 : return true;
246 0 : }
247 :
248 : }// end of namespace
|