CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
poly2d_base.h
Go to the documentation of this file.
1 #ifndef poly2d_base_h
2 #define poly2d_base_h
3 
4 #include <iostream>
5 #include <fstream>
6 #include <iomanip>
7 #include <vector>
8 #include <set>
9 #include <cstring>
10 
11 #include <cmath>
12 #include <cfloat> //in order to use DBL_EPSILON (1+DBL_EPSILON > 1)
13 
15 // //
16 // The "poly2d_term" represent a term of a polynomial of 2 variables. //
17 // //
19 
20 //_______________________________________________________________________________
21 namespace magfieldparam {
22 
23  struct poly2d_term {
24  double coeff; //Coefficient of the term
25  unsigned np[2]; //Powers of variables
26 
27  poly2d_term() { memset(this, 0, sizeof(*this)); }
28  poly2d_term(double C, unsigned nr, unsigned nz) {
29  coeff = C;
30  np[0] = nr;
31  np[1] = nz;
32  }
33  void Print(std::ostream &out = std::cout, bool first_term = true);
34  };
35 
37  // //
38  // Base class that represent a polynomial of 2 variables. It isn't supposed //
39  // to be used directly and provides no way of setting coefficients directly. //
40  // Such methods must be defined in derived classes. //
41  // //
43 
44  //_______________________________________________________________________________
45  class poly2d_base { // a general polynomial of 2 variables
46 
47  protected:
48  //Group of static members for the class memory management
49  //----------------------------------------------------------------------------
50  static double rval; //last r-value used in calculation
51  static double zval; //last z-value used in calculation
52 
53  static double **rz_pow; //table with calculated r^n*z^m values
54  static unsigned NTab; //rz_pow table size
55  static unsigned NPwr; //max power in use by CLASS
56  static bool rz_set;
57 
58  static const double MIN_COEFF; //Threshold for assigning a coeff. to 0
59 
60  static std::set<poly2d_base *> poly2d_base_set; //Set of all poly2d_base objects
61  // static std::set<poly2d_base*, less<poly2d_base*> > poly2d_base_set; //Set of all poly2d_base objects
62 
63  static void SetTabSize(const unsigned N); //Set rz-table size
64  static void FillTable(const double r, const double z);
65 
66  static void AdjustTab();
67  //----------------------------------------------------------------------------
68 
69  std::vector<poly2d_term> data; //polynomial terms
70  unsigned max_pwr; //max power in use by INSTANCE
71 
72  public:
73  static void IncNPwr(const unsigned N) {
74  if (N > NPwr)
75  NPwr = N;
76  }
77  static int GetMaxPow();
78  static unsigned Count() { return poly2d_base_set.size(); }
79  static void PrintTab(std::ostream &out = std::cout, const std::streamsize prec = 5);
80 
81  static void SetPoint(const double r, const double z);
82 
84  max_pwr = 0;
85  poly2d_base_set.insert(this);
86  }
88  data = S.data;
89  max_pwr = S.max_pwr;
90  poly2d_base_set.insert(this);
91  }
92 
93  virtual ~poly2d_base();
94 
95  bool IsOn() { return bool(!data.empty()); }
96  bool IsRZSet() { return rz_set; }
97 
98  void Collect(); //Collect terms and remove zero terms
99  void Compress() { Collect(); }
100 
101  void Diff(int nvar); //differentiate the polynomial by variable# nvar
102  void Int(int nvar); //Integrate the polynomial by variable# nvar
103  void IncPow(int nvar); //Multiply the polynomial by variable# nvar
104  void DecPow(int nvar); //Divide the polynomial by variable# nvar
105 
106  void Scale(const double C);
107  // poly2d_base& operator*=(const double C) { Scale(C); return *this;}
108 
109  double Eval(); //Evaluation with no check that rz_pow table exist
110  double GetVal() {
111  if (rz_set)
112  return Eval();
113  else
114  return 0.;
115  }
116  double GetVal(const double r, const double z) {
117  SetPoint(r, z);
118  return Eval();
119  }
120 
121  void Print(std::ostream &out = std::cout, const std::streamsize prec = 5);
122 
123  }; //Class poly2d_base
124 } // namespace magfieldparam
125 
126 #endif
static unsigned NTab
Definition: poly2d_base.h:54
void Print(std::ostream &out=std::cout, const std::streamsize prec=5)
Definition: poly2d_base.cc:214
std::vector< poly2d_term > data
Definition: poly2d_base.h:69
poly2d_base(const poly2d_base &S)
Definition: poly2d_base.h:87
static void SetTabSize(const unsigned N)
Definition: poly2d_base.cc:73
double GetVal(const double r, const double z)
Definition: poly2d_base.h:116
static double ** rz_pow
Definition: poly2d_base.h:53
poly2d_term(double C, unsigned nr, unsigned nz)
Definition: poly2d_base.h:28
static std::set< poly2d_base * > poly2d_base_set
Definition: poly2d_base.h:60
void Scale(const double C)
Definition: poly2d_base.cc:307
static void SetPoint(const double r, const double z)
Definition: poly2d_base.cc:151
void Print(std::ostream &out=std::cout, bool first_term=true)
Definition: poly2d_base.cc:12
static void FillTable(const double r, const double z)
Definition: poly2d_base.cc:93
static void IncNPwr(const unsigned N)
Definition: poly2d_base.h:73
#define N
Definition: blowfish.cc:9
static void PrintTab(std::ostream &out=std::cout, const std::streamsize prec=5)
Definition: poly2d_base.cc:127
static unsigned NPwr
Definition: poly2d_base.h:55
tuple cout
Definition: gather_cfg.py:144
static const double MIN_COEFF
Definition: poly2d_base.h:58
static unsigned Count()
Definition: poly2d_base.h:78