DUNE-FEM (unstable)

dgfoned.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_GRID_FILE_IO_DGFPARSER_DGFONED_HH
6#define DUNE_GRID_FILE_IO_DGFPARSER_DGFONED_HH
7
8//- C++ includes
9#include <algorithm>
10#include <fstream>
11#include <iostream>
12#include <istream>
13#include <vector>
14
15//- dune-common includes
17
18//- dune-grid includes
19#include <dune/grid/common/intersection.hh>
20#include <dune/grid/onedgrid.hh>
21
22//- local includes
23#include "dgfparser.hh"
24
25
26namespace
27{
28 // helper method used below
29 double getfirst ( std::vector< double > v )
30 {
31 return v[ 0 ];
32 }
33} // end anonymous namespace
34
35
36
37namespace Dune
38{
39
40 // DGFGridInfo
41 // -----------
42
43 template< >
44 struct DGFGridInfo< OneDGrid >
45 {
46 static int refineStepsForHalf ()
47 {
48 return 1;
49 }
50
51 static double refineWeight ()
52 {
53 return 0.5;
54 }
55 };
56
57
58
59 // DGFGridFactory< OneDGrid >
60 // --------------------------
61
62 template< >
63 struct DGFGridFactory< OneDGrid >
64 {
66 typedef OneDGrid Grid;
68 const static int dimension = Grid::dimension;
70 typedef MPIHelper::MPICommunicator MPICommunicatorType;
71
73 explicit DGFGridFactory ( std::istream &input,
74 MPICommunicatorType comm = MPIHelper::getCommunicator() )
75 : grid_( 0 ),
76 emptyParameters_( 0 )
77 {
78 generate( input, comm );
79 }
80
82 explicit DGFGridFactory ( const std::string &filename,
83 MPICommunicatorType comm = MPIHelper::getCommunicator() )
84 : grid_( 0 ),
85 emptyParameters_( 0 )
86 {
87 std::ifstream input( filename.c_str() );
88 generate( input, comm );
89 }
90
92 Grid *grid () const
93 {
94 return grid_;
95 }
96
98 template< class GG, class II >
99 bool wasInserted ( const Dune::Intersection< GG, II > &intersection ) const
100 {
101 return false;
102 }
103
104 template< class GG, class II >
105 int boundaryId ( const Dune::Intersection< GG, II > &intersection ) const
106 {
107 // OneDGrid returns boundary segment index;
108 // we return the index as the method boundaryId is deprecated
109 return intersection.boundarySegmentIndex();
110 }
111
113 template< class Entity >
114 int numParameters ( const Entity & ) const
115 {
116 return 0;
117 }
118
120 template< int codim >
121 int numParameters () const
122 {
123 return 0;
124 }
125
126 template< class Entity >
127 std::vector< double >& parameter ( const Entity &entity )
128 {
129 return parameter< Entity::codimension >( entity );
130 }
131
133 template< int codim >
134 std::vector< double > &parameter ( [[maybe_unused]] const typename Grid::Codim< codim >::Entity &element )
135 {
136 return emptyParameters_;
137 }
138
140 bool haveBoundaryParameters () const
141 {
142 return false;
143 }
144
146 template< class GG, class II >
147 const DGFBoundaryParameter::type &boundaryParameter ( [[maybe_unused]] const Dune::Intersection< GG, II > &intersection ) const
148 {
150 }
151
152 private:
153 // generate grid
154 void generate ( std::istream &input, MPICommunicatorType comm );
155
156 Grid *grid_;
157 std::vector< double > emptyParameters_;
158 };
159
160
161
162 // Implementation of DGFGridFactory< OneDGrid >
163 // --------------------------------------------
164
165 inline void DGFGridFactory< OneDGrid >::generate ( std::istream &input, [[maybe_unused]] MPICommunicatorType comm )
166 {
167 // try to open interval block
168 dgf::IntervalBlock intervalBlock( input );
169
170 // try to open vertex block
171 int dimensionworld = Grid::dimensionworld;
172 dgf::VertexBlock vertexBlock( input, dimensionworld );
173
174 // check at least one block is active
175 if( !( vertexBlock.isactive() || intervalBlock.isactive() ))
176 DUNE_THROW( DGFException, "No readable block found" );
177
178 std::vector< std::vector< double > > vertices;
179
180 // read vertices first
181 if( vertexBlock.isactive() )
182 {
183 int nparameter = 0;
184 std::vector< std::vector< double > > parameter;
185 vertexBlock.get( vertices, parameter, nparameter );
186
187 if( nparameter > 0 )
188 std::cerr << "Warning: vertex parameters will be ignored" << std::endl;
189 }
190
191 // get vertices from interval block
192 if ( intervalBlock.isactive() )
193 {
194 if( intervalBlock.dimw() != dimensionworld )
195 {
196 DUNE_THROW( DGFException, "Error: wrong coordinate dimension in interval block \
197 (got " << intervalBlock.dimw() << ", expected " << dimensionworld << ")" );
198 }
199
200 int nintervals = intervalBlock.numIntervals();
201 for( int i = 0; i < nintervals; ++i )
202 intervalBlock.getVtx( i, vertices );
203 }
204
205 // copy to vector of doubles
206 std::vector< double > vtx( vertices.size() );
207 transform( vertices.begin(), vertices.end(), vtx.begin(), getfirst );
208
209 // remove duplicates
210 std::sort( vtx.begin(), vtx.end() );
211 std::vector< double >::iterator it = std::unique( vtx.begin(), vtx.end() );
212 vtx.erase( it, vtx.end() );
213 if( vertices.size() != vtx.size() )
214 std::cerr << "Warning: removed duplicate vertices" << std::endl;
215
216 // create grid
217 grid_ = new OneDGrid( vtx );
218 }
219
220} // end namespace Dune
221
222#endif // #ifndef DUNE_GRID_FILE_IO_DGFPARSER_DGFONED_HH
static constexpr int dimension
The dimension of the grid.
Definition: grid.hh:387
static constexpr int dimensionworld
The dimension of the world the grid lives in.
Definition: grid.hh:390
Intersection of a mesh entity of codimension 0 ("element") with a "neighboring" element or with the d...
Definition: intersection.hh:164
size_t boundarySegmentIndex() const
index of the boundary segment within the macro grid
Definition: intersection.hh:236
MPI_Comm MPICommunicator
The type of the mpi communicator.
Definition: mpihelper.hh:192
static MPICommunicator getCommunicator()
get the default communicator
Definition: mpihelper.hh:200
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13
The OneDGrid class.
static const type & defaultValue()
default constructor
Definition: parser.hh:28
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
GridFamily::Traits::template Codim< cd >::Entity Entity
A type that is a model of a Dune::Entity<cd,dim,...>.
Definition: grid.hh:419
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 13, 23:29, 2024)