[267620]: src / Mesh.cc  Maximize  Restore  History

Download this file

349 lines (314 with data), 15.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
Copyright (C) 2013 Marco Vassallo <gedeone-octave@users.sourceforge.net>
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "mesh.h"
DEFUN_DLD (Mesh, args, ,"-*- texinfo -*-\n\
@deftypefn {Function File} {[@var{mesh_out}]} = \
Mesh (@var{mesh_in}) \n\
Construct a mesh from file or from (p, e, t) format.\n\
The @var{mesh_in} should be either\n\
@itemize @bullet \n\
@item a string containing the name of the file where the mesh \
is stored in .xml file\n\
If the file is not a .xml file you can try to use the command\
dolfin-convert directly from the terminal. \n\
@item a a PDE-tool like structure with matrix fields (p,e,t)\
@end itemize\n\
The output @var{mesh_out} is a representation of the\
@var{mesh_in} which is compatible with fem-fenics.\n\
The easiest way for dealing with meshes is using the msh pkg. \n\
@seealso{fem_get_mesh}\n\
@end deftypefn")
{
int nargin = args.length ();
octave_value retval = 0;
if (nargin < 1 || nargin > 1)
print_usage ();
else
{
if (!error_state)
{
if (! mesh_type_loaded)
{
mesh::register_type ();
mesh_type_loaded = true;
mlock ();
}
if (args(0).is_string () == true)
{
std::string filename = args(0).string_value ();
//if the filename is not valid, dolfin takes care of it
retval = new mesh (filename);
}
else if (args(0).is_map () == true)
{
octave_scalar_map a = args(0).scalar_map_value ();
Array<double> p = a.contents ("p").matrix_value ();
Array<octave_idx_type> t = a.contents ("t").matrix_value ();
Array<octave_idx_type> e = a.contents ("e").matrix_value ();
if (! error_state)
{
retval = new mesh (p, e, t);
}
}
else
error ("Mesh: the argument you provide is invalid");
}
}
return retval;
}
mesh::mesh (Array<double>& p,
Array<octave_idx_type>& e,
Array<octave_idx_type>& t)
{
std::size_t D = p.rows ();
if (D < 2 || D > 3)
error ("Mesh constructor: only 2D or 3D meshes are supported");
else
{
dolfin::MeshEditor editor;
boost::shared_ptr<dolfin::Mesh> msh (new dolfin::Mesh ());
editor.open (*msh, D, D);
editor.init_vertices (p.cols ());
editor.init_cells (t.cols ());
if (D == 2)
{
for (uint i = 0; i < p.cols (); ++i)
editor.add_vertex (i,
p.xelem (0, i),
p.xelem (1, i));
for (uint i = 0; i < t.cols (); ++i)
editor.add_cell (i,
t.xelem (0, i) - 1,
t.xelem (1, i) - 1,
t.xelem (2, i) - 1);
}
if (D == 3)
{
for (uint i = 0; i < p.cols (); ++i)
editor.add_vertex (i,
p.xelem (0, i),
p.xelem (1, i),
p.xelem (2, i));
for (uint i = 0; i < t.cols (); ++i)
editor.add_cell (i,
t.xelem (0, i) - 1,
t.xelem (1, i) - 1,
t.xelem (2, i) - 1,
t.xelem (3, i) - 1);
}
editor.close ();
// store information associated with e
msh->init (D - 1);
dolfin::MeshValueCollection<std::size_t> facet (D - 1);
std::size_t num_side_edges = e.cols ();
if (D == 2)
{
for (uint i = 0; i < num_side_edges; ++i)
{
dolfin::Vertex v (*msh, e.xelem (0, i) - 1);
for (dolfin::FacetIterator f (v); ! f.end (); ++f)
{
if ((*f).entities(0)[0] == e.xelem (0, i) - 1
&& (*f).entities(0)[1] == e.xelem (1, i) - 1
|| (*f).entities(0)[0] == e.xelem (1, i) - 1
&& (*f).entities(0)[1] == e.xelem (0, i) - 1)
{
facet.set_value ((*f).index (), e.xelem (4, i), *msh);
break;
}
}
}
}
if (D == 3)
{
for (uint i = 0; i < num_side_edges; ++i)
{
dolfin::Vertex v (*msh, e.xelem (0, i) - 1);
for (dolfin::FacetIterator f (v); ! f.end (); ++f)
{
if ((*f).entities(0)[0] == e(0, i) - 1
&& (*f).entities(0)[1] == e.xelem (1, i) - 1
&& (*f).entities(0)[2] == e.xelem (2, i) - 1
|| (*f).entities(0)[0] == e.xelem (0, i) - 1
&& (*f).entities(0)[1] == e.xelem (2, i) - 1
&& (*f).entities(0)[2] == e.xelem (1, i) - 1
|| (*f).entities(0)[0] == e.xelem (1, i) - 1
&& (*f).entities(0)[1] == e.xelem (0, i) - 1
&& (*f).entities(0)[2] == e.xelem (2, i) - 1
|| (*f).entities(0)[0] == e.xelem (1, i) - 1
&& (*f).entities(0)[1] == e.xelem (2, i) - 1
&& (*f).entities(0)[2] == e.xelem (0, i) - 1
|| (*f).entities(0)[0] == e.xelem (2, i) - 1
&& (*f).entities(0)[1] == e.xelem (0, i) - 1
&& (*f).entities(0)[2] == e.xelem (1, i) - 1
|| (*f).entities(0)[0] == e.xelem (2, i) - 1
&& (*f).entities(0)[1] == e.xelem (1, i) - 1
&& (*f).entities(0)[2] == e.xelem (0, i) - 1)
{
facet.set_value ((*f).index (), e.xelem (9, i), *msh);
break;
}
}
}
}
*(msh->domains ().markers (D - 1)) = facet;
// store information associated with t
dolfin::MeshValueCollection<std::size_t> cell (D);
std::size_t num_cells = t.cols ();
if (D == 2)
{
for (uint i = 0; i < num_cells; ++i)
{
dolfin::Vertex v (*msh, t.xelem (0, i) - 1);
for (dolfin::CellIterator f (v); ! f.end (); ++f)
{
if ((*f).entities(0)[0] == t.xelem (0, i) - 1
&& (*f).entities(0)[1] == t.xelem (1, i) - 1
&& (*f).entities(0)[2] == t.xelem (2, i) - 1
|| (*f).entities(0)[0] == t.xelem (0, i) - 1
&& (*f).entities(0)[1] == t.xelem (2, i) - 1
&& (*f).entities(0)[2] == t.xelem (1, i) - 1
|| (*f).entities(0)[0] == t.xelem (1, i) - 1
&& (*f).entities(0)[1] == t.xelem (0, i) - 1
&& (*f).entities(0)[2] == t.xelem (2, i) - 1
|| (*f).entities(0)[0] == t.xelem (1, i) - 1
&& (*f).entities(0)[1] == t.xelem (2, i) - 1
&& (*f).entities(0)[2] == t.xelem (0, i) - 1
|| (*f).entities(0)[0] == t.xelem (2, i) - 1
&& (*f).entities(0)[1] == t.xelem (0, i) - 1
&& (*f).entities(0)[2] == t.xelem (1, i) - 1
|| (*f).entities(0)[0] == t.xelem (2, i) - 1
&& (*f).entities(0)[1] == t.xelem (1, i) - 1
&& (*f).entities(0)[2] == t.xelem (0, i) - 1)
{
cell.set_value ((*f).index (), t.xelem (3, i), *msh);
break;
}
}
}
}
if (D == 3)
{
for (uint i = 0; i < num_cells; ++i)
{
dolfin::Vertex v (*msh, t.xelem (0, i) - 1);
for (dolfin::CellIterator f (v); ! f.end (); ++f)
{
if ((*f).entities(0)[0] == t.xelem (0, i) - 1
&& (*f).entities(0)[1] == t.xelem (1, i) - 1
&& (*f).entities(0)[2] == t.xelem (2, i) - 1
&& (*f).entities(0)[3] == t.xelem (3, i) - 1
|| (*f).entities(0)[0] == t.xelem (0, i) - 1
&& (*f).entities(0)[1] == t.xelem (1, i) - 1
&& (*f).entities(0)[2] == t.xelem (3, i) - 1
&& (*f).entities(0)[3] == t.xelem (2, i) - 1
|| (*f).entities(0)[0] == t.xelem (0, i) - 1
&& (*f).entities(0)[1] == t.xelem (2, i) - 1
&& (*f).entities(0)[2] == t.xelem (1, i) - 1
&& (*f).entities(0)[3] == t.xelem (3, i) - 1
|| (*f).entities(0)[0] == t.xelem (0, i) - 1
&& (*f).entities(0)[1] == t.xelem (2, i) - 1
&& (*f).entities(0)[2] == t.xelem (3, i) - 1
&& (*f).entities(0)[3] == t.xelem (1, i) - 1
|| (*f).entities(0)[0] == t.xelem (0, i) - 1
&& (*f).entities(0)[1] == t.xelem (3, i) - 1
&& (*f).entities(0)[2] == t.xelem (1, i) - 1
&& (*f).entities(0)[3] == t.xelem (2, i) - 1
|| (*f).entities(0)[0] == t.xelem (0, i) - 1
&& (*f).entities(0)[1] == t.xelem (3, i) - 1
&& (*f).entities(0)[2] == t.xelem (2, i) - 1
&& (*f).entities(0)[3] == t.xelem (1, i) - 1
|| (*f).entities(0)[0] == t.xelem (1, i) - 1
&& (*f).entities(0)[1] == t.xelem (0, i) - 1
&& (*f).entities(0)[2] == t.xelem (2, i) - 1
&& (*f).entities(0)[3] == t.xelem (3, i) - 1
|| (*f).entities(0)[0] == t.xelem (1, i) - 1
&& (*f).entities(0)[1] == t.xelem (0, i) - 1
&& (*f).entities(0)[2] == t.xelem (3, i) - 1
&& (*f).entities(0)[3] == t.xelem (2, i) - 1
|| (*f).entities(0)[0] == t.xelem (1, i) - 1
&& (*f).entities(0)[1] == t.xelem (2, i) - 1
&& (*f).entities(0)[2] == t.xelem (0, i) - 1
&& (*f).entities(0)[3] == t.xelem (3, i) - 1
|| (*f).entities(0)[0] == t.xelem (1, i) - 1
&& (*f).entities(0)[1] == t.xelem (2, i) - 1
&& (*f).entities(0)[2] == t.xelem (3, i) - 1
&& (*f).entities(0)[3] == t.xelem (0, i) - 1
|| (*f).entities(0)[0] == t.xelem (1, i) - 1
&& (*f).entities(0)[1] == t.xelem (3, i) - 1
&& (*f).entities(0)[2] == t.xelem (0, i) - 1
&& (*f).entities(0)[3] == t.xelem (2, i) - 1
|| (*f).entities(0)[0] == t.xelem (1, i) - 1
&& (*f).entities(0)[1] == t.xelem (3, i) - 1
&& (*f).entities(0)[2] == t.xelem (2, i) - 1
&& (*f).entities(0)[3] == t.xelem (0, i) - 1
|| (*f).entities(0)[0] == t.xelem (2, i) - 1
&& (*f).entities(0)[1] == t.xelem (0, i) - 1
&& (*f).entities(0)[2] == t.xelem (1, i) - 1
&& (*f).entities(0)[3] == t.xelem (3, i) - 1
|| (*f).entities(0)[0] == t.xelem (2, i) - 1
&& (*f).entities(0)[1] == t.xelem (0, i) - 1
&& (*f).entities(0)[2] == t.xelem (3, i) - 1
&& (*f).entities(0)[3] == t.xelem (1, i) - 1
|| (*f).entities(0)[0] == t.xelem (2, i) - 1
&& (*f).entities(0)[1] == t.xelem (1, i) - 1
&& (*f).entities(0)[2] == t.xelem (0, i) - 1
&& (*f).entities(0)[3] == t.xelem (3, i) - 1
|| (*f).entities(0)[0] == t.xelem (2, i) - 1
&& (*f).entities(0)[1] == t.xelem (1, i) - 1
&& (*f).entities(0)[2] == t.xelem (3, i) - 1
&& (*f).entities(0)[3] == t.xelem (0, i) - 1
|| (*f).entities(0)[0] == t.xelem (2, i) - 1
&& (*f).entities(0)[1] == t.xelem (3, i) - 1
&& (*f).entities(0)[2] == t.xelem (0, i) - 1
&& (*f).entities(0)[3] == t.xelem (1, i) - 1
|| (*f).entities(0)[0] == t.xelem (2, i) - 1
&& (*f).entities(0)[1] == t.xelem (3, i) - 1
&& (*f).entities(0)[2] == t.xelem (1, i) - 1
&& (*f).entities(0)[3] == t.xelem (0, i) - 1
|| (*f).entities(0)[0] == t.xelem (3, i) - 1
&& (*f).entities(0)[1] == t.xelem (0, i) - 1
&& (*f).entities(0)[2] == t.xelem (1, i) - 1
&& (*f).entities(0)[3] == t.xelem (2, i) - 1
|| (*f).entities(0)[0] == t.xelem (3, i) - 1
&& (*f).entities(0)[1] == t.xelem (0, i) - 1
&& (*f).entities(0)[2] == t.xelem (2, i) - 1
&& (*f).entities(0)[3] == t.xelem (1, i) - 1
|| (*f).entities(0)[0] == t.xelem (3, i) - 1
&& (*f).entities(0)[1] == t.xelem (1, i) - 1
&& (*f).entities(0)[2] == t.xelem (0, i) - 1
&& (*f).entities(0)[3] == t.xelem (2, i) - 1
|| (*f).entities(0)[0] == t.xelem (3, i) - 1
&& (*f).entities(0)[1] == t.xelem (1, i) - 1
&& (*f).entities(0)[2] == t.xelem (2, i) - 1
&& (*f).entities(0)[3] == t.xelem (0, i) - 1
|| (*f).entities(0)[0] == t.xelem (3, i) - 1
&& (*f).entities(0)[1] == t.xelem (2, i) - 1
&& (*f).entities(0)[2] == t.xelem (0, i) - 1
&& (*f).entities(0)[3] == t.xelem (1, i) - 1
|| (*f).entities(0)[0] == t.xelem (3, i) - 1
&& (*f).entities(0)[1] == t.xelem (2, i) - 1
&& (*f).entities(0)[2] == t.xelem (1, i) - 1
&& (*f).entities(0)[3] == t.xelem (0, i) - 1)
{
cell.set_value ((*f).index (), t.xelem (4, i), *msh);
break;
}
}
}
}
*(msh->domains ().markers (D)) = cell;
pmsh = msh;
}
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks