rl_raylib  1.1.10
rl_DielectricPODArray.cc
1 // File: rl_DielectricPODArray.cc
2 // Author: Terry Gaetz
3 
4 // --8<--8<--8<--8<--
5 //
6 // Copyright (C) 2006, 2007 Smithsonian Astrophysical Observatory
7 //
8 // This file is part of rl_raylib
9 //
10 // rl_raylib is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License
12 // as published by the Free Software Foundation; either version 2
13 // of the License, or (at your option) any later version.
14 //
15 // rl_raylib is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the
22 // Free Software Foundation, Inc.
23 // 51 Franklin Street, Fifth Floor
24 // Boston, MA 02110-1301, USA
25 //
26 // -->8-->8-->8-->8--
27 
28 // Description: read in dielectric constant information from rdb table;
29 // organize into bsearchable array
30 //
31 // History
32 //--------
33 // 0.3.0 tjg 1998-Jul-08 split out into rl_raylibsup
34 // 0.2.0 tjg 1998-Jun-23 convert to C++
35 // 0.1.0 tjg 1998-Jun-17 rename to dielectricdata.c; extensive revisions.
36 // 0.0.1 tjg 1995-May-18 original
37 
38 #include <rl_DielectricPODArray.h> // rl_DielectricPODArray
39 
40 //#include <rl_Traits.h>
41 
42 #include <cstdio> // fopen
43 #include <cstdlib> // qsort
44 #include <cstring>
45 #include <cmath>
46 
47 using namespace std;
48 
49 typedef enum
50 {
51  Ealpha_gamma
52 } ERfldataType;
53 
54 // forward declaration
55 extern "C" {
56  int diel_compare( const void *ck, // search key
57  const void *ce // array element
58  );
59 }
60 
61 //-------------------------------------------------------------------------
62 // dtor, ctors...
63 
66 { if ( data_ ) { delete [] data_; } }
67 
70  : nelts_(0), data_(0)
71 {}
72 
74 rl_DielectricPODArray( size_t nelts,
75  double const* energy,
76  double const* alpha,
77  double const* gamma )
78  : nelts_( nelts )
79 {
80  /*
81  * allocate array to hold data
82  */
83 
84  try {
85 
87 
88  } catch ( std::exception& bad_alloc ) {
89 
90  char msg[256] = "rl_DielectricPODArray(nelts,energy,alpha,gamma): "
91  "unable to allocate rl_DielectricPOD array";
92 
93  throw rl_Exception( msg );
94  }
95 
96  init( nelts, energy, alpha, gamma );
97 }
98 
100 rl_DielectricPODArray( size_t nelts,
102  : nelts_( nelts )
103 {
104  try {
105 
107 
108  } catch ( std::exception& bad_alloc ) {
109 
110  char msg[] = "rl_DielectricPODArray(nelts,diel): "
111  "unable to allocate rl_DielectricPOD array";
112 
113  throw rl_Exception( msg );
114  }
115 
116  init( nelts, diel );
117 }
118 
120 init( size_t nelts,
121  double const* energy,
122  double const* alpha,
123  double const* gamma )
124 {
125  for ( size_t n = 0 ; n < nelts ; ++n )
126  {
127  data_[n].energy_ = energy[n];
128  data_[n].alpha_ = alpha[n];
129  data_[n].gamma_ = gamma[n];
130  }
131  /*
132  * sort on energy...
133  */
134  qsort( data_, nelts_, sizeof( rl_Traits::rl_DielectricPOD ), diel_compare );
135 }
136 
138 init( size_t nelts,
140 {
141  for ( size_t n = 0 ; n < nelts ; ++n )
142  {
143  data_[n].energy_ = diel[n].energy_;
144  data_[n].alpha_ = diel[n].alpha_;
145  data_[n].gamma_ = diel[n].gamma_;
146  }
147  /*
148  * sort on energy...
149  */
150  qsort( data_, nelts_, sizeof( rl_Traits::rl_DielectricPOD ), diel_compare );
151 }
152 
154 cprint_on( std::FILE* of, char const pre[], char const pst[] ) const
155 {
156  if ( std::strlen(pre) ) { std::fprintf(of, "%s", pre); }
157  for ( size_t i = 0 ; i < nelts_ ; ++i )
158  {
159  std::fprintf(of, "%.15e %.15e %.15e\n",
160  data_[i].energy_, data_[i].alpha_, data_[i].gamma_);
161  }
162  if ( std::strlen(pst) ) { std::fprintf(of, "%s", pst); }
163 }
164 
165 int diel_compare( const void *ck, // search key
166  const void *ce // array element
167  )
168 {
169  double *pk = (double*) ck;
171 
172  if ( *pk < pe->energy_ )
173  { return -1; }
174  else if ( *pk > pe->energy_ )
175  { return +1; }
176  else
177  { return 0; }
178 }
179 
rl_Traits::rl_DielectricPOD * data_
pointer to the data
double alpha_
dielectric decrement, real part
Definition: rl_Traits.h:98
The exception thrown by the rl_RayLib and rl_RaySupLib libraries.
Definition: rl_Exception.h:36
double gamma_
dielectric decrement, imag part
Definition: rl_Traits.h:100
void init(size_t nelts, double const *energy, double const *alpha, double const *gamma)
Initializer.
size_t nelts_
number of dielectric decrements read in
double energy_
energy (keV)
Definition: rl_Traits.h:96
void cprint_on(std::FILE *of, char const pre[]="", char const pst[]="") const
Accessor.
rl_DielectricPODArray()
Default constructor.