rl_DielectricPODArray.cc

00001 // File:   rl_DielectricPODArray.cc
00002 // Author: Terry Gaetz
00003 
00004 // --8<--8<--8<--8<--
00005 //
00006 // Copyright (C) 2006, 2007 Smithsonian Astrophysical Observatory
00007 //
00008 // This file is part of rl_raylib
00009 //
00010 // rl_raylib is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU General Public License
00012 // as published by the Free Software Foundation; either version 2
00013 // of the License, or (at your option) any later version.
00014 //
00015 // rl_raylib is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU General Public License
00021 // along with this program; if not, write to the 
00022 //       Free Software Foundation, Inc. 
00023 //       51 Franklin Street, Fifth Floor
00024 //       Boston, MA  02110-1301, USA
00025 //
00026 // -->8-->8-->8-->8--
00027 
00028 // Description: read in dielectric constant information from rdb table; 
00029 //              organize into bsearchable array
00030 //
00031 // History
00032 //--------
00033 // 0.3.0  tjg  1998-Jul-08 split out into rl_raylibsup
00034 // 0.2.0  tjg  1998-Jun-23 convert to C++
00035 // 0.1.0  tjg  1998-Jun-17 rename to dielectricdata.c; extensive revisions.
00036 // 0.0.1  tjg  1995-May-18 original
00037 
00038 #include <rl_DielectricPODArray.h>      // rl_DielectricPODArray
00039 
00040 //#include <rl_Traits.h>
00041 
00042 #include <cstdio>                     // fopen
00043 #include <cstdlib>                    // qsort
00044 #include <cstring>
00045 #include <cmath>
00046 
00047 using namespace std;
00048 
00049 typedef enum
00050 {
00051   Ealpha_gamma
00052 } ERfldataType;
00053 
00054 // forward declaration
00055 extern "C" {
00056   int diel_compare( const void      *ck,   // search key
00057                     const void      *ce    // array element
00058                   );
00059 }
00060 
00061 //-------------------------------------------------------------------------
00062 // dtor, ctors...
00063 
00064 rl_DielectricPODArray::
00065 ~rl_DielectricPODArray()
00066 { if ( data_ ) { delete [] data_; } }
00067 
00068 rl_DielectricPODArray::
00069 rl_DielectricPODArray( )
00070   : nelts_(0), data_(0)
00071 {}
00072 
00073 rl_DielectricPODArray::
00074 rl_DielectricPODArray( size_t        nelts,
00075                        double const* energy,
00076                        double const* alpha,
00077                        double const* gamma )
00078     throw ( rl_Exception )
00079   : nelts_( nelts )
00080 {
00081   /*
00082    * allocate array to hold data
00083    */
00084 
00085   try {
00086 
00087     data_ = new rl_Traits::rl_DielectricPOD[ nelts_ ];
00088 
00089   } catch ( std::exception& bad_alloc ) {
00090 
00091     char msg[256] = "rl_DielectricPODArray(nelts,energy,alpha,gamma): "
00092                     "unable to allocate rl_DielectricPOD array";
00093     
00094     throw rl_Exception( msg );
00095   }
00096 
00097   init( nelts, energy, alpha, gamma );
00098 }
00099 
00100 rl_DielectricPODArray::
00101 rl_DielectricPODArray( size_t                       nelts,
00102                        rl_Traits::rl_DielectricPOD* diel )
00103     throw ( rl_Exception )
00104   : nelts_( nelts )
00105 {
00106   try {
00107 
00108     data_ = new rl_Traits::rl_DielectricPOD[ nelts_ ];
00109 
00110   } catch ( std::exception& bad_alloc ) {
00111 
00112     char msg[] = "rl_DielectricPODArray(nelts,diel): "
00113                  "unable to allocate rl_DielectricPOD array";
00114 
00115     throw rl_Exception( msg );
00116   }
00117 
00118   init( nelts, diel );
00119 }
00120 
00121 void rl_DielectricPODArray::
00122 init( size_t        nelts,
00123       double const* energy,
00124       double const* alpha,
00125       double const* gamma )
00126 {
00127   for ( size_t n = 0 ; n < nelts ; ++n )
00128   {
00129     data_[n].energy_ = energy[n];
00130     data_[n].alpha_  = alpha[n];
00131     data_[n].gamma_  = gamma[n];
00132   }
00133   /* 
00134    * sort on energy...
00135    */
00136   qsort( data_, nelts_, sizeof( rl_Traits::rl_DielectricPOD ), diel_compare );
00137 }
00138 
00139 void rl_DielectricPODArray::
00140 init( size_t                       nelts,
00141       rl_Traits::rl_DielectricPOD* diel )
00142 {
00143   for ( size_t n = 0 ; n < nelts ; ++n )
00144   {
00145     data_[n].energy_ = diel[n].energy_;
00146     data_[n].alpha_  = diel[n].alpha_;
00147     data_[n].gamma_  = diel[n].gamma_;
00148   }
00149   /* 
00150    * sort on energy...
00151    */
00152   qsort( data_, nelts_, sizeof( rl_Traits::rl_DielectricPOD ), diel_compare );
00153 }
00154 
00155 void rl_DielectricPODArray::
00156 cprint_on( std::FILE* of, char const pre[], char const pst[] ) const
00157 {
00158   if ( std::strlen(pre) ) { std::fprintf(of, "%s", pre); }
00159   for ( size_t i = 0 ; i < nelts_ ; ++i )
00160   {
00161     std::fprintf(of, "%.15e %.15e %.15e\n",
00162        data_[i].energy_, data_[i].alpha_, data_[i].gamma_);
00163   }
00164   if ( std::strlen(pst) ) { std::fprintf(of, "%s", pst); }
00165 }
00166 
00167 int diel_compare( const void      *ck,   // search key
00168                   const void      *ce    // array element
00169                 )
00170 {
00171   double                      *pk = (double*)                     ck;
00172   rl_Traits::rl_DielectricPOD *pe = (rl_Traits::rl_DielectricPOD*)ce;
00173 
00174   if ( *pk < pe->energy_ )
00175   { return -1; }
00176   else if ( *pk > pe->energy_ )
00177   { return +1; }
00178   else
00179   { return 0; }
00180 }
00181 

Generated on Mon Nov 3 18:15:05 2008 for rl_raylib by  doxygen 1.5.6