BubbleProfiler  0.3.0
by Peter Athron, Csaba Balazs, Michael Bardsley, Andrew Fowlie, Dylan Harries & Graham White
perturbations_ode_system.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of BubbleProfiler.
3  *
4  * BubbleProfiler is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * BubbleProfiler is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with BubbleProfiler. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
19 #include "error.hpp"
20 #include "field_profiles.hpp"
21 #include "potential.hpp"
22 
23 #include <cmath>
24 #include <sstream>
25 
26 namespace BubbleProfiler {
27 
29  Potential& potential_, Field_profiles& profiles_,
30  int n_spacetime_dimensions_)
31  : n_fields(potential_.get_number_of_fields())
32  , n_spacetime_dimensions(n_spacetime_dimensions_)
33  , potential(&potential_)
34  , profiles(&profiles_)
35 {
36 }
37 
39  const Eigen::VectorXd& x, Eigen::VectorXd& dxdt, double rho) const
40 {
41  if (rho <= 0.) {
42  throw Domain_error("Perturbations_ODE_system::operator(): "
43  "spatial coordinate must be positive");
44  }
45 
46  if (!is_finite(x)) {
47  std::ostringstream msg;
48  msg << "Perturbations_ODE_system::operator(): "
49  << "perturbations contained a nan at rho = " << rho;
50  throw Numerical_error(msg.str());
51  }
52 
53  if (dxdt.rows() != 2 * n_fields) {
54  dxdt.resize(2 * n_fields, Eigen::NoChange);
55  }
56 
57  dxdt.segment(0, n_fields) = x.segment(n_fields, n_fields);
58  dxdt.segment(n_fields, n_fields) = calculate_mass_matrix(rho)
59  * x.segment(0, n_fields) - ((n_spacetime_dimensions - 1) / rho)
60  * x.segment(n_fields, n_fields) + calculate_inhomogeneities(rho);
61 
62  if (!is_finite(dxdt)) {
63  std::ostringstream msg;
64  msg << "Perturbations_ODE_system::operator(): "
65  << "derivatives contained a nan at rho = " << rho;
66  throw Numerical_error(msg.str());
67  }
68 }
69 
70 bool Perturbations_ODE_system::is_finite(const Eigen::VectorXd& x) const
71 {
72 #if EIGEN_VERSION_AT_LEAST(3,2,0)
73  return x.allFinite();
74 #else
75  const int n = x.size();
76  for (int i = 0; i < n; ++i) {
77  if (!std::isfinite(x(i))) {
78  return false;
79  }
80  }
81  return true;
82 #endif
83 }
84 
86  double rho) const
87 {
88  using Index = Eigen::VectorXd::Index;
89 
90  Eigen::VectorXd inh(Eigen::VectorXd::Zero(n_fields));
91  const auto field_values = profiles->evaluate_at(rho);
92 
93  for (Index i = 0; i < n_fields; ++i) {
94  inh(i) = potential->partial(field_values, i)
95  - profiles->derivative_at(i, 2, rho)
96  - ((n_spacetime_dimensions - 1.) / rho)
97  * profiles->derivative_at(i, 1, rho);
98  }
99 
100  return inh;
101 }
102 
104  double rho) const
105 {
106  using Index = Eigen::MatrixXd::Index;
107 
108  Eigen::MatrixXd mass_matrix(n_fields, n_fields);
109  const auto field_values = profiles->evaluate_at(rho);
110 
111  for (Index j = 0; j < n_fields; ++j) {
112  for (Index i = j; i < n_fields; ++i) {
113  mass_matrix(i, j)
114  = potential->partial(field_values, i, j);
115  }
116  }
117 
118  for (Index j = 0; j < n_fields; ++j) {
119  for (Index i = 0; i < j; ++i) {
120  mass_matrix(i, j) = mass_matrix(j, i);
121  }
122  }
123 
124  return mass_matrix;
125 }
126 
127 } // namespace BubbleProfiler
contains the definition of the Field_profiles clas
double derivative_at(int field, int order, double rho) const
Take the radial derivative of a field at a given radius.
Exception indicating function evaluation out of allowed domain.
Definition: error.hpp:110
Perturbations_ODE_system(Potential &potential_, Field_profiles &profiles_, int n_spacetime_dimensions_)
Constructs the system of ODEs for the given potential.
virtual double partial(const Eigen::VectorXd &coords, int i) const =0
Partial derivative WRT coordinate i at a point.
bool is_finite(const Eigen::VectorXd &) const
Eigen::VectorXd calculate_inhomogeneities(double) const
Eigen::VectorXd evaluate_at(double rho) const
Get all field values at a given radius.
void operator()(const Eigen::VectorXd &eps, Eigen::VectorXd &depsdr, double r) const
Calculate the derivatives of the perturbations.
Eigen::MatrixXd calculate_mass_matrix(double) const
Abstract base class for a generic potential.
Definition: potential.hpp:36
Exception indicating generic numerical error.
Definition: error.hpp:116
Discretized set of field profiles.