Dune Core Modules (unstable)

dgfgeogrid.hh
1// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4// vi: set et ts=4 sw=2 sts=2:
5#ifndef DUNE_DGFGEOGRID_HH
6#define DUNE_DGFGEOGRID_HH
7
9
10#include <dune/grid/geometrygrid.hh>
11#include <dune/grid/io/file/dgfparser/dgfparser.hh>
12#include <dune/grid/io/file/dgfparser/blocks/projection.hh>
13#include <dune/grid/utility/hostgridaccess.hh>
14#include <dune/grid/common/intersection.hh>
15
16#include <dune/grid/io/file/dgfparser/parser.hh>
17
18
19namespace Dune
20{
21
22 /************************************************************************
23 * Warning:
24 * Reading DGF files directly into a GeometryGrid is a dirty hack for
25 * two reasons:
26 * 1) The host grid and coordinate function are never deleted (dangling
27 * pointers).
28 * 2) The coordinate function has to provide a default constructor
29 ************************************************************************/
30
31 // External Forward Declarations
32 // -----------------------------
33
34 template< class GridImp, class IntersectionImp >
35 class Intersection;
36
37
38
39 // DGFCoordFunction
40 // ----------------
41
42 template< int dimD, int dimR >
43 class DGFCoordFunction
44 : public AnalyticalCoordFunction< double, dimD, dimR, DGFCoordFunction< dimD, dimR > >
45 {
46 typedef DGFCoordFunction< dimD, dimR > This;
47 typedef AnalyticalCoordFunction< double, dimD, dimR, This > Base;
48
49 public:
50 typedef typename Base::DomainVector DomainVector;
51 typedef typename Base::RangeVector RangeVector;
52
53 typedef dgf::ProjectionBlock::Expression Expression;
54
55 DGFCoordFunction ( const Expression *expression )
56 : expression_( expression )
57 {}
58
59 void evaluate ( const DomainVector &x, RangeVector &y ) const
60 {
61 std::vector< double > vx( dimD );
62 std::vector< double > vy;
63 for( int i = 0; i < dimD; ++i )
64 vx[ i ] = x[ i ];
65 expression_->evaluate( vx, vy );
66 assert( vy.size() == size_t( dimR ) );
67 for( int i = 0; i < dimR; ++i )
68 y[ i ] = vy[ i ];
69 }
70
71 private:
72 const Expression *expression_;
73 };
74
75
76
77 // DGFCoordFunctionFactory
78 // -----------------------
79
80 template< class HostGrid, class CoordFunction,
81 bool discrete = GeoGrid::isDiscreteCoordFunctionInterface< typename CoordFunction::Interface >::value >
82 struct DGFCoordFunctionFactory;
83
84
85 template< class HostGrid, class CoordFunction >
86 struct DGFCoordFunctionFactory< HostGrid, CoordFunction, false >
87 {
88 static CoordFunction *create ( std::istream &, const HostGrid & )
89 {
90 return new CoordFunction;
91 }
92 };
93
94
95 template< class HostGrid, class CoordFunction >
96 struct DGFCoordFunctionFactory< HostGrid, CoordFunction, true >
97 {
98 static CoordFunction *create ( std::istream &, const HostGrid &hostGrid )
99 {
100 return new CoordFunction( hostGrid );
101 }
102 };
103
104
105 template< class HostGrid, int dimD, int dimR >
106 struct DGFCoordFunctionFactory< HostGrid, DGFCoordFunction< dimD, dimR >, false >
107 {
108 typedef DGFCoordFunction< dimD, dimR > CoordFunction;
109
110 static CoordFunction *create ( std::istream &input, const HostGrid &hostGrid )
111 {
112 dgf::ProjectionBlock projectionBlock( input, dimR );
113 const typename CoordFunction::Expression *expression = projectionBlock.function( "coordfunction" );
114 if( expression == 0 )
115 DUNE_THROW( DGFException, "no coordfunction specified in DGF file." );
116 return new CoordFunction( expression );
117 }
118 };
119
120
121
122 // DGFGridFactory for GeometryGrid
123 // -------------------------------
124
125 template< class HostGrid, class CoordFunction, class Allocator >
126 struct DGFGridFactory< GeometryGrid< HostGrid, CoordFunction, Allocator > >
127 {
128 typedef GeometryGrid< HostGrid, CoordFunction, Allocator > Grid;
129
130 const static int dimension = Grid::dimension;
131 typedef MPIHelper::MPICommunicator MPICommunicator;
132 typedef typename Grid::template Codim<0>::Entity Element;
133 typedef typename Grid::template Codim<dimension>::Entity Vertex;
134
135 typedef DGFCoordFunctionFactory< HostGrid, CoordFunction > CoordFunctionFactory;
136
137 explicit DGFGridFactory ( std::istream &input,
138 MPICommunicator comm = MPIHelper::getCommunicator() )
139 : dgfHostFactory_( input, comm ),
140 grid_( 0 )
141 {
142 auto hostGrid = std::shared_ptr<HostGrid>(dgfHostFactory_.grid());
143 assert( hostGrid != 0 );
144 auto coordFunction = std::shared_ptr<CoordFunction>(CoordFunctionFactory::create( input, *hostGrid ));
145 grid_ = new Grid( hostGrid, coordFunction );
146 }
147
148 explicit DGFGridFactory ( const std::string &filename,
149 MPICommunicator comm = MPIHelper::getCommunicator() )
150 : dgfHostFactory_( filename, comm ),
151 grid_( 0 )
152 {
153 auto hostGrid = std::shared_ptr<HostGrid>(dgfHostFactory_.grid());
154 assert( hostGrid != 0 );
155 std::ifstream input( filename.c_str() );
156 auto coordFunction = std::shared_ptr<CoordFunction>(CoordFunctionFactory::create( input, *hostGrid ));
157 grid_ = new Grid( hostGrid, coordFunction );
158 }
159
160 Grid *grid () const
161 {
162 return grid_;
163 }
164
165 template< class Intersection >
166 bool wasInserted ( const Intersection &intersection ) const
167 {
168 return dgfHostFactory_.wasInserted( HostGridAccess< Grid >::hostIntersection( intersection ) );
169 }
170
171 template< class Intersection >
172 int boundaryId ( const Intersection &intersection ) const
173 {
174 return dgfHostFactory_.boundaryId( HostGridAccess< Grid >::hostIntersection( intersection ) );
175 }
176
177 template< int codim >
178 int numParameters () const
179 {
180 return dgfHostFactory_.template numParameters< codim >();
181 }
182
183 // return true if boundary parameters found
184 bool haveBoundaryParameters () const
185 {
186 return dgfHostFactory_.haveBoundaryParameters();
187 }
188
189 template< class GG, class II >
190 const typename DGFBoundaryParameter::type &
191 boundaryParameter ( const Dune::Intersection< GG, II > & intersection ) const
192 {
193 return dgfHostFactory_.boundaryParameter( HostGridAccess< Grid >::hostIntersection( intersection ) );
194 }
195
196 template< class Entity >
197 std::vector< double > &parameter ( const Entity &entity )
198 {
199 return dgfHostFactory_.parameter( HostGridAccess< Grid >::hostEntity( entity ) );
200 }
201
202 private:
203 DGFGridFactory< HostGrid > dgfHostFactory_;
204 Grid *grid_;
205 };
206
207
208
209 // DGFGridInfo for GeometryGrid
210 // ----------------------------
211
212 template< class HostGrid, class CoordFunction, class Allocator >
213 struct DGFGridInfo< GeometryGrid< HostGrid, CoordFunction, Allocator > >
214 {
215 static int refineStepsForHalf ()
216 {
218 }
219
220 static double refineWeight ()
221 {
222 return -1.0;
223 }
224 };
225
226}
227
228#endif // #ifndef DUNE_DGFGEOGRID_HH
static constexpr int dimension
The dimension of the grid.
Definition: grid.hh:387
Intersection of a mesh entity of codimension 0 ("element") with a "neighboring" element or with the d...
Definition: intersection.hh:164
MPI_Comm MPICommunicator
The type of the mpi communicator.
Definition: mpihelper.hh:192
static MPICommunicator getCommunicator()
get the default communicator
Definition: mpihelper.hh:200
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13
std::string type
type of additional boundary parameters
Definition: parser.hh:25
static double refineWeight()
static int refineStepsForHalf()
number of globalRefine steps needed to refuce h by 0.5
Traits for type conversions and type information.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)