Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: Stochastic rescaling with Pade approximation #99

Open
wants to merge 10 commits into
base: dev
Choose a base branch
from
3 changes: 3 additions & 0 deletions include/linearAlgebra/staticMatrix/staticMatrix3x3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ namespace linearAlgebra
template <typename T>
[[nodiscard]] StaticMatrix3x3<T> exp(const StaticMatrix3x3<T> &mat);

template <typename T>
[[nodiscard]] StaticMatrix3x3<T> expPade(const StaticMatrix3x3<T> &mat);

} // namespace linearAlgebra

#include "staticMatrix3x3.tpp.hpp" // DO NOT MOVE THIS LINE
Expand Down
30 changes: 29 additions & 1 deletion include/linearAlgebra/staticMatrix/staticMatrix3x3.tpp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

#include "staticMatrix3x3.hpp"

#include "staticMatrix3x3Class.hpp" // for StaticMatrix3x3
#include "vector3d.hpp" // for Vector3D

galjos marked this conversation as resolved.
Show resolved Hide resolved
namespace linearAlgebra
{
/**
Expand Down Expand Up @@ -458,7 +461,32 @@ namespace linearAlgebra
auto result = StaticMatrix3x3<T>(0.0);

for (size_t i = 0; i < 3; ++i)
for (size_t j = i + 1; j < 3; ++j) result[i][j] = ::exp(mat[i][j]);
for (size_t j = 0; j < 3; ++j) result[i][j] = ::exp(mat[i][j]);

return result;
}

/**
* @brief Pade approximation of the exponential of a StaticMatrix3x3
*
* @link https://en.wikipedia.org/wiki/Matrix_exponential
* @link https://en.wikipedia.org/wiki/Pad%C3%A9_table
* @link https://en.wikipedia.org/wiki/Pad%C3%A9_approximant
* @link
* https://github.com/bussilab/crescale/blob/master/simplemd_anisotropic/simplemd.cpp#L351
*/
template <typename T>
[[nodiscard]] StaticMatrix3x3<T> expPade(const StaticMatrix3x3<T> &mat)
{
auto result = StaticMatrix3x3<T>(0.0);

auto mat2 = mat * mat;
auto mat3 = mat2 * mat;

auto den = diagonalMatrix(1.0) - 0.5 * mat + 0.1 * mat2 - mat3 / 120.0;
auto num = diagonalMatrix(1.0) + 0.5 * mat + 0.1 * mat2 + mat3 / 120.0;

result = inverse(den) * num;

return result;
}
Expand Down
1 change: 1 addition & 0 deletions include/manostat/manostat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ namespace manostat
virtual ~Manostat() = default;

void calculatePressure(const pq::SimBox &, pq::PhysicalData &);
void rotateMu(linearAlgebra::tensor3D &mu);
virtual void applyManostat(pq::SimBox &, pq::PhysicalData &);

virtual pq::ManostatType getManostatType() const;
Expand Down
10 changes: 2 additions & 8 deletions src/manostat/berendsenManostat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ SemiIsotropicBerendsenManostat::SemiIsotropicBerendsenManostat(
)
: BerendsenManostat(targetPressure, tau, compressibility),
_2DAnisotropicAxis(anisotropicAxis),
_2DIsotropicAxes(isotropicAxes){};
_2DIsotropicAxes(isotropicAxes) {};

/**
* @brief apply Berendsen manostat for NPT ensemble
Expand Down Expand Up @@ -178,13 +178,7 @@ tensor3D FullAnisotropicBerendsenManostat::calculateMu() const

auto mu = kronecker - preFactor * (pTarget - _pressureTensor);

mu[0][1] += mu[1][0];
mu[0][2] += mu[2][0];
mu[1][2] += mu[2][1];

mu[1][0] = 0.0;
mu[2][0] = 0.0;
mu[2][1] = 0.0;
rotateMu(mu);

return mu;
}
Expand Down
20 changes: 20 additions & 0 deletions src/manostat/manostat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ using namespace simulationBox;
using namespace physicalData;
using namespace constants;
using namespace settings;
using namespace linearAlgebra;

/**
* @brief Construct a new Manostat:: Manostat object
Expand Down Expand Up @@ -66,6 +67,25 @@ void Manostat::calculatePressure(const SimulationBox &box, PhysicalData &data)
data.setPressure(_pressure);
}

/**
* @brief rotate mu back into upper diagonal space
*
* @details first order approximation of mu rotation according to gromacs
* @link https://manual.gromacs.org/current/reference-manual/algorithms/molecular-dynamics.html
*
* @param mu
*/
void Manostat::rotateMu(tensor3D &mu)
{
mu[0][1] += mu[1][0];
mu[0][2] += mu[2][0];
mu[1][2] += mu[2][1];

mu[1][0] = 0.0;
mu[2][0] = 0.0;
mu[2][1] = 0.0;
}

/**
* @brief apply dummy manostat for NVT ensemble
*
Expand Down
9 changes: 6 additions & 3 deletions src/manostat/stochasticRescalingManostat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ StochasticRescalingManostat::StochasticRescalingManostat(
: Manostat(other),
_tau(other._tau),
_compressibility(other._compressibility),
_dt(other._dt){};
_dt(other._dt) {};

/**
* @brief Construct a new Stochastic Rescaling Manostat:: Stochastic Rescaling
Expand All @@ -78,7 +78,7 @@ SemiIsotropicStochasticRescalingManostat::
)
: StochasticRescalingManostat(targetPressure, tau, compressibility),
_2DAnisotropicAxis(anisotropicAxis),
_2DIsotropicAxes(isotropicAxes){};
_2DIsotropicAxes(isotropicAxes) {};

/**
* @brief Construct a new Stochastic Rescaling Manostat:: Stochastic Rescaling
Expand Down Expand Up @@ -251,8 +251,11 @@ tensor3D FullAnisotropicStochasticRescalingManostat::calculateMu(
stochasticFactor = ::sqrt(stochasticFactor) * random;

const auto deltaP = diagonalMatrix(_targetPressure) - _pressureTensor;
auto mu = expPade(-compress * deltaP / 3.0 + stochasticFactor);

return exp(-compress * deltaP / 3.0 + stochasticFactor);
rotateMu(mu);

return mu;
}

/***************************
Expand Down
Loading