dune-grid  2.4.1
boundarydom.hh
Go to the documentation of this file.
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 
12 
13 
14 namespace Dune
15 {
16 
17  namespace dgf
18  {
19 
20  struct DomainData
21  {
23 
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 
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 
192  : public BasicBlock
193  {
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
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  {
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
DGFBoundaryParameter::type BoundaryParameter
Definition: boundarydom.hh:22
Definition: boundarydom.hh:20
~BoundaryDomBlock()
Definition: boundarydom.hh:214
~DomainData()
Definition: boundarydom.hh:30
bool contains(const Vector &x) const
Definition: boundarydom.hh:153
const DomainData & data() const
Definition: boundarydom.hh:164
void reset()
Definition: boundarydom.hh:249
bool isDefault() const
Definition: boundarydom.hh:66
const BoundaryParameter & parameter() const
Definition: boundarydom.hh:52
const DomainData * defaultData() const
Definition: boundarydom.hh:240
bool hasDefaultData() const
Definition: boundarydom.hh:234
const DomainData * contains(const std::vector< Vector > &v) const
Definition: boundarydom.hh:264
DomainData()
Definition: boundarydom.hh:24
const Domain & domain() const
Definition: boundarydom.hh:228
std::string type
type of additional boundary parameters
Definition: parser.hh:23
bool hasParameter() const
Definition: boundarydom.hh:46
Definition: boundarydom.hh:87
void reset(int id, BoundaryParameter parameter, bool defaultData=false)
Definition: boundarydom.hh:58
Contains types for additional features.
Definition: parser.hh:20
int id() const
Definition: boundarydom.hh:40
void reset()
Definition: basic.hh:49
DGFBoundaryParameter::type BoundaryParameter
Definition: boundarydom.hh:92
Domain(const Domain &other)
Definition: boundarydom.hh:123
Include standard header files.
Definition: agrid.hh:59
Domain(std::vector< double > p1, std::vector< double > p2, DomainData &data)
Definition: boundarydom.hh:109
DomainData(int id, BoundaryParameter parameter, bool defaultData=false)
Definition: boundarydom.hh:33
friend std::ostream & operator<<(std::ostream &os, const DomainData &ddata)
Definition: boundarydom.hh:71
bool ok()
Definition: boundarydom.hh:256
Definition: boundarydom.hh:191
bool next()
Definition: boundarydom.hh:221
Domain(std::vector< double > p1, std::vector< double > p2, int id, BoundaryParameter &parameter)
Definition: boundarydom.hh:95
const int dimensionworld
Definition: boundarydom.hh:90
Definition: basic.hh:28
exception class for IO errors in the DGF parser
Definition: dgfexception.hh:12