dune-grid  2.9.0
dgfoned.hh
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 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
16 #include <dune/common/exceptions.hh>
17 
18 //- dune-grid includes
20 #include <dune/grid/onedgrid.hh>
21 
22 //- local includes
23 #include "dgfparser.hh"
24 
25 
26 namespace
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 
37 namespace Dune
38 {
39 
40  // DGFGridInfo
41  // -----------
42 
43  template< >
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< >
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 
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
The OneDGrid class.
Include standard header files.
Definition: agrid.hh:60
Definition: dgfgridfactory.hh:38
MPIHelper::MPICommunicator MPICommunicatorType
Definition: dgfgridfactory.hh:41
std::vector< double > & parameter(const Element &element)
Definition: dgfgridfactory.hh:124
static const int dimension
Definition: dgfgridfactory.hh:40
Intersection of a mesh entity of codimension 0 ("element") with a "neighboring" element or with the d...
Definition: common/intersection.hh:164
size_t boundarySegmentIndex() const
index of the boundary segment within the macro grid
Definition: common/intersection.hh:236
Wrapper class for entities.
Definition: common/entity.hh:66
Grid abstract base class.
Definition: common/grid.hh:375
constexpr static int dimension
The dimension of the grid.
Definition: common/grid.hh:387
constexpr static int dimensionworld
The dimension of the world the grid lives in.
Definition: common/grid.hh:390
GridFamily::Traits::template Codim< cd >::Entity Entity
A type that is a model of a Dune::Entity<cd,dim,...>.
Definition: common/grid.hh:419
static double refineWeight()
Definition: dgfoned.hh:51
static int refineStepsForHalf()
Definition: dgfoned.hh:46
std::vector< double > & parameter(const Entity &entity)
Definition: dgfoned.hh:127
OneDGrid Grid
grid type
Definition: dgfoned.hh:66
const DGFBoundaryParameter::type & boundaryParameter([[maybe_unused]] const Dune::Intersection< GG, II > &intersection) const
return invalid default value
Definition: dgfoned.hh:147
std::vector< double > & parameter([[maybe_unused]] const typename Grid::Codim< codim >::Entity &element)
return empty vector
Definition: dgfoned.hh:134
MPIHelper::MPICommunicator MPICommunicatorType
MPI communicator type.
Definition: dgfoned.hh:70
int boundaryId(const Dune::Intersection< GG, II > &intersection) const
Definition: dgfoned.hh:105
bool haveBoundaryParameters() const
OneDGrid does not support boundary parameters.
Definition: dgfoned.hh:140
Grid * grid() const
get grid
Definition: dgfoned.hh:92
int numParameters(const Entity &) const
OneDGrid does not support parameters, returns 0.
Definition: dgfoned.hh:114
DGFGridFactory(const std::string &filename, MPICommunicatorType comm=MPIHelper::getCommunicator())
constructor taking filename
Definition: dgfoned.hh:82
DGFGridFactory(std::istream &input, MPICommunicatorType comm=MPIHelper::getCommunicator())
constructor taking istream
Definition: dgfoned.hh:73
int numParameters() const
OneDGrid does not support parameters, returns 0.
Definition: dgfoned.hh:121
bool wasInserted(const Dune::Intersection< GG, II > &intersection) const
always returns false
Definition: dgfoned.hh:99
Some simple static information for a given GridType.
Definition: io/file/dgfparser/dgfparser.hh:56
static const type & defaultValue()
default constructor
Definition: parser.hh:28
std::string type
type of additional boundary parameters
Definition: parser.hh:25
One-dimensional adaptive grid.
Definition: onedgrid.hh:94