nec2++  1.7.0
BaseInput.h
1 /*
2  Copyright (C) 2004 Timothy C.A. Molteno
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #ifndef __Base_Input__
19 #define __Base_Input__
20 
21 #include "math_util.h"
22 
23 #include <fstream>
24 #include <sstream>
25 
26 nec_float diff(nec_float a, nec_float b)
27 {
28  if (a == b)
29  return 0;
30  if ((a < 1e-8) && (b < 1e-8))
31  return 0;
32 
33  nec_float sub = a - b;
34  nec_float sum = a + b;
35 
36  nec_float ret = sub*sub;
37  if ((a != 0) && (b != 0))
38  ret /= sum*sum;
39  ret = sqrt(ret);
40  if (ret > 1e-2)
41  {
42  cout << " diff(" << a << "," << b << ") = " << ret << endl;
43  }
44  return ret;
45 }
46 
47 nec_float diff(nec_complex a, nec_complex b)
48 {
49  if (a == b)
50  return 0;
51  if ((abs(a) < 1e-8) && (abs(b) < 1e-8))
52  return 0;
53 
54  nec_float ret = norm(a - b);
55  nec_float sum = norm(a + b);
56 
57  if (sum != 0)
58  ret /= sum;
59 
60  ret = sqrt(ret);
61  if (ret > 1e-2)
62  {
63  stringstream ss;
64  cout << " diff(" << a << "," << b << ") = " << ret << endl;
65  }
66  return ret;
67 }
68 
69 class BaseInput
70 {
71 public:
72  BaseInput(const std::string& filename)
73  : m_filename(filename),
74  m_stream(filename.c_str())
75  {
76  }
77 
78 private:
79  char linec[512];
80 
81 
82 protected:
83 
84  std::string m_filename;
85  std::ifstream m_stream;
86 
87  string readline()
88  {
89  m_stream.getline(&linec[0],512);
90  return string(&linec[0]);
91  }
92 
93  double read_sci(std::istream& is)
94  {
95  double x;
96  is.setf(ios_base::skipws);
97  is >> x;
98  return x;
99  }
100 
101  double read_fixed(std::istream& is)
102  {
103  double x;
104  is.setf(ios_base::skipws);
105  is >> x;
106  return x;
107  }
108 };
109 
110 #endif /* __Base_Input__ */
Definition: BaseInput.h:69