Loading [MathJax]/extensions/tex2jax.js

dune-mmesh (1.4)

dgfparser.hh
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_MMESH_GRID_DGFPARSER_HH
4#define DUNE_MMESH_GRID_DGFPARSER_HH
5
6// System includes
7#include <memory>
8#include <utility>
9#include <vector>
10
11// Dune includes
12#include <dune/grid/common/intersection.hh>
13#include <dune/grid/io/file/dgfparser/blocks/projection.hh>
14#include <dune/grid/io/file/dgfparser/dgfparser.hh>
15#include <dune/grid/io/file/dgfparser/parser.hh>
16
17// MMesh includes
18#include <dune/mmesh/grid/explicitgridfactory.hh>
19
20namespace Dune
21{
22
23 // External Forward Declarations
24 // -----------------------------
25 template< class GridImp, class IntersectionImp >
26 class Intersection;
27
28 // DGFGridFactory for MMesh
29 // ------------------------
30 template< class HostGrid, int dim >
31 struct DGFGridFactory< MMesh<HostGrid, dim> >
32 {
33 typedef MMesh<HostGrid, dim> Grid;
34 const static int dimension = Grid::dimension;
35 typedef MPIHelper::MPICommunicator MPICommunicatorType;
36 typedef typename Grid::template Codim<0>::Entity Element;
37 typedef typename Grid::template Codim<dimension>::Entity Vertex;
38 typedef Dune::MMeshExplicitGridFactory<Grid> GridFactory;
39
40 explicit DGFGridFactory ( std::istream &input,
41 MPICommunicatorType comm = MPIHelper::getCommunicator() );
42 explicit DGFGridFactory ( const std::string &filename,
43 MPICommunicatorType comm = MPIHelper::getCommunicator() );
44
46 Grid* grid() const
47 {
48 return grid_;
49 }
50
52 template< class Intersection >
53 bool wasInserted ( const Intersection &intersection ) const
54 {
55 return factory_.wasInserted( intersection );
56 }
57
59 template< class Intersection >
60 int boundaryId ( const Intersection &intersection ) const
61 {
62 return intersection.impl().boundaryId();
63 }
64
66 std::vector< double > &parameter ( const Element &element )
67 {
68 if( numParameters< 0 >() <= 0 )
69 {
70 DUNE_THROW( InvalidStateException,
71 "Calling DGFGridFactory::parameter is only allowed if there are parameters." );
72 }
73 return dgf_.elParams[ factory_.insertionIndex( element ) ];
74 }
75
77 std::vector< double > &parameter ( const Vertex &vertex )
78 {
79 if( numParameters< dimension >() <= 0 )
80 {
81 DUNE_THROW( InvalidStateException,
82 "Calling DGFGridFactory::parameter is only allowed if there are parameters." );
83 }
84 return dgf_.vtxParams[ factory_.insertionIndex( vertex ) ];
85 }
86
88 bool haveBoundaryParameters () const
89 {
90 return dgf_.haveBndParameters;
91 }
92
93 template < class GG, class II >
94 const DGFBoundaryParameter::type
95 boundaryParameter ( const Intersection< GG, II > & intersection ) const { return DGFBoundaryParameter::type(); }
96
97 template< int codim >
98 int numParameters () const { return 0; }
99
100 private:
101 bool generate( std::istream &input )
102 {
103 dgf_.element = DuneGridFormatParser::Simplex;
104 dgf_.dimgrid = dimension;
105 dgf_.dimw = dimension;
106
107 if( !dgf_.readDuneGrid( input, dimension, dimension ) )
108 return false;
109
110 // Insert vertices
111 for( int n = 0; n < dgf_.nofvtx; ++n )
112 {
113 typename GridFactory::WorldVector coord;
114 for( std::size_t i = 0; i < dimension; ++i )
115 coord[ i ] = dgf_.vtx[ n ][ i ];
116 factory_.insertVertex( coord );
117 }
118
119 // Insert elements
120 for( int n = 0; n < dgf_.nofelements; ++n )
121 factory_.insertElement( GeometryTypes::simplex(dimension), dgf_.elements[ n ] );
122
123 // Insert boundary segments and ids
124 for( const auto& face : dgf_.facemap )
125 {
126 const auto& entityKey = face.first;
127 const std::size_t boundaryId = face.second.first;
128
129 std::vector< unsigned int > vertices;
130 for( int i = 0; i < entityKey.size(); ++i )
131 vertices.push_back( entityKey[i] );
132
133 std::sort( vertices.begin(), vertices.end() );
134
135 // insert boundary segment
136 factory_.insertBoundarySegment( vertices );
137
138 // insert boundary id
139 std::size_t index = factory_.boundarySegments().size()-1;
140 factory_.addBoundaryId( index, boundaryId );
141 }
142
143 grid_ = factory_.createGrid().release();
144 return true;
145 }
146
147 Grid* grid_;
148 GridFactory factory_;
149 DuneGridFormatParser dgf_;
150 };
151
152
153 // DGFGridInfo for MMesh
154 // ---------------------
155
156 template< class HostGrid, int dim >
157 struct DGFGridInfo< MMesh<HostGrid, dim> >
158 {
159 static int refineStepsForHalf ()
160 {
161 return 2;
162 }
163
164 static double refineWeight ()
165 {
166 return -1;
167 }
168 };
169
170
171 // Implementation of DGFGridFactory for MMesh
172 // ------------------------------------------
173
175 template< class HostGrid, int dim >
176 inline DGFGridFactory< MMesh<HostGrid, dim> >
177 ::DGFGridFactory ( std::istream &input, MPICommunicatorType comm )
178 : dgf_( 0, 1 )
179 {
180 input.clear();
181 input.seekg( 0 );
182 if( !input )
183 DUNE_THROW(DGFException, "Error resetting input stream." );
184 generate( input );
185 }
186
188 template< class HostGrid, int dim >
189 inline DGFGridFactory< MMesh<HostGrid, dim> >
190 ::DGFGridFactory ( const std::string &filename, MPICommunicatorType comm )
191 : dgf_( 0, 1 )
192 {
193 std::ifstream input( filename.c_str() );
194 if( !input )
195 DUNE_THROW( DGFException, "Macrofile " << filename << " not found." );
196 if( !generate( input ) )
197 DUNE_THROW( DGFException, "Could not generate MMesh from macrofile " << filename );
198 input.close();
199 }
200
201} // namespace Dune
202
203#endif
specialization of the explicit GridFactory for MMesh
Definition: explicitgridfactory.hh:35
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Apr 6, 22:49, 2025)