OpenFCST: The open-source Fuel Cell Simulation Toolbox
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fcst_variables.h
Go to the documentation of this file.
1 // ------------------------------------------------------------------------------------------------
2 //
3 // FCST: Fuel Cell Simulation Toolbox
4 //
5 // Copyright (C) 2006-2013 by Energy Systems Design Laboratory, University of Alberta
6 //
7 // This software is distributed under the MIT License
8 // For more information, see the README file in /doc/LICENSE
9 //
10 // - Class: fcst_variables.h
11 // - Description: This structure keeps a name of an FCST solution variable
12 // and
13 // values of this variable in the quadrature points of a mesh entity
14 // - Developers: Phil Wardlaw, University of Alberta
15 // Madhur Bhaiya, University of Alberta
16 // Valentin N. Zingan, University of Alberta
17 //
18 // ------------------------------------------------------------------------------------------------
19 
20 #ifndef _FCST_FUELCELLSHOP_SOLUTION_VARIABLE_H_
21 #define _FCST_FUELCELLSHOP_SOLUTION_VARIABLE_H_
22 //-- C++ Standard libraries
23 #include <cmath>
24 #include <iostream>
25 
26 //-- dealII
27 #include <deal.II/base/point.h>
28 #include <deal.II/base/function.h>
29 #include <deal.II/lac/vector.h>
30 #include <deal.II/fe/fe_values.h>
31 
32 //--OpenFCST
34 
35 using namespace dealii;
36 
37 namespace FuelCellShop
38 {
87  {
88 
90 
91 
94  {
95  data = NULL;
96  initialized = false;
97  initialized_default_data = false;
98  initialized_data = false;
99  }
100 
104  SolutionVariable(const std::vector<double>* data_in, const VariableNames& name_in)
105  {
106  data = data_in;
107  name = name_in;
108  initialized = true;
109  initialized_default_data = false;
110  initialized_data = true;
111  }
112 
118  SolutionVariable(const double& value, const unsigned int& length, const VariableNames& name_in)
119  {
120  data = NULL;
121  default_data = std::vector<double>(length, value);
122  name = name_in;
123  initialized = true;
124  initialized_default_data = true;
125  initialized_data = false;
126  }
127 
134  SolutionVariable(const std::vector<double>& data_in, const VariableNames& name_in)
135  {
136  data = NULL;
137  default_data = data_in;
138  name = name_in;
139  initialized = true;
140  initialized_default_data = true;
141  initialized_data = false;
142  }
143 
145 
147 
148 
152  const std::vector<double>& get_default_data() const
153  {
154  Assert( initialized_default_data, ExcMessage("default_data is not initialized") );
155  return default_data;
156  }
157 
161  const std::vector<double>* get_data() const
162  {
163  Assert( initialized_data, ExcMessage("data is not initialized") );
164  return data;
165  }
166 
171  {
172  Assert( initialized, ExcMessage("SolutionVariable not initialized !!!") );
173  return name;
174  }
175 
179  const bool is_initialized() const
180  {
181  return initialized;
182  }
183 
184  const bool is_default_data_initialized() const
185  {
186  return initialized_default_data;
187  }
188 
189  const bool is_data_initialized() const
190  {
191  return initialized_data;
192  }
193 
197  unsigned int size() const
198  {
199  unsigned int answer = 0;
200  //If initialized and pointer is not NULL
201  if (initialized && (data != NULL))
202  answer = data->size();
203  else if (initialized && (data == NULL))
204  answer = default_data.size();
205 
206  return answer;
207  }
208 
210 
212 
213 
218  const double& operator[](const unsigned int& i) const
219  {
220  Assert( initialized, ExcMessage("SolutionVariables struct is not initialized !!!") );
221 
222  if (data != NULL)
223  {
224  Assert( i < data->size(), ExcMessage("Index is out of range in operator[] for SolutionVariables struct.") );
225  return data->at(i);
226  }
227  else if (data == NULL)
228  {
229  Assert( i < default_data.size(), ExcMessage("Index is out of range in operator[] for SolutionVariables struct.") );
230  return default_data.at(i);
231  }
232  }
233 
241  friend SolutionVariable operator* (const SolutionVariable& left,
242  const double& right)
243  {
244  Assert( left.is_default_data_initialized(), ExcMessage("SolutionVariable left operand default_data is not initialized") );
245 
246  std::vector<double> tmp(left.size());
247 
248  const VariableNames name = left.get_variablename();
249 
250  if( left.is_default_data_initialized() && !left.is_data_initialized() )
251  {
252  for(unsigned int q = 0; q < tmp.size(); ++q)
253  tmp[q] = left.get_default_data().at(q);
254 
255  for(unsigned int q = 0; q < tmp.size(); ++q)
256  tmp[q] *= right;
257 
258  return SolutionVariable(tmp,
259  name);
260  }
261  else if( !left.is_default_data_initialized() && left.is_data_initialized() )
262  {
263  AssertThrow( false, ExcNotImplemented() );
264  }
265  else
266  {
267  AssertThrow( false, ExcInternalError() );
268  }
269  }
270 
278  friend SolutionVariable operator* (const double& left,
279  const SolutionVariable& right)
280  {
281  Assert( right.is_default_data_initialized(), ExcMessage("SolutionVariable right operand default_data is not initialized") );
282 
283  std::vector<double> tmp(right.size());
284 
285  const VariableNames name = right.get_variablename();
286 
287  if( right.is_default_data_initialized() && !right.is_data_initialized() )
288  {
289  for(unsigned int q = 0; q < tmp.size(); ++q)
290  tmp[q] = right.get_default_data().at(q);
291 
292  for(unsigned int q = 0; q < tmp.size(); ++q)
293  tmp[q] *= left;
294 
295  return SolutionVariable(tmp,
296  name);
297  }
298  else if( !right.is_default_data_initialized() && right.is_data_initialized() )
299  {
300  AssertThrow( false, ExcNotImplemented() );
301  }
302  else
303  {
304  AssertThrow( false, ExcInternalError() );
305  }
306  }
307 
315  friend SolutionVariable operator/ (const SolutionVariable& left,
316  const double& right)
317  {
318  Assert( left.is_default_data_initialized(), ExcMessage("SolutionVariable left operand default_data is not initialized") );
319 
320  std::vector<double> tmp(left.size());
321 
322  const VariableNames name = left.get_variablename();
323 
324  if( left.is_default_data_initialized() && !left.is_data_initialized() )
325  {
326  for(unsigned int q = 0; q < tmp.size(); ++q)
327  tmp[q] = left.get_default_data().at(q);
328 
329  for(unsigned int q = 0; q < tmp.size(); ++q)
330  tmp[q] /= right;
331 
332  return SolutionVariable(tmp,
333  name);
334  }
335  else if( !left.is_default_data_initialized() && left.is_data_initialized() )
336  {
337  AssertThrow( false, ExcNotImplemented() );
338  }
339  else
340  {
341  AssertThrow( false, ExcInternalError() );
342  }
343  }
344 
345 
347 
348  private:
349 
351 
352 
358  std::vector<double> default_data;
359 
366  const std::vector<double>* data;
367 
372 
378 
384 
390 
392 
393  };
394 
396 
397 
401  static bool is_phiM(const SolutionVariable& sol_var)
402  {
403  return (sol_var.get_variablename() == protonic_electrical_potential);
404  }
405 
409  static bool is_phiS(const SolutionVariable& sol_var)
410  {
412  }
413 
415 
416 
457  class SolutionMap : private std::map< VariableNames, SolutionVariable>
458  {
459  public:
460 
474  void push_back(const SolutionVariable& a){
475 
476 
477  //Check that the key hasn't already been entered
478  if (this->count(a.get_variablename()) > 0)
479  throw std::runtime_error("You have already added a SolutionVariable of corresponding VariableNames type to SolutionMap");
480 
481 
482  //add the variable
483  this->operator [](a.get_variablename()) = a;
484  }
485 
490  return std::map<VariableNames, SolutionVariable>::at(key);
491  }
492 
496  void clear(){
497  std::map<VariableNames, SolutionVariable>::clear();
498  }
499 
503  void erase(const VariableNames& v){
504  std::map<VariableNames, SolutionVariable>::erase(v);
505  }
506 
507 
511  bool has(const VariableNames& v) const{
512  bool answer = false;
513 
514  if(this->count(v) >0)
515  answer = true;
516 
517  return answer;
518  }
519 
524  SolutionVariable temp = this->at(v);
525  std::map<VariableNames, SolutionVariable>::erase(v);
526  return temp;
527  }
528  };
529 }
530 #endif
const std::vector< double > * get_data() const
Definition: fcst_variables.h:161
void clear()
Expose std::map&lt;VariableNames, SolutionVariable&gt;::clear() interface publicly.
Definition: fcst_variables.h:496
bool has(const VariableNames &v) const
Find if a solution corresponding VariableNames type exist inside map.
Definition: fcst_variables.h:511
void erase(const VariableNames &v)
Expose std::map&lt;VariableNames, SolutionVariable&gt;::erase() interface publicly.
Definition: fcst_variables.h:503
const double & operator[](const unsigned int &i) const
Operator to access the value at a particular quadrature point in the cell.
Definition: fcst_variables.h:218
VariableNames get_variablename() const
Function to get the VariableNames enumeration corresponding to this struct.
Definition: fcst_variables.h:170
VariableNames name
FCST variable name stored in VariableNames enumeration.
Definition: fcst_variables.h:371
VariableNames
The enumeration containing the names of some of the available FCST solution variables and their deriv...
Definition: system_management.h:63
Convenient storage object for SolutionVariables.
Definition: fcst_variables.h:457
This structure is used to encapsulate data from constant values and variable solution data that is us...
Definition: fcst_variables.h:86
Definition: system_management.h:71
SolutionVariable pop(const VariableNames &v)
Returns and entry whilst removing it from the list.
Definition: fcst_variables.h:523
SolutionVariable(const std::vector< double > *data_in, const VariableNames &name_in)
Constructor for setting up the pointer to solution variable values and name of the solution variable...
Definition: fcst_variables.h:104
void push_back(const SolutionVariable &a)
Public function for adding SolutionVariable, uses the VariableNames stored within the SolutionVariabl...
Definition: fcst_variables.h:474
const bool is_default_data_initialized() const
Definition: fcst_variables.h:184
std::vector< double > default_data
Constant data.
Definition: fcst_variables.h:358
const std::vector< double > & get_default_data() const
Return a reference to the default_data stored in the class.
Definition: fcst_variables.h:152
const bool is_data_initialized() const
Definition: fcst_variables.h:189
bool initialized_default_data
true if default_data is initialized.
Definition: fcst_variables.h:383
SolutionVariable & at(VariableNames key)
Expose std::map&lt;VariableNames, SolutionVariable&gt;::at() interface publicly.
Definition: fcst_variables.h:489
bool initialized
true if either default_data or data is initialized.
Definition: fcst_variables.h:377
unsigned int size() const
Function to the length of the internal data element.
Definition: fcst_variables.h:197
static bool is_phiS(const SolutionVariable &sol_var)
Unary Predicate to return true if a SolutionVariable object belongs to electronic_electrical_potentia...
Definition: fcst_variables.h:409
SolutionVariable(const double &value, const unsigned int &length, const VariableNames &name_in)
Constructor to initialize the solution variable values, taking a default value and size of the vector...
Definition: fcst_variables.h:118
SolutionVariable()
Default Constructor.
Definition: fcst_variables.h:93
const std::vector< double > * data
Data in quadrature points of a mesh entity.
Definition: fcst_variables.h:366
Definition: system_management.h:72
static bool is_phiM(const SolutionVariable &sol_var)
Unary Predicate to return true if a SolutionVariable object belongs to protonic_electrical_potential...
Definition: fcst_variables.h:401
bool initialized_data
true if data is initialized.
Definition: fcst_variables.h:389
const bool is_initialized() const
Function to determine whether the structure is initialized or not.
Definition: fcst_variables.h:179
SolutionVariable(const std::vector< double > &data_in, const VariableNames &name_in)
Constructor to initialize the solution variable values, taking values as an input vector argument...
Definition: fcst_variables.h:134