OpenFCST: The open-source Fuel Cell Simulation Toolbox
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fe_vectors.h
Go to the documentation of this file.
1 // ------------------------------------------------------------------
2 // $Id: fe_vectors.h 1348 2013-08-16 00:45:07Z secanell $
3 //
4 // Copyright (C) 2006, 2007, 2008, 2009 by Guido Kanschat.
5 // Copyright (C) 2012 by Valentin N. Zingan, University of Alberta.
6 //
7 // This file is subject to QPL and may not be distributed
8 // without copyright and license information. Please refer
9 // to the file deal.II/doc/license.html for the text and
10 // further information on this license.
11 // ------------------------------------------------------------------
12 
13 #ifndef _DEALII_APPFRAME_FE_VECTORS_H_
14 #define _DEALII_APPFRAME_FE_VECTORS_H_
15 
16 #include <lac/vector_memory.h>
17 #include <lac/block_vector.h>
18 
19 #include <algorithm>
20 #include <fstream>
21 
22 using namespace dealii;
23 
24 namespace AppFrame
25 {
26 
31 
49 class FEVectors : public Subscriptor
50 {
51 public:
52 
54  FEVectors();
55 
65  void add_scalar(double& s,
66  const std::string& name);
67 
72  void add_vector(FEVector& v,
73  const std::string& name);
74 
76  void add_vector(const FEVector& v,
77  const std::string& name);
78 
84  void merge(FEVectors& other);
85 
95  void merge(const FEVectors& other);
97 
104 
105  unsigned int n_scalars() const;
106 
108  unsigned int n_vectors() const;
109 
111  double& scalar(unsigned int i);
112 
114  const double& scalar(unsigned int i) const;
115 
117  FEVector& vector(unsigned int i);
118 
120  const FEVector& vector(unsigned int i) const;
121 
123  const std::string& scalar_name(unsigned int i) const;
124 
126  const std::string& vector_name(unsigned int i) const;
127 
129  unsigned int find_scalar(const std::string& name) const;
130 
132  unsigned int find_vector(const std::string& name) const;
133 
140  template<typename OUT>
141  void list(OUT& out,
142  unsigned int verbosity = 1) const;
144 
152  DeclException2(ExcNameMismatch,
153  int,
154  std::string,
155  << "Name at position " << arg1 << " is not equal to " << arg2);
156 
157 private:
158 
161 
163  std::vector<double*> scalars;
164 
166  std::vector<std::string> scalar_names;
167 
169  std::vector<FEVector*> vectors;
170 
172  std::vector<std::string> vector_names;
173 };
174 
175 inline
176 FEVectors::FEVectors()
177 :
178 is_constant(false)
179 { }
180 
181 inline
182 void
184  const std::string& name)
185 {
186  Assert(!is_constant, ExcNotImplemented());
187  scalars.push_back(&s);
188  scalar_names.push_back(name);
189 }
190 
191 inline
192 void
194  const std::string& name)
195 {
196  Assert(!is_constant, ExcNotImplemented());
197  vectors.push_back(&v);
198  vector_names.push_back(name);
199 }
200 
201 inline
202 void
204  const std::string& name)
205 {
206  Assert(!is_constant, ExcNotImplemented());
207  vectors.push_back(const_cast<FEVector*> (&v));
208  vector_names.push_back(name);
209  is_constant = true;
210 }
211 
212 inline
213 void
215 {
216  bool constness = is_constant;
217 
218  for(unsigned int i = 0; i < other.n_scalars(); ++i)
219  add_scalar(*other.scalars[i], other.scalar_names[i]);
220 
221  for(unsigned int i = 0; i < other.n_vectors(); ++i)
222  add_vector(*other.vectors[i], other.vector_names[i]);
223 
224  is_constant = constness | other.is_constant;
225 }
226 
227 inline
228 void
230 {
231  for(unsigned int i = 0; i < other.n_scalars(); ++i)
232  add_scalar(*other.scalars[i], other.scalar_names[i]);
233 
234  for(unsigned int i = 0; i < other.n_vectors(); ++i)
235  add_vector(*other.vectors[i], other.vector_names[i]);
236 
237  is_constant = true;
238 }
239 
240 inline
241 unsigned int
243 {
244  return scalars.size();
245 }
246 
247 inline
248 unsigned int
250 {
251  return vectors.size();
252 }
253 
254 inline
255 double&
256 FEVectors::scalar(unsigned int i)
257 {
258  Assert(!is_constant, ExcNotImplemented());
259  Assert(i < n_scalars(), ExcIndexRange(i,0,n_scalars()));
260  return *scalars[i];
261 }
262 
263 inline
264 const double&
265 FEVectors::scalar(unsigned int i) const
266 {
267  Assert(i < n_scalars(), ExcIndexRange(i,0,n_scalars()));
268  return *scalars[i];
269 }
270 
271 inline
272 FEVector&
273 FEVectors::vector(unsigned int i)
274 {
275  Assert(!is_constant, ExcNotImplemented());
276  Assert(i < n_vectors(), ExcIndexRange(i,0,n_vectors()));
277  return *vectors[i];
278 }
279 
280 inline
281 const FEVector&
282 FEVectors::vector(unsigned int i) const
283 {
284  Assert(i < n_vectors(), ExcIndexRange(i,0,n_vectors()));
285  return *vectors[i];
286 }
287 
288 inline
289 const std::string&
290 FEVectors::scalar_name(unsigned int i) const
291 {
292  Assert(i < n_scalars(), ExcIndexRange(i,0,n_scalars()));
293  return scalar_names[i];
294 }
295 
296 inline
297 const std::string&
298 FEVectors::vector_name(unsigned int i) const
299 {
300  Assert(i < n_vectors(), ExcIndexRange(i,0,n_vectors()));
301  return vector_names[i];
302 }
303 
304 inline
305 unsigned int
306 FEVectors::find_scalar(const std::string& name) const
307 {
308  const std::vector<std::string>::const_iterator
309  iter = std::find(scalar_names.begin(), scalar_names.end(), name);
310 
311  if(iter == scalar_names.end())
312  return deal_II_numbers::invalid_unsigned_int;
313 
314  return iter - scalar_names.begin();
315 }
316 
317 inline
318 unsigned int
319 FEVectors::find_vector(const std::string& name) const
320 {
321  const std::vector<std::string>::const_iterator
322  iter = std::find(vector_names.begin(), vector_names.end(), name);
323 
324  if(iter == vector_names.end())
325  return deal_II_numbers::invalid_unsigned_int;
326 
327  return iter - vector_names.begin();
328 }
329 
330 template<typename OUT>
331 inline
332 void
334  unsigned int verbosity) const
335 {
336  if(verbosity == 0)
337  return;
338  if(verbosity == 1)
339  {
340  out << std::endl << "Scalars:";
341  for(unsigned int i = 0; i < n_scalars(); ++i)
342  out << ' ' << '\"' << scalar_names[i] << '\"';
343 
344  out << "Vectors:";
345  for(unsigned int i = 0; i < n_vectors(); ++i)
346  out << ' ' << '\"' << vector_names[i] << '\"';
347  out << std::endl;
348  }
349  else
350  {
351  for(unsigned int i = 0; i < n_scalars(); ++i)
352  {
353  out << "Scalar-" << i << ' ' << scalar_names[i] << '=' << *scalars[i];
354  out << std::endl;
355  }
356  for(unsigned int i = 0; i < n_vectors(); ++i)
357  {
358  out << "Vector-" << i << ' ' << vector_names[i];
359  for(unsigned int b = 0; b < vectors[i]->n_blocks(); ++b)
360  out << ' ' << vectors[i]->block(b).size();
361  out << " Norm: " << vectors[i]->l2_norm();
362  out << std::endl;
363  }
364  }
365 }
366 
367 }
368 
369 #endif