Loading...
Searching...
No Matches
lineunstructured.cpp
1/*---------------------------------------------------------------------------*\
2 *
3 * bitpit
4 *
5 * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6 *
7 * -------------------------------------------------------------------------
8 * License
9 * This file is part of bitpit.
10 *
11 * bitpit is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License v3 (LGPL)
13 * as published by the Free Software Foundation.
14 *
15 * bitpit is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with bitpit. If not, see <http://www.gnu.org/licenses/>.
22 *
23\*---------------------------------------------------------------------------*/
24
25#include "bitpit_common.hpp"
26
27#include "lineunstructured.hpp"
28
29namespace bitpit {
30
41#if BITPIT_ENABLE_MPI==1
53 : LineKernel(communicator, 1, ADAPTION_MANUAL, PARTITIONING_ENABLED)
54#else
59 : LineKernel(ADAPTION_MANUAL)
60#endif
61{
62}
63
64#if BITPIT_ENABLE_MPI==1
76LineUnstructured::LineUnstructured(int dimension, MPI_Comm communicator)
77 : LineKernel(PatchManager::AUTOMATIC_ID, dimension, communicator, 1, ADAPTION_MANUAL, PARTITIONING_ENABLED)
78#else
85 : LineKernel(PatchManager::AUTOMATIC_ID, dimension, ADAPTION_MANUAL)
86#endif
87{
88}
89
90#if BITPIT_ENABLE_MPI==1
103LineUnstructured::LineUnstructured(int id, int dimension, MPI_Comm communicator)
104 : LineKernel(id, dimension, communicator, 1, ADAPTION_MANUAL, PARTITIONING_ENABLED)
105#else
112LineUnstructured::LineUnstructured(int id, int dimension)
113 : LineKernel(id, dimension, ADAPTION_MANUAL)
114#endif
115{
116}
117
118#if BITPIT_ENABLE_MPI==1
129LineUnstructured::LineUnstructured(std::istream &stream, MPI_Comm communicator)
130 : LineKernel(communicator, 1, ADAPTION_MANUAL, PARTITIONING_ENABLED)
131#else
137LineUnstructured::LineUnstructured(std::istream &stream)
138 : LineKernel(ADAPTION_MANUAL)
139#endif
140{
141 // Restore the patch
142 restore(stream);
143}
144
150std::unique_ptr<PatchKernel> LineUnstructured::clone() const
151{
152 return std::unique_ptr<LineUnstructured>(new LineUnstructured(*this));
153}
154
161{
162 const int DUMP_VERSION = 1;
163
164 return DUMP_VERSION;
165}
166
172void LineUnstructured::_dump(std::ostream &stream) const
173{
174#if BITPIT_ENABLE_MPI==1
175 // Dump works only for serial calculations
176 if (getProcessorCount() != 1) {
177 throw std::runtime_error ("Dump of lineunstructured is implemented only for serial calculations.");
178 }
179#endif
180
181 // Save the vertices
182 dumpVertices(stream);
183
184 // Save the cells
185 dumpCells(stream);
186
187 // Save the interfaces
188 dumpInterfaces(stream);
189}
190
196void LineUnstructured::_restore(std::istream &stream)
197{
198#if BITPIT_ENABLE_MPI==1
199 // Restore works only for serial calculations
200 if (getProcessorCount() != 1) {
201 throw std::runtime_error ("Restore of lineunstructured is implemented only for serial calculations.");
202 }
203#endif
204
205 // Restore the vertices
206 restoreVertices(stream);
207
208 // Restore the cells
209 restoreCells(stream);
210
211 // Restore the interfaces
212 restoreInterfaces(stream);
213}
214
228long LineUnstructured::locatePoint(const std::array<double, 3> &point) const
229{
230 BITPIT_UNUSED(point);
231
232 throw std::runtime_error ("The function 'locatePoint' is not implemented yet");
233
234 return false;
235}
236
247unsigned short LineUnstructured::importDGF(const std::string &dgf_name, int PIDOffset, bool PIDSquash)
248{
249 // ====================================================================== //
250 // VARIABLES DECLARATION //
251 // ====================================================================== //
252
253 // Local variables
254 DGFObj dgf_in(dgf_name);
255 int nV = 0, nS = 0;
256 long vcount, idx;
257 std::vector<std::array<double, 3>> vertex_list;
258 std::vector<std::vector<int>> simplex_list;
259 std::vector<int> simplex_PID;
260 std::vector<long> vertex_map;
261 std::vector<long> connect;
262
263 // Counters
264 std::vector<std::array<double, 3>>::const_iterator v_, ve_;
265 std::vector<std::vector<int>>::iterator c_, ce_;
266 std::vector<int>::iterator i_, ie_;
267 std::vector<long>::iterator j_, je_;
268
269 // ====================================================================== //
270 // IMPORT DATA //
271 // ====================================================================== //
272
273 // Read vertices and cells from DGF file
274 dgf_in.load(nV, nS, vertex_list, simplex_list, simplex_PID);
275
276 // Add vertices
277 ve_ = vertex_list.cend();
278 vcount = 0;
279 vertex_map.resize(nV);
280 for (v_ = vertex_list.cbegin(); v_ != ve_; ++v_) {
281 idx = addVertex(*v_)->getId();
282 vertex_map[vcount] = idx;
283 ++vcount;
284 } //next v_
285
286 // Update connectivity infos
287 ce_ = simplex_list.end();
288 for (c_ = simplex_list.begin(); c_ != ce_; ++c_) {
289 ie_ = c_->end();
290 for (i_ = c_->begin(); i_ != ie_; ++i_) {
291 *i_ = vertex_map[*i_];
292 } //next i_
293 } //next c_
294
295 // Add cells
296 int k;
297 for (c_ = simplex_list.begin(), k = 0; c_ != ce_; ++c_, ++k) {
298 // Create cell
299 i_ = c_->begin();
300 connect.resize(c_->size(), Vertex::NULL_ID);
301 je_ = connect.end();
302 for (j_ = connect.begin(); j_ != je_; ++j_) {
303 *j_ = *i_;
304 ++i_;
305 } //next j_
306 CellIterator cellIterator = addCell(getDGFFacetType(c_->size()), connect);
307
308 // Set cell PID
309 int cellPID = PIDOffset;
310 if (!PIDSquash) {
311 cellPID += simplex_PID[k];
312 }
313
314 cellIterator->setPID(cellPID);
315 } //next c_
316
317 return 0;
318}
319
327unsigned short LineUnstructured::exportDGF(const std::string &dgf_name)
328{
329 // ====================================================================== //
330 // VARIABLES DECLARATION //
331 // ====================================================================== //
332
333 // Local variables
334 DGFObj dgf_in(dgf_name);
335 int nV = getVertexCount(), nS = getCellCount();
336 int v, nv;
337 long vcount, ccount, idx;
338 std::vector<std::array<double, 3>> vertex_list(nV);
339 std::vector<std::vector<int>> simplex_list(nS);
340 std::unordered_map<long, long> vertex_map;
341
342 // Counters
343 VertexIterator v_, ve_;
344 CellIterator c_, ce_;
345
346 // ====================================================================== //
347 // EXPORT DATA //
348 // ====================================================================== //
349
350 // Create vertex list
351 ve_ = vertexEnd();
352 vcount = 0;
353 for (v_ = vertexBegin(); v_ != ve_; ++v_) {
354 idx = v_->getId();
355 vertex_list[vcount] = v_->getCoords();
356 vertex_map[idx] = vcount;
357 ++vcount;
358 } //next v_
359
360 // Add cells
361 ce_ = cellEnd();
362 ccount = 0;
363 for (c_ = cellBegin(); c_ != ce_; ++c_) {
364 ConstProxyVector<long> cellVertexIds = c_->getVertexIds();
365 nv = cellVertexIds.size();
366 simplex_list[ccount].resize(nv);
367 for (v = 0; v < nv; ++v) {
368 simplex_list[ccount][v] = vertex_map[cellVertexIds[v]];
369 } //next v
370 ++ccount;
371 } //next c_
372
373 // Read vertices and cells from DGF file
374 dgf_in.save(nV, nS, vertex_list, simplex_list);
375
376 return 0;
377}
378
387{
388 switch(nFacetVertices) {
389
390 case 1:
391 return ElementType::VERTEX;
392
393 case 2:
394 return ElementType::LINE;
395
396 default:
397 return ElementType::UNDEFINED;
398
399 }
400}
401
402}
Interface to DGF I/O function.
Definition DGF.hpp:78
void save(int &, int &, std::vector< std::vector< double > > &, std::vector< std::vector< int > > &)
Definition DGF.cpp:676
void load(int &, int &, std::vector< std::vector< double > > &, std::vector< std::vector< int > > &)
Definition DGF.cpp:516
The LineUnstructured class defines an unstructured line tasselation.
int _getDumpVersion() const override
void _dump(std::ostream &stream) const override
unsigned short exportDGF(const std::string &)
static ElementType getDGFFacetType(int nFacetVertices)
unsigned short importDGF(const std::string &, int PIDOffset=0, bool PIDSquash=false)
std::unique_ptr< PatchKernel > clone() const override
LineUnstructured(MPI_Comm communicator)
void _restore(std::istream &stream) override
long locatePoint(const std::array< double, 3 > &point) const override
VertexIterator vertexEnd()
CellIterator cellBegin()
void dumpInterfaces(std::ostream &stream) const
void dumpVertices(std::ostream &stream) const
void dumpCells(std::ostream &stream) const
VertexIterator vertexBegin()
virtual long getVertexCount() const
void restoreCells(std::istream &stream)
VertexIterator addVertex(const Vertex &source, long id=Vertex::NULL_ID)
CellIterator addCell(const Cell &source, long id=Element::NULL_ID)
CellIterator cellEnd()
void restoreVertices(std::istream &stream)
void restore(std::istream &stream, bool reregister=false)
void restoreInterfaces(std::istream &stream)
virtual long getCellCount() const
The PatchManager oversee the handling of the patches.
id_t getId(const id_t &fallback=-1) const noexcept
Iterator for the class PiercedStorage.
Metafunction for generating a list of elements that can be either stored in an external vectror or,...
std::size_t size() const
#define BITPIT_UNUSED(variable)
Definition compiler.hpp:63
--- layout: doxygen_footer ---