Version: SMASH-2.2
density.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2013-2021
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 
10 #include "smash/density.h"
11 #include "smash/constants.h"
12 #include "smash/logging.h"
13 
14 namespace smash {
15 
16 double density_factor(const ParticleType &type, DensityType dens_type) {
17  switch (dens_type) {
19  return type.is_hadron() ? 1. : 0.;
21  return static_cast<double>(type.baryon_number());
23  return type.is_baryon() || type.is_nucleus() ? type.isospin3_rel() : 0.;
24  case DensityType::Pion:
25  return type.pdgcode().is_pion() ? 1. : 0.;
27  return type.is_hadron() ? type.isospin3() : 0.;
29  return static_cast<double>(type.charge());
31  return static_cast<double>(type.strangeness());
32  default:
33  return 0.;
34  }
35 }
36 
37 std::pair<double, ThreeVector> unnormalized_smearing_factor(
38  const ThreeVector &r, const FourVector &p, const double m_inv,
39  const DensityParameters &dens_par, const bool compute_gradient) {
40  const double r_sqr = r.sqr();
41  // Distance from particle to point of interest > r_cut
42  if (r_sqr > dens_par.r_cut_sqr()) {
43  return std::make_pair(0.0, ThreeVector(0.0, 0.0, 0.0));
44  }
45 
46  const FourVector u = p * m_inv;
47  const double u_r_scalar = r * u.threevec();
48  const double r_rest_sqr = r_sqr + u_r_scalar * u_r_scalar;
49 
50  // Lorentz contracted distance from particle to point of interest > r_cut
51  if (r_rest_sqr > dens_par.r_cut_sqr()) {
52  return std::make_pair(0.0, ThreeVector(0.0, 0.0, 0.0));
53  }
54  const double sf = std::exp(-r_rest_sqr * dens_par.two_sig_sqr_inv()) * u.x0();
55  const ThreeVector sf_grad = compute_gradient
56  ? sf * (r + u.threevec() * u_r_scalar) *
57  dens_par.two_sig_sqr_inv() * 2.0
58  : ThreeVector(0.0, 0.0, 0.0);
59 
60  return std::make_pair(sf, sf_grad);
61 }
62 
64 template <typename /*ParticlesContainer*/ T>
65 std::tuple<double, FourVector, ThreeVector, ThreeVector, FourVector, FourVector,
66  FourVector, FourVector>
67 current_eckart_impl(const ThreeVector &r, const T &plist,
68  const DensityParameters &par, DensityType dens_type,
69  bool compute_gradient, bool smearing) {
70  /* The current density of the positively and negatively charged particles.
71  * Division into positive and negative charges is necessary to avoid
72  * problems with the Eckart frame definition. Example of problem:
73  * get Eckart frame for two identical oppositely flying bunches of
74  * electrons and positrons. For this case jmu = (0, 0, 0, non-zero),
75  * so jmu.abs does not exist and Eckart frame is not defined.
76  * If one takes rho = jmu_pos.abs - jmu_neg.abs, it is still Lorentz-
77  * invariant and gives the right limit in non-relativistic case, but
78  * it gives no such problem. */
79  FourVector jmu_pos, jmu_neg;
80  /* The array of the derivatives of the current density.
81  * The zeroth component is the time derivative,
82  * while the next 3 ones are spacial derivatives. */
83  std::array<FourVector, 4> djmu_dxnu;
84 
85  for (const auto &p : plist) {
86  if (par.only_participants()) {
87  // if this conditions holds, the hadron is a spectator
88  if (p.get_history().collisions_per_particle == 0) {
89  continue;
90  }
91  }
92  const double dens_factor = density_factor(p.type(), dens_type);
93  if (std::fabs(dens_factor) < really_small) {
94  continue;
95  }
96  const FourVector mom = p.momentum();
97  const double m = mom.abs();
98  if (m < really_small) {
99  continue;
100  }
101  const double m_inv = 1.0 / m;
102  const auto sf_and_grad = unnormalized_smearing_factor(
103  p.position().threevec() - r, mom, m_inv, par, compute_gradient);
104  const FourVector tmp = mom * (dens_factor / mom.x0());
105  if (smearing) {
106  if (dens_factor > 0.) {
107  jmu_pos += tmp * sf_and_grad.first;
108  } else {
109  jmu_neg += tmp * sf_and_grad.first;
110  }
111  } else {
112  if (dens_factor > 0.) {
113  jmu_pos += tmp;
114  } else {
115  jmu_neg += tmp;
116  }
117  }
118  if (compute_gradient) {
119  for (int k = 1; k <= 3; k++) {
120  djmu_dxnu[k] += tmp * sf_and_grad.second[k - 1];
121  djmu_dxnu[0] -= tmp * sf_and_grad.second[k - 1] *
122  tmp.threevec()[k - 1] / dens_factor;
123  }
124  }
125  }
126 
127  // Eckart density (rest frame density)
128  const double rho_eck = (jmu_pos.abs() - jmu_neg.abs()) * par.norm_factor_sf();
129 
130  // $\partial_t j^{\mu}$
131  const FourVector djmu_dt = compute_gradient
132  ? djmu_dxnu[0] * par.norm_factor_sf()
133  : FourVector(0.0, 0.0, 0.0, 0.0);
134  // $\partial_x j^{\mu}$
135  const FourVector djmu_dx = compute_gradient
136  ? djmu_dxnu[1] * par.norm_factor_sf()
137  : FourVector(0.0, 0.0, 0.0, 0.0);
138  // $\partial_y j^{\mu}$
139  const FourVector djmu_dy = compute_gradient
140  ? djmu_dxnu[2] * par.norm_factor_sf()
141  : FourVector(0.0, 0.0, 0.0, 0.0);
142  // $\partial_z j^{\mu}$
143  const FourVector djmu_dz = compute_gradient
144  ? djmu_dxnu[3] * par.norm_factor_sf()
145  : FourVector(0.0, 0.0, 0.0, 0.0);
146 
147  // Gradient of j0
148  ThreeVector grad_j0 = ThreeVector(0.0, 0.0, 0.0);
149  // Curl of the 3-current density
150  ThreeVector curl_vecj = ThreeVector(0.0, 0.0, 0.0);
151  if (compute_gradient) {
152  curl_vecj.set_x1(djmu_dxnu[2].x3() - djmu_dxnu[3].x2());
153  curl_vecj.set_x2(djmu_dxnu[3].x1() - djmu_dxnu[1].x3());
154  curl_vecj.set_x3(djmu_dxnu[1].x2() - djmu_dxnu[2].x1());
155  curl_vecj *= par.norm_factor_sf();
156  for (int i = 1; i < 4; i++) {
157  grad_j0[i - 1] += djmu_dxnu[i].x0() * par.norm_factor_sf();
158  }
159  }
160  return std::make_tuple(rho_eck, (jmu_pos + jmu_neg) * par.norm_factor_sf(),
161  grad_j0, curl_vecj, djmu_dt, djmu_dx, djmu_dy,
162  djmu_dz);
163 }
164 
165 std::tuple<double, FourVector, ThreeVector, ThreeVector, FourVector, FourVector,
166  FourVector, FourVector>
167 current_eckart(const ThreeVector &r, const ParticleList &plist,
168  const DensityParameters &par, DensityType dens_type,
169  bool compute_gradient, bool smearing) {
170  return current_eckart_impl(r, plist, par, dens_type, compute_gradient,
171  smearing);
172 }
173 std::tuple<double, FourVector, ThreeVector, ThreeVector, FourVector, FourVector,
174  FourVector, FourVector>
175 current_eckart(const ThreeVector &r, const Particles &plist,
176  const DensityParameters &par, DensityType dens_type,
177  bool compute_gradient, bool smearing) {
178  return current_eckart_impl(r, plist, par, dens_type, compute_gradient,
179  smearing);
180 }
181 
186  RectangularLattice<std::array<FourVector, 4>> *four_grad_lattice,
187  const LatticeUpdate update, const DensityType dens_type,
188  const DensityParameters &par, const std::vector<Particles> &ensembles,
189  const double time_step, const bool compute_gradient) {
190  // Do not proceed if lattice does not exists/update not required
191  if (lat == nullptr || lat->when_update() != update) {
192  return;
193  }
194  const std::array<int, 3> lattice_n_cells = lat->n_cells();
195  const int number_of_nodes =
196  lattice_n_cells[0] * lattice_n_cells[1] * lattice_n_cells[2];
197 
198  /*
199  * Take the provided DensityOnLattice lattice and use the information about
200  * the current to create a lattice of current FourVectors. Because the lattice
201  * hasn't been updated at this point yet, it provides the t_0 time step
202  * information on the currents.
203  */
204  // copy values of jmu at t_0 onto old_jmu;
205  // proceed only if finite difference gradients are calculated
207  for (int i = 0; i < number_of_nodes; i++) {
208  old_jmu->assign_value(i, ((*lat)[i]).jmu_net());
209  }
210  }
211 
212  update_lattice(lat, update, dens_type, par, ensembles, compute_gradient);
213 
214  // calculate the gradients for finite difference derivatives
216  // copy values of jmu FourVectors at t_0 + time_step onto new_jmu
217  for (int i = 0; i < number_of_nodes; i++) {
218  new_jmu->assign_value(i, ((*lat)[i]).jmu_net());
219  }
220 
221  // compute time derivatives and gradients of all components of jmu
222  new_jmu->compute_four_gradient_lattice(*old_jmu, time_step,
223  *four_grad_lattice);
224 
225  // substitute new derivatives
226  int node_number = 0;
227  for (auto &node : *lat) {
228  auto tmp = (*four_grad_lattice)[node_number];
229  node.overwrite_djmu_dxnu(tmp[0], tmp[1], tmp[2], tmp[3]);
230  node_number++;
231  }
232  } // if (par.derivatives() == DerivativesMode::FiniteDifference)
233 
234  // calculate gradients of rest frame density
236  for (auto &node : *lat) {
237  // the rest frame density
238  double rho = node.rho();
239  const int sgn = rho > 0 ? 1 : -1;
240  if (std::abs(rho) < very_small_double) {
241  rho = sgn * very_small_double;
242  }
243 
244  // the computational frame j^mu
245  const FourVector jmu = node.jmu_net();
246  // computational frame array of derivatives of j^mu
247  const std::array<FourVector, 4> djmu_dxnu = node.djmu_dxnu();
248 
249  const double drho_dt =
250  (1 / rho) *
251  (jmu.x0() * djmu_dxnu[0].x0() - jmu.x1() * djmu_dxnu[0].x1() -
252  jmu.x2() * djmu_dxnu[0].x2() - jmu.x3() * djmu_dxnu[0].x3());
253 
254  const double drho_dx =
255  (1 / rho) *
256  (jmu.x0() * djmu_dxnu[1].x0() - jmu.x1() * djmu_dxnu[1].x1() -
257  jmu.x2() * djmu_dxnu[1].x2() - jmu.x3() * djmu_dxnu[1].x3());
258 
259  const double drho_dy =
260  (1 / rho) *
261  (jmu.x0() * djmu_dxnu[2].x0() - jmu.x1() * djmu_dxnu[2].x1() -
262  jmu.x2() * djmu_dxnu[2].x2() - jmu.x3() * djmu_dxnu[2].x3());
263 
264  const double drho_dz =
265  (1 / rho) *
266  (jmu.x0() * djmu_dxnu[3].x0() - jmu.x1() * djmu_dxnu[3].x1() -
267  jmu.x2() * djmu_dxnu[3].x2() - jmu.x3() * djmu_dxnu[3].x3());
268 
269  const FourVector drho_dxnu = {drho_dt, drho_dx, drho_dy, drho_dz};
270 
271  node.overwrite_drho_dxnu(drho_dxnu);
272  }
273  } // if (par.rho_derivatives() == RestFrameDensityDerivatives::On){
274 } // void update_lattice()
275 
276 std::ostream &operator<<(std::ostream &os, DensityType dens_type) {
277  switch (dens_type) {
278  case DensityType::Hadron:
279  os << "hadron density";
280  break;
281  case DensityType::Baryon:
282  os << "baryon density";
283  break;
285  os << "baryonic isospin density";
286  break;
287  case DensityType::Pion:
288  os << "pion density";
289  break;
291  os << "total isospin3 density";
292  break;
293  case DensityType::None:
294  os << "none";
295  break;
296  default:
297  os.setstate(std::ios_base::failbit);
298  }
299  return os;
300 }
301 
302 } // namespace smash
A class to pre-calculate and store parameters relevant for density calculation.
Definition: density.h:108
RestFrameDensityDerivativesMode rho_derivatives() const
Definition: density.h:147
bool only_participants() const
Definition: density.h:169
DerivativesMode derivatives() const
Definition: density.h:145
double two_sig_sqr_inv() const
Definition: density.h:161
double norm_factor_sf() const
Definition: density.h:167
double r_cut_sqr() const
Definition: density.h:159
The FourVector class holds relevant values in Minkowski spacetime with (+, −, −, −) metric signature.
Definition: fourvector.h:33
double x3() const
Definition: fourvector.h:320
double x2() const
Definition: fourvector.h:316
double abs() const
calculate the lorentz invariant absolute value
Definition: fourvector.h:459
ThreeVector threevec() const
Definition: fourvector.h:324
double x0() const
Definition: fourvector.h:308
double x1() const
Definition: fourvector.h:312
Particle type contains the static properties of a particle species.
Definition: particletype.h:97
bool is_baryon() const
Definition: particletype.h:203
int isospin3() const
Definition: particletype.h:176
int strangeness() const
Definition: particletype.h:212
bool is_nucleus() const
Definition: particletype.h:245
PdgCode pdgcode() const
Definition: particletype.h:156
int32_t charge() const
The charge of the particle.
Definition: particletype.h:188
bool is_hadron() const
Definition: particletype.h:197
double isospin3_rel() const
Definition: particletype.h:179
int baryon_number() const
Definition: particletype.h:209
The Particles class abstracts the storage and manipulation of particles.
Definition: particles.h:33
bool is_pion() const
Definition: pdgcode.h:384
A container class to hold all the arrays on the lattice and access them.
Definition: lattice.h:47
LatticeUpdate when_update() const
Definition: lattice.h:169
void compute_four_gradient_lattice(RectangularLattice< FourVector > &old_lat, double time_step, RectangularLattice< std::array< FourVector, 4 >> &grad_lat) const
Compute a fourgradient on a lattice of FourVectors jmu via the finite difference method.
Definition: lattice.h:418
const std::array< int, 3 > & n_cells() const
Definition: lattice.h:157
void assign_value(int lattice_index, T value)
Overwrite with a template value T at a given node.
Definition: lattice.h:193
The ThreeVector class represents a physical three-vector with the components .
Definition: threevector.h:31
void set_x1(double x)
set first component
Definition: threevector.h:167
double sqr() const
Definition: threevector.h:259
void set_x3(double z)
set third component
Definition: threevector.h:175
void set_x2(double y)
set second component
Definition: threevector.h:171
Collection of useful constants that are known at compile time.
std::ostream & operator<<(std::ostream &out, const ActionPtr &action)
Convenience: dereferences the ActionPtr to Action.
Definition: action.h:532
constexpr int p
Proton.
int sgn(T val)
Signum function.
Definition: random.h:190
Definition: action.h:24
void update_lattice(RectangularLattice< T > *lat, const LatticeUpdate update, const DensityType dens_type, const DensityParameters &par, const std::vector< Particles > &ensembles, const bool compute_gradient)
Updates the contents on the lattice.
Definition: density.h:535
std::tuple< double, FourVector, ThreeVector, ThreeVector, FourVector, FourVector, FourVector, FourVector > current_eckart(const ThreeVector &r, const ParticleList &plist, const DensityParameters &par, DensityType dens_type, bool compute_gradient, bool smearing)
Calculates Eckart rest frame density and 4-current of a given density type and optionally the gradien...
Definition: density.cc:167
constexpr double very_small_double
A very small double, used to avoid division by zero.
Definition: constants.h:40
std::tuple< double, FourVector, ThreeVector, ThreeVector, FourVector, FourVector, FourVector, FourVector > current_eckart_impl(const ThreeVector &r, const T &plist, const DensityParameters &par, DensityType dens_type, bool compute_gradient, bool smearing)
Calculates Eckart rest frame density and 4-current of a given density type and optionally the gradien...
Definition: density.cc:67
LatticeUpdate
Enumerator option for lattice updates.
Definition: lattice.h:36
std::pair< double, ThreeVector > unnormalized_smearing_factor(const ThreeVector &r, const FourVector &p, const double m_inv, const DensityParameters &dens_par, const bool compute_gradient=false)
Implements gaussian smearing for any quantity.
Definition: density.cc:37
constexpr double really_small
Numerical error tolerance.
Definition: constants.h:37
DensityType
Allows to choose which kind of density to calculate.
Definition: density.h:36
double density_factor(const ParticleType &type, DensityType dens_type)
Get the factor that determines how much a particle contributes to the density type that is computed.
Definition: density.cc:16