Dune Core Modules (2.9.0)

boundarydom.hh
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_DGF_BOUNDARYDOMBLOCK_HH
6#define DUNE_DGF_BOUNDARYDOMBLOCK_HH
7
8#include <iostream>
9#include <string>
10#include <vector>
11
12#include <dune/grid/io/file/dgfparser/blocks/basic.hh>
13#include <dune/grid/io/file/dgfparser/parser.hh>
14
15
16namespace Dune
17{
18
19 namespace dgf
20 {
21
22 struct DomainData
23 {
24 typedef DGFBoundaryParameter::type BoundaryParameter;
25
26 DomainData ()
27 : id_( 0 ),
28 parameter_( DGFBoundaryParameter::defaultValue() ),
29 defaultData_( false )
30 { }
31
32 ~DomainData () { }
33
34 // constructor
35 DomainData ( int id, BoundaryParameter parameter, bool defaultData = false )
36 : id_( id ),
37 parameter_( parameter ),
38 defaultData_( defaultData )
39 { }
40
41 // return id
42 int id () const
43 {
44 return id_;
45 }
46
47 // return true, if additional parameters given
48 bool hasParameter () const
49 {
50 return (!parameter_.empty());
51 }
52
53 // return additional parameters
54 const BoundaryParameter & parameter () const
55 {
56 return parameter_;
57 }
58
59 // reset data
60 void reset ( int id, BoundaryParameter parameter, bool defaultData = false )
61 {
62 id_ = id;
63 parameter_ = parameter;
64 defaultData_ = defaultData;
65 }
66
67 // returns true if data origins from default boundary domain
68 bool isDefault () const
69 {
70 return defaultData_;
71 }
72
73 friend std::ostream & operator<< ( std :: ostream & os, const DomainData & ddata )
74 {
75 os << "domain data: id = " << ddata.id();
76 if( ddata.hasParameter() )
77 os << ", parameter = " << ddata.parameter();
78 return os;
79 }
80
81 private:
82 int id_;
83 BoundaryParameter parameter_;
84 bool defaultData_;
85
86 }; // end struct DomainData
87
88
89 struct Domain
90 {
91 // dimension of world coordinates
92 const int dimensionworld;
93
94 typedef DGFBoundaryParameter::type BoundaryParameter;
95
96 // constructor
97 Domain( std::vector< double > p1, std::vector< double > p2, int id, BoundaryParameter & parameter )
98 : dimensionworld( p1.size() ),
99 left_( p1 ),
100 right_( p2 ),
101 data_( id, parameter )
102 {
103 if( int( p2.size() ) != dimensionworld )
104 {
105 DUNE_THROW(DGFException,
106 "ERROR in " << *this << "!");
107 }
108 }
109
110 // constructor
111 Domain( std::vector< double > p1, std::vector< double > p2, DomainData & data )
112 : dimensionworld( p1.size() ),
113 left_( p1 ),
114 right_( p2 ),
115 data_( data )
116 {
117 if( int( p2.size() ) != dimensionworld )
118 {
119 DUNE_THROW(DGFException,
120 "ERROR in " << *this << "!");
121 }
122 }
123
124 // copy constructor
125 Domain ( const Domain & other )
126 : dimensionworld( other.dimensionworld ),
127 left_( other.left_ ),
128 right_( other.right_ ),
129 data_( other.data_ )
130 {
131 if( dimensionworld != other.dimensionworld )
132 {
133 DUNE_THROW(DGFException,
134 "ERROR in " << *this << "!");
135 }
136 }
137
138 // assignment
139 Domain & operator = ( const Domain & other )
140 {
141 if( dimensionworld != other.dimensionworld )
142 {
143 DUNE_THROW(DGFException,
144 "ERROR in " << *this << "!");
145 }
146
147 left_ = other.left_;
148 right_= other.right_;
149 data_= other.data_;
150 return *this;
151 }
152
153 // return true if point is contained in boundary domain
154 template< class Vector >
155 bool contains ( const Vector & x ) const
156 {
157 bool ret = true;
158 for( int i = 0; i < dimensionworld; ++i )
159 {
160 if( x[ i ] < left_[ i ] || x[ i ] > right_[ i ] )
161 ret = false;
162 }
163 return ret;
164 }
165
166 const DomainData & data () const
167 {
168 return data_;
169 }
170
171 // for error messages
172 friend std::ostream & operator<< ( std :: ostream &os, const Domain & domain )
173 {
174 os << "domain: " << std::endl;
175 os << "left = ";
176 for( int i = 0; i < domain.dimensionworld; ++i )
177 os << domain.left_[ i ] << " ";
178 os << std::endl;
179 os << "right = ";
180 for( int i = 0; i < domain.dimensionworld; ++i )
181 os << domain.right_[ i ] << " ";
182 os << std::endl;
183 os << domain.data();
184 return os;
185 }
186
187 private:
188 std::vector< double > left_, right_;
189 DomainData data_;
190
191 };
192
193 class BoundaryDomBlock
194 : public BasicBlock
195 {
196 typedef DGFBoundaryParameter::type BoundaryParameter;
197
198 // the dimension of the vertices (is given from user)
199 int dimworld_;
200
201 // internal counter
202 int counter_;
203
204 // default values if given
205 DomainData * default_;
206
207 // storage for all domains;
208 int ndomains_;
209 std::vector< Domain > domains_;
210
211 public:
212 // initialize vertex block and get first vertex
213 BoundaryDomBlock ( std::istream & in, int cdimworld );
214
215 // destructor
216 ~BoundaryDomBlock ()
217 {
218 if( default_ )
219 delete default_;
220 }
221
222 // go to next domain in block
223 bool next ()
224 {
225 counter_++;
226 return ( counter_ < ndomains_ );
227 }
228
229 // return domain
230 const Domain & domain () const
231 {
232 return domains_.at( counter_ );
233 }
234
235 // return true if default is given
236 bool hasDefaultData () const
237 {
238 return bool( default_ );
239 }
240
241 // return default data
242 const DomainData * defaultData () const
243 {
244 return default_;
245 }
246
247 // return true if any boundary domain block has
248 // additional parameters
249 bool hasParameter () const;
250
251 void reset ()
252 {
253 BasicBlock::reset();
254 counter_ = -1;
255 }
256
257 // return true while block is active
258 bool ok ()
259 {
260 return ( counter_ <= ndomains_ );
261 }
262
263 // return data if all vectors in array are contained within
264 // a single domain
265 template< class Vector >
266 const DomainData * contains ( const std::vector< Vector > & v ) const
267 {
268 std::vector< int > index( ndomains_ );
269 for( int i = 0; i < ndomains_; ++i)
270 index[ i ] = i;
271
272 size_t N = v.size();
273 for( size_t i = 0; i < N; ++i )
274 {
275 if( index.empty() )
276 break;
277
278 const int n = index.size();
279 assert( n > 0 );
280 for( int j = n-1; j >= 0; --j )
281 {
282 bool inside = domains_[ index[ j ] ].contains( v[ i ] );
283 if( !inside )
284 index.erase( index.begin() + j );
285 }
286 }
287
288 // check wheter no boundary domain found
289 if( index.empty() )
290 return default_;
291
292 // check for ambiguity
293 if( index.size() > 1 )
294 dwarn << "WARNING: ambiguous boundary domain assignment, use first boundary domain in list" << std::endl;
295
296 return &domains_[ index[ 0 ] ].data();
297 }
298
299 private:
300 void readBlock ();
301 };
302
303 } // end namespace dgf
304
305} // end namespace Dune
306
307#endif
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
DWarnType dwarn(std::cerr)
Stream for warnings indicating problems.
Definition: stdstreams.hh:161
Dune namespace.
Definition: alignedallocator.hh:13
std::string type
type of additional boundary parameters
Definition: parser.hh:25
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 21, 23:30, 2024)