OpenFCST: The open-source Fuel Cell Simulation Toolbox
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ideal_gas.h
Go to the documentation of this file.
1 //---------------------------------------------------------------------------
2 //
3 // Copyright (C) 2012 by Marc Secanell
4 //
5 // This file is subject to QPL and may not be distributed
6 // without copyright and license information. Please refer
7 // to the file deal.II/doc/license.html for the text and
8 // further information on this license.
9 //
10 //---------------------------------------------------------------------------
11 
12 #ifndef _FUELCELLSHOP__IDEAL_GAS_H
13 #define _FUELCELLSHOP__IDEAL_GAS_H
14 
15 namespace FuelCellShop
16 {
17  namespace Material
18  {
45  class IdealGas
46  {
47  public:
50  :
51  initialized(false)
52  {}
53 
56  {}
57 
61  void set_pT_and_gas(const double p, const double T, FuelCellShop::Material::PureGas* gas)
62  {
63  initialized = true;
64  double R = Constants::R();
65  press = p;
66  temp = T;
67  conc = press / (R*temp);
68  gas_type = gas;
69  density = (press * (gas_type->M*1.0e-3))/ (R*temp);
70  }
74  void set_cT_and_gas(const double c, const double T, FuelCellShop::Material::PureGas* gas)
75  {
76  initialized = true;
77  double R = Constants::R();
78  conc = c;
79  temp = T;
80  press = conc*R*temp;
81  gas_type = gas;
82  density = (press * (gas_type->M*1.0e-3))/ (R*temp);
83  }
84 
88  double get_T()
89  {
90  Assert (initialized, ExcNotInitialized());
91  return temp;
92  }
96  double get_p()
97  {
98  Assert (initialized, ExcNotInitialized());
99  return press;
100  }
104  double get_p_atm()
105  {
106  Assert (initialized, ExcNotInitialized());
107  return press/101325.0;
108  }
112  double get_c()
113  {
114  Assert (initialized, ExcNotInitialized());
115  return conc;
116  }
120  double get_density()
121  {
122  Assert (initialized, ExcNotInitialized());
123  return density;
124  }
129  {
130  Assert (initialized, ExcNotInitialized());
131  return gas_type;
132  }
137  {
138  Assert (initialized, ExcNotInitialized());
139  std::cout<<"Pressure is: "<< press <<" Pa"<<std::endl;
140  std::cout<<"Temperature is: "<< temp <<" K"<<std::endl;
141  std::cout<<"Concentration is: "<< conc <<" mol/m3"<<std::endl;
142  std::cout<<"Density is: "<< density <<" kg/m3"<<std::endl;
143  }
147  void test()
148  {
151  marc.set_pT_and_gas(101325, 298, &oxygen);
152  std::cout<<"======== TESTING set_pT_and_gas ========"<<std::endl;
153  if (marc.get_T() - 298 < 1e-4)
154  std::cout<<"======== Temp OK ========"<<std::endl;
155  else
156  std::cout<<"!!!!!!! Temp BUG !!!!!!!"<<std::endl;
157  // test pressure
158  if (marc.get_p() - 101325 < 1e-4)
159  std::cout<<"======== Pressure OK ========"<<std::endl;
160  else
161  std::cout<<"!!!!!!! Pressure BUG !!!!!!!"<<std::endl;
162  // test concentration
163  if (marc.get_c() - 40.8949 < 1e-4)
164  std::cout<<"======== Concentration OK ========"<<std::endl;
165  else
166  std::cout<<"!!!!!!! Concentration BUG !!!!!!!"<<std::endl;
167  // test density
168  if (marc.get_density() - 1.308592 < 1e-4)
169  std::cout<<"======== Density OK ========"<<std::endl;
170  else
171  std::cout<<"!!!!!!! Density BUG !!!!!!!"<<std::endl;
172 
173  marc.set_cT_and_gas(40.8949, 298, &oxygen);
174  std::cout<<"======== TESTING set_cT_and_gas ========"<<std::endl;
175  if (marc.get_T() - 298 < 1e-4)
176  std::cout<<"======== Temp OK ========"<<std::endl;
177  else
178  std::cout<<"!!!!!!! Temp BUG !!!!!!!"<<std::endl;
179  // test pressure
180  if (marc.get_p() - 101325 < 1e-4)
181  std::cout<<"======== Pressure OK ========"<<std::endl;
182  else
183  std::cout<<"!!!!!!! Pressure BUG !!!!!!!"<<std::endl;
184  // test concentration
185  if (marc.get_c() - 40.8949 < 1e-4)
186  std::cout<<"======== Concentration OK ========"<<std::endl;
187  else
188  std::cout<<"!!!!!!! Concentration BUG !!!!!!!"<<std::endl;
189  // test density
190  if (marc.get_density() - 1.308592 < 1e-4)
191  std::cout<<"======== Density OK ========"<<std::endl;
192  else
193  std::cout<<"!!!!!!! Density BUG !!!!!!!"<<std::endl;
194  }
195  private:
199  double temp;
201  double press;
203  double conc;
205  double density;
207  SmartPointer<FuelCellShop::Material::PureGas> gas_type;
208 
209  }; //class
210  } //namespace
211 
212  namespace Mixture
213  {
246  {
247  public:
250  :
251  initialized(false)
252  {}
253 
256  {}
260  void set_pT_xi_and_gases(const double p, const double T, const std::vector<double> xv, std::vector<FuelCellShop::Material::PureGas* > gases)
261  {
262  Assert (xv.size() == gases.size(),ExcDimensionMismatch(xv.size(),gases.size()));
263  initialized = true;
264  double R = Constants::R();
265  press = p;
266  temp = T;
267  conc = press / (R*temp);
268  concv.resize(xv.size());
269  pressv.resize(xv.size());
270  gas_types.resize(xv.size());
271  densityv.resize(xv.size());
272  M_mix = 0.0;
273  for (unsigned int i = 0; i<xv.size(); i++)
274  {
275  gas_types[i] = gases[i];
276  concv[i] = xv[i]*(press / (R*temp));
277  pressv[i] = xv[i]*press;
278  densityv[i] = (pressv[i] * (gas_types[i]->M*1.0e-3))/ (R*temp);
279  M_mix += xv[i]*gas_types[i]->M;
280  }
281  density = (press * (M_mix*1.0e-3))/ (R*temp);
282 
283  }
287  void set_cT_xi_and_gases(const double c, const double T, const std::vector<double> xv, std::vector<FuelCellShop::Material::PureGas* > gases)
288  {
289  Assert (xv.size() == gases.size(),ExcDimensionMismatch(xv.size(),gases.size()));
290  initialized = true;
291  double R = Constants::R();
292  conc = c;
293  temp = T;
294  press = c*R*T;
295  concv.resize(xv.size());
296  pressv.resize(xv.size());
297  gas_types.resize(xv.size());
298  densityv.resize(xv.size());
299  M_mix = 0.0;
300  for (unsigned int i = 0; i<xv.size(); i++)
301  {
302  gas_types[i] = gases[i];
303  concv[i] = xv[i]*(press / (R*temp));
304  pressv[i] = xv[i]*press;
305  densityv[i] = (pressv[i] * (gas_types[i]->M*1.0e-3))/ (R*temp);
306  M_mix += xv[i]*gas_types[i]->M;
307  }
308  density = (press * (M_mix*1.0e-3))/ (R*temp);
309 
310  }
314  void set_pv_T_and_gases(const std::vector<double> pv, const double T, std::vector<FuelCellShop::Material::PureGas* > gases)
315  {
316  Assert (pv.size() == gases.size(),ExcDimensionMismatch(pv.size(),gases.size()));
317  initialized = true;
318  double R = Constants::R();
319  pressv = pv;
320  temp = T;
321  concv.resize(pv.size());
322  gas_types.resize(pv.size());
323  densityv.resize(pv.size());
324  press = 0;
325  for (unsigned int i = 0; i<pv.size(); i++)
326  {
327  gas_types[i] = gases[i];
328  press += pv[i];
329  }
330  conc = press / (R*temp);
331  for (unsigned int i = 0; i<pv.size(); i++)
332  {
333  concv[i] = pv[i] / (R*temp);
334  densityv[i] = (pressv[i] * (gas_types[i]->M*1.0e-3))/ (R*temp);
335  M_mix += (pressv[i]/press)*gas_types[i]->M;
336  }
337  density = (press * (M_mix*1.0e-3))/ (R*temp);
338 
339  }
343  void set_cv_T_and_gases(const std::vector<double> cv, const double T, std::vector<FuelCellShop::Material::PureGas* > gases)
344  {
345  Assert (cv.size() == gases.size(),ExcDimensionMismatch(cv.size(),gases.size()));
346  initialized = true;
347  double R = Constants::R();
348  temp = T;
349  concv = cv;
350  pressv.resize(cv.size());
351  gas_types.resize(cv.size());
352  densityv.resize(cv.size());
353  conc = 0;
354  for (unsigned int i = 0; i<cv.size(); i++)
355  {
356  gas_types[i] = gases[i];
357  conc += cv[i];
358  }
359  press = conc*R*temp;
360  for (unsigned int i = 0; i<cv.size(); i++)
361  {
362  pressv[i] = concv[i]*R*temp;
363  densityv[i] = (pressv[i] * (gas_types[i]->M*1.0e-3))/ (R*temp);
364  M_mix += (pressv[i]/press)*gas_types[i]->M;
365  }
366  density = (press * (M_mix*1.0e-3))/ (R*temp);
367  }
371  double get_T()
372  {
373  Assert (initialized, ExcNotInitialized());
374  return temp;
375  }
379  double get_p()
380  {
381  Assert (initialized, ExcNotInitialized());
382  return press;
383  }
384 
388  std::vector<double> get_pi()
389  {
390  Assert (initialized, ExcNotInitialized());
391  return pressv;
392  }
393 
397  double get_p_atm()
398  {
399  Assert (initialized, ExcNotInitialized());
400  return press/101325.0;
401  }
405  double get_c()
406  {
407  Assert (initialized, ExcNotInitialized());
408  return conc;
409  }
413  std::vector<double> get_ci()
414  {
415  Assert (initialized, ExcNotInitialized());
416  return concv;
417  }
421  double get_density()
422  {
423  Assert (initialized, ExcNotInitialized());
424  return density;
425  }
429  std::vector<double> get_densityi()
430  {
431  Assert (initialized, ExcNotInitialized());
432  return densityv;
433  }
437  std::vector<double> get_xi()
438  {
439  Assert (initialized, ExcNotInitialized());
440  std::vector<double> xi;
441  xi.resize(pressv.size());
442  for (unsigned int i = 0; i<pressv.size(); ++i)
443  xi[i] = pressv[i]/press;
444 
445  return xi;
446  }
450  dealii::SmartPointer<FuelCellShop::Material::PureGas> get_gas(const unsigned int index)
451  {
452  Assert (initialized, ExcNotInitialized());
453  return gas_types[index];
454  }
459  {
460  Assert (initialized, ExcNotInitialized());
461  std::cout<<"Pressure is: "<< press <<" Pa"<<std::endl;
462  std::cout<<"Temperature is: "<< temp <<" K"<<std::endl;
463  std::cout<<"Concentration is: "<< conc <<" mol/m3"<<std::endl;
464  std::cout<<"Density is: "<< density <<" kg/m3"<<std::endl;
465 
466  for (unsigned int i = 0; i<concv.size(); i++)
467  {
468  std::cout<<"Parial pressure for species "<<i<<" is: "<< pressv[i] <<" Pa"<<std::endl;
469  std::cout<<"Concentration for species: "<<i<<" is: "<< concv[i] <<" mol/m3"<<std::endl;
470  std::cout<<"Density for species: "<<i<<" is: "<< densityv[i] <<" kg/m3"<<std::endl;
471  }
472  }
477  void test()
478  {
481  std::vector<FuelCellShop::Material::PureGas* > gasv;
483  gasv.push_back(&oxygen);
484  gasv.push_back(&nitrogen);
485  std::vector<double> v;
486  v.push_back(0.5*40.8949);
487  v.push_back(0.5*40.8949);
488  marc.set_cv_T_and_gases(v, 298, gasv);
489  // test temp
490  if (marc.get_T() - 298 < 1e-4)
491  std::cout<<"======== Temp OK ========"<<std::endl;
492  else
493  std::cout<<"!!!!!!! Temp BUG !!!!!!!"<<std::endl;
494  // test pressure
495  if (marc.get_p() - 101325 < 1e-4)
496  std::cout<<"======== Pressure OK ========"<<std::endl;
497  else
498  std::cout<<"!!!!!!! Pressure BUG !!!!!!!"<<std::endl;
499  // test concentration
500  if (marc.get_c() - 40.8949 < 1e-4)
501  std::cout<<"======== Concentration OK ========"<<std::endl;
502  else
503  std::cout<<"!!!!!!! Concentration BUG !!!!!!!"<<std::endl;
504  // test density
505  if (marc.get_density() - 1.22711 < 1e-4)
506  std::cout<<"======== Density OK ========"<<std::endl;
507  else
508  std::cout<<"!!!!!!! Density BUG !!!!!!!"<<std::endl;
509  // test gases
510  for (unsigned int i=0; i<marc.get_ci().size(); ++i)
511  if (marc.get_gas(i)->M - gasv[i]->M < 1e-4)
512  std::cout<<"======== Gas index "<<i<<" is OK ========"<<std::endl;
513  else
514  std::cout<<"!!!!!!! Gas index "<<i<<" has a BUG !!!!!!!"<<std::endl;
515  // test gases
516  for (unsigned int i=0; i<marc.get_ci().size(); ++i)
517  if (marc.get_ci()[i] - marc.get_c()*0.5 < 1e-4)
518  std::cout<<"======== Gas index "<<i<<" is OK ========"<<std::endl;
519  else
520  std::cout<<"!!!!!!! Gas index "<<i<<" has a BUG !!!!!!!"<<std::endl;
521  }
522  private:
526  double temp;
528  double press;
530  double conc;
532  double density;
534  double M_mix;
536  std::vector<double> pressv;
538  std::vector<double> concv;
540  std::vector<double> densityv;
542  std::vector<SmartPointer<FuelCellShop::Material::PureGas> > gas_types;
543 
544  }; //class
545  } //namespacie
546 
547 } //namespacie
548 
549 
550 #endif