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