Dune Core Modules (2.3.1)

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