Canoe
Comprehensive Atmosphere N' Ocean Engine
xiz_h2h2_cia.cpp
Go to the documentation of this file.
1 // C/C++
2 #include <fstream>
3 #include <iostream>
4 #include <sstream>
5 #include <stdexcept>
6 
7 // application
8 #include <application/exceptions.hpp>
9 
10 // canoe
11 #include <air_parcel.hpp>
12 #include <constants.hpp>
13 
14 // climath
15 #include <climath/core.h>
16 #include <climath/interpolation.h>
17 
18 // utils
19 #include <utils/fileio.hpp>
20 
21 // opacity
22 #include "hydrogen_cia.hpp"
23 
24 void XizH2H2CIA::LoadCoefficient(std::string fname, int bid) {
25  if (!FileExists(fname)) {
26  throw NotFoundError("XizH2H2CIA", fname);
27  }
28  len_[1] = GetNumCols(fname) - 1;
29  len_[0] = GetNumRows(fname) - 1;
30 
31  std::ifstream infile(fname.c_str(), std::ios::in);
32  axis_.resize(len_[0] + len_[1]);
33  kcoeff_.resize(len_[0] * len_[1]);
34  Real junk;
35  if (infile.is_open()) {
36  infile >> junk;
37  for (int j = 0; j < len_[1]; j++) {
38  infile >> axis_[len_[0] + j];
39  }
40  for (int k = 0; k < len_[0]; k++) {
41  infile >> axis_[k];
42  for (int j = 0; j < len_[1]; j++) infile >> kcoeff_[k * len_[1] + j];
43  }
44  infile.close();
45  } else {
46  throw RuntimeError("XizH2H2CIA::LoadCoefficient",
47  "Cannot open file: " + fname);
48  }
49 }
50 
51 Real XizH2H2CIA::GetAttenuation(Real wave1, Real wave2,
52  AirParcel const& var) const {
53  // first axis is wavenumber, second is temperature
54  Real val, coord[2] = {wave1, var.w[IDN]};
55  interpn(&val, coord, kcoeff_.data(), axis_.data(), len_, 2, 1);
56 
57  Real amagat = var.w[IPR] / (Constants::kBoltz * var.w[IDN] * Constants::Lo);
58  Real x0 = 1.;
59  if (GetSpeciesIndex(0) == 0) {
60  for (int n = 1; n <= NVAPOR; ++n) x0 -= var.w[n];
61  } else {
62  x0 = var.w[GetSpeciesIndex(0)];
63  }
64 
65  return 100. * exp(-val) *
66  sqr(x0 * amagat * GetPar<Real>("mixr")); // 1/cm -> 1/m
67 }
Real *const w
Definition: air_parcel.hpp:36
int GetSpeciesIndex(int n) const
size_t len_[2]
std::vector< Real > kcoeff_
void LoadCoefficient(std::string fname, int bid=-1)
Real GetAttenuation(Real wave1, Real wave2, AirParcel const &var) const
Get attenuation coefficient [1/m].
std::vector< Real > axis_
double sqr(double x)
Definition: core.h:14
int GetNumCols(std::string fname, char c)
get number of columns in a data table
Definition: fileio.cpp:50
int GetNumRows(std::string fname)
get number of rows in a data table
Definition: fileio.cpp:62
bool FileExists(std::string fname)
test file existance
Definition: fileio.cpp:16
void interpn(double *val, double const *coor, double const *data, double const *axis, size_t const *len, int ndim, int nval)
Definition: interpn.c:12
double const Lo
Definition: constants.hpp:9
double const kBoltz
Definition: constants.hpp:7