OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
TrimCoilFit.cpp
Go to the documentation of this file.
2 
3 #include <cmath>
4 
6  double rmin,
7  double rmax,
8  const std::vector<double>& coefnum,
9  const std::vector<double>& coefdenom,
10  const std::vector<double>& coefnumphi,
11  const std::vector<double>& coefdenomphi):
12 
13  TrimCoil(bmax, rmin, rmax)
14 {
15  coefs.resize(4);
16  coefs[NUM] = coefnum;
17  coefs[DENOM] = coefdenom;
18  coefs[NUMPHI] = coefnumphi;
19  coefs[DENOMPHI] = coefdenomphi;
20 
21  // normal polynom if no denominator coefficients (denominator = 1)
22  if (coefs[DENOM].empty())
23  coefs[DENOM].push_back(1.0);
24  if (coefs[DENOMPHI].empty())
25  coefs[DENOMPHI].push_back(1.0);
26  // default constant nominator
27  if (coefs[NUM].empty())
28  coefs[NUM].push_back(1.0);
29  if (coefs[NUMPHI].empty())
30  coefs[NUMPHI].push_back(1.0);
31 }
32 
33 void TrimCoilFit::calculateRationalFunction(FunctionType type, double val, double& quot, double& der_quot) const
34 {
35  double num = 0.0; // numerator
36  double dnum = 0.0; // derivative of numerator
37  double powval = 1.0; // power of value
38 
39  const std::vector<double>& coefnum = coefs[type];
40  const std::vector<double>& coefdenom = coefs[type+1];
41 
42  // add constant
43  num += coefnum[0];
44  for (std::size_t i = 1; i < coefnum.size(); ++i) {
45  dnum += coefnum[i] * powval * i;
46  powval *= val;
47  num += coefnum[i] * powval;
48  }
49  double denom = 0.0; // denominator
50  double ddenom = 0.0; // derivative of denominator
51  powval = 1.0; // power of value
52 
53  // add constant
54  denom += coefdenom[0];
55  for (std::size_t i = 1; i < coefdenom.size(); ++i) {
56  ddenom += coefdenom[i] * powval * i;
57  powval *= val;
58  denom += coefdenom[i] * powval;
59  }
60 
61  quot = num / denom;
62  // derivative with quotient rule
63  der_quot = (dnum * denom - ddenom * num) / (denom*denom);
64 }
65 
66 void TrimCoilFit::calculateRationalFunction(FunctionType type, double val, double& quot, double& der_quot, double& der2_quot) const
67 {
68  double num = 0.0; // numerator
69  double d_num = 0.0; // derivative of numerator
70  double d2_num = 0.0; // second derivative of numerator
71  double powval = 1.0; // power of r
72 
73  const std::vector<double>& coefnum = coefs[type];
74  const std::vector<double>& coefdenom = coefs[type+1];
75 
76  unsigned int order = coefnum.size();
77 
78  // add constant and first term
79  num += coefnum[0];
80  if (order > 1) {
81  num += coefnum[1] * val;
82  d_num += coefnum[1];
83  }
84  for (std::size_t i = 2; i < coefnum.size(); ++i) {
85  d2_num += coefnum[i] * powval * i * (i-1);
86  powval *= val; // r^(i-1)
87  d_num += coefnum[i] * powval * i;
88  num += coefnum[i] * powval * val;
89  }
90 
91  double denom = 0.0; // denominator
92  double d_denom = 0.0; // derivative of denominator
93  double d2_denom = 0.0; // derivative of denominator
94  powval = 1.0; // power of r
95  order = coefdenom.size();
96 
97  // add constant
98  denom += coefdenom[0];
99  if (order > 1) {
100  denom += coefdenom[1] * val;
101  d_denom += coefdenom[1];
102  }
103  for (std::size_t i = 2; i < coefdenom.size(); ++i) {
104  d2_denom += coefdenom[i] * powval * i * (i-1);
105  powval *= val;
106  d_denom += coefdenom[i] * powval * i;
107  denom += coefdenom[i] * powval * val;
108  }
109 
110  quot = num / denom;
111 
112  // derivative of phase with quotient rule (B - field)
113  der_quot = (d_num * denom - d_denom * num) / (denom*denom);
114 
115  // second derivitive of phase (dB/dr) with quotient rule
116  // (d2_num - 2*(num/denom)' * d_denom - (num/denom) * d2_denom) / denom
117  der2_quot = (d2_num - 2*der_quot*d_denom - quot * d2_denom) / denom;
118 }
std::vector< std::vector< double > > coefs
rational function coefficients
Definition: TrimCoilFit.h:37
Abstract TrimCoil class.
Definition: TrimCoil.h:8
void calculateRationalFunction(FunctionType, double value, double &quot, double &der_quot) const
calculate rational function and its first derivative
Definition: TrimCoilFit.cpp:33
TrimCoilFit()=delete