CbmRoot
ThermalModel.cxx
Go to the documentation of this file.
1 #include "ThermalModel.h"
2 
3 using namespace std;
4 
5 
8 
9  void broyden2(vector<double>& xin,
10  vector<double> (*func)(const vector<double>&, ThermalModel*),
11  ThermalModel* ThM) {
12  const double TOLF = 1.0e-8, EPS = 1.0e-12;
13  const int MAXITS = 200;
14  vector<double> h = xin, xh1 = xin, xh2 = xin;
15  for (unsigned int i = 0; i < xin.size(); ++i) {
16  h[i] = EPS * abs(h[i]);
17  if (h[i] == 0.0) h[i] = EPS;
18  if (i == 0)
19  xh1[i] = xin[i] + h[i];
20  else
21  xh2[i] = xin[i] + h[i];
22  }
23  vector<double> r1 = func(xin, ThM), r21 = func(xh1, ThM),
24  r22 = func(xh2, ThM);
25  if (fabs(r1[0]) < TOLF && fabs(r1[1]) < TOLF) return;
26  double J[2][2];
27  J[0][0] = (r21[0] - r1[0]) / h[0];
28  J[0][1] = (r22[0] - r1[0]) / h[1];
29  J[1][0] = (r21[1] - r1[1]) / h[0];
30  J[1][1] = (r22[1] - r1[1]) / h[1];
31  double Jinv[2][2];
32  double det = J[0][0] * J[1][1] - J[0][1] * J[1][0];
33  //cout << xin[0] << " " << h[0] << " " << xin[1] << " " << h[1] << "\n";
34  //cout << J[0][0] << " " << J[0][1] << " " << J[1][0] << " " << J[1][1] << "\n";
35  if (det == 0.0) throw("singular Jacobian in broyden22");
36  Jinv[0][0] = J[1][1] / det;
37  Jinv[0][1] = -J[0][1] / det;
38  Jinv[1][0] = -J[1][0] / det;
39  Jinv[1][1] = J[0][0] / det;
40  vector<double> xold = xin;
41  vector<double> rold = r1;
42  vector<double> rprevten = r1;
43  for (int iter = 1; iter <= MAXITS; ++iter) {
44  //std::cout << iter << " " << xin[0] << " " << xin[1] << " " << r1[0] << " " << r1[1] << "\n";
45  xin[0] = xold[0] - Jinv[0][0] * rold[0] - Jinv[0][1] * rold[1];
46  xin[1] = xold[1] - Jinv[1][0] * rold[0] - Jinv[1][1] * rold[1];
47  r1 = func(xin, ThM);
48  if (fabs(r1[0]) < TOLF && fabs(r1[1]) < TOLF) {
49  //dbgstrm << "Broyden iterations = " << iter << endl;
50  return;
51  }
52  //if (iter%10==0) {
53  // if (abs(r1[0]/rprevten[0])>1e-2 && abs(r1[1]/rprevten[1])>1e-2) throw("broyden is stuck");
54  // rprevten = r1;
55  //}
56  //if (abs(r1[0])>10*abs(rold[0]) || abs(r1[1])>10*abs(rold[1])) throw("broyden doesn't work");
57  double JF[2];
58  double DF[2];
59  double dx[2];
60  DF[0] = (r1[0] - rold[0]);
61  DF[1] = (r1[1] - rold[1]);
62  /*if (abs(DF[0]/rold[0])>1e1 && abs(DF[1]/rold[1])>1e1 && !fl) {
63  xin[0] = 0.;
64  xin[1] = 0.;
65  broyden22(xin, func);
66  return;
67  }*/
68  JF[0] = Jinv[0][0] * DF[0] + Jinv[0][1] * DF[1];
69  JF[1] = Jinv[1][0] * DF[0] + Jinv[1][1] * DF[1];
70  dx[0] = xin[0] - xold[0];
71  dx[1] = xin[1] - xold[1];
72  double znam = dx[0] * JF[0] + dx[1] * JF[1];
73  if (znam == 0.0) throw("singular Jacobian in broyden22");
74  double xJ[2];
75  JF[0] = dx[0] - JF[0];
76  JF[1] = dx[1] - JF[1];
77  xJ[0] = dx[0] * Jinv[0][0] + dx[1] * Jinv[1][0];
78  xJ[1] = dx[0] * Jinv[0][1] + dx[1] * Jinv[1][1];
79  Jinv[0][0] = Jinv[0][0] + JF[0] * xJ[0] / znam;
80  Jinv[0][1] = Jinv[0][1] + JF[0] * xJ[1] / znam;
81  Jinv[1][0] = Jinv[1][0] + JF[1] * xJ[0] / znam;
82  Jinv[1][1] = Jinv[1][1] + JF[1] * xJ[1] / znam;
83  xold = xin;
84  rold = r1;
85  }
86  //std::cout << r1[0] << " " << r1[1] << "\n";
87  //dbgstrm << "Broyden iterations = " << MAXITS << endl;
88  throw("exceed MAXITS in broyden");
89  }
90 
91 
92  vector<double> function2(const vector<double>& xin, ThermalModel* ThM) {
93  vector<double> ret(2);
94  ThM->Parameters.muQ = xin[0];
95  ThM->Parameters.muS = xin[1];
96  ThM->CalculateDensities();
97  double fBd = ThM->CalculateBaryonDensity();
98  double fCd = ThM->CalculateChargeDensity();
99  double fSd = ThM->CalculateStrangenessDensity();
100  double fASd = ThM->CalculateAbsoluteStrangenessDensity();
101  ret[0] = (fCd / fBd - ThM->QBgoal) / ThM->QBgoal;
102  ret[1] = fSd / fASd;
103  return ret;
104  }
105 } // namespace ThermalModelNamespace
106 
107 using namespace ThermalModelNamespace;
108 
110 
112  if (fabs(Parameters.muB) < 1e-6) {
113  Parameters.muS = Parameters.muQ = 0.;
114  return;
115  }
116  vector<double> x2(2), xinit(2);
117  xinit[0] = x2[0] = Parameters.muQ;
118  xinit[1] = x2[1] = Parameters.muS;
120  int iter = 0, iterMAX = 10;
121  while (iter < iterMAX) {
122  try {
124  } catch (...) {}
125  if (fabs(Parameters.muB / Parameters.muS) > 1.
126  && fabs(Parameters.muB / Parameters.muQ) > 1.)
127  break;
128  xinit[0] = x2[0] = xinit[0] / 10.;
129  xinit[1] = x2[1] = xinit[1] / 10.;
130  iter++;
131  }
133 }
134 
135 
137  QBgoal = QB;
138  FixParameters();
139 }
ThermalModelParameters::muS
double muS
Definition: ThermalModelBase.h:7
ThermalModel
Definition: ThermalModel.h:9
h
Generates beam ions for transport simulation.
Definition: CbmBeamGenerator.h:17
ThermalModel::~ThermalModel
virtual ~ThermalModel(void)
Definition: ThermalModel.cxx:109
ThermalModelBase::QBgoal
double QBgoal
Definition: ThermalModelBase.h:27
ThermalModelNamespace::gThM
ThermalModel * gThM
Definition: ThermalModel.cxx:7
i
int i
Definition: L1/vectors/P4_F32vec4.h:25
ThermalModel::FixParameters
virtual void FixParameters()
Definition: ThermalModel.cxx:111
ThermalModelNamespace::function2
vector< double > function2(const vector< double > &xin, ThermalModel *ThM)
Definition: ThermalModel.cxx:92
ThermalModel::CalculateDensities
virtual void CalculateDensities()
Definition: ThermalModel.h:46
ThermalModelNamespace
Definition: ThermalModel.cxx:6
ThermalModel::CalculateAbsoluteStrangenessDensity
virtual double CalculateAbsoluteStrangenessDensity()
Definition: ThermalModel.h:119
ThermalModel::CalculateChargeDensity
virtual double CalculateChargeDensity()
Definition: ThermalModel.h:98
ThermalModel.h
ThermalModel::CalculateStrangenessDensity
virtual double CalculateStrangenessDensity()
Definition: ThermalModel.h:105
fabs
friend F32vec4 fabs(const F32vec4 &a)
Definition: L1/vectors/P4_F32vec4.h:60
ThermalModelParameters::muQ
double muQ
Definition: ThermalModelBase.h:7
ThermalModelBase::Parameters
ThermalModelParameters Parameters
Definition: ThermalModelBase.h:21
ThermalModelNamespace::broyden2
void broyden2(vector< double > &xin, vector< double >(*func)(const vector< double > &, ThermalModel *), ThermalModel *ThM)
Definition: ThermalModel.cxx:9
ThermalModel::CalculateBaryonDensity
virtual double CalculateBaryonDensity()
Definition: ThermalModel.h:91