From 2e4360687ebbc201b2f31ec9664e60dd63330ec9 Mon Sep 17 00:00:00 2001 From: Nathan Miller Date: Tue, 11 Jun 2024 09:57:35 -0600 Subject: [PATCH] FEAT: Added the Jacobians of the balance of mass --- src/cpp/tardigrade_balance_of_mass.cpp | 39 ++++++ src/cpp/tardigrade_balance_of_mass.h | 5 + .../tests/test_tardigrade_balance_of_mass.cpp | 131 +++++++++++++++++- 3 files changed, 174 insertions(+), 1 deletion(-) diff --git a/src/cpp/tardigrade_balance_of_mass.cpp b/src/cpp/tardigrade_balance_of_mass.cpp index 961f88c..d5bd520 100644 --- a/src/cpp/tardigrade_balance_of_mass.cpp +++ b/src/cpp/tardigrade_balance_of_mass.cpp @@ -37,6 +37,45 @@ namespace tardigradeBalanceEquations{ } + void computeBalanceOfMass( const floatType &density, const floatType &density_dot, const floatVector &density_gradient, + const floatVector &velocity, const secondOrderTensor &velocity_gradient, floatType &mass_change_rate, + floatType &dCdRho, floatType &dCdRhoDot, floatVector &dCdGradRho, + floatVector &dCdV, secondOrderTensor &dCdGradV ){ + /*! + * Compute the value of the balance of mass returning the value of the mass change rate including the Jacobians + * + * \f$ \frac{\partial \rho}{\partial t} + \left( \rho v_i \right)_{,i} = c \f$ + * + * \param &density: The value of the density \f$ \rho \f$ + * \param &density_dot: The value of the partial derivative of the density \f$ \frac{\partial \rho}{\partial t} \f$ + * \param &density_gradient: The value of the spatial gradient of the density \f$ \rho_{,i} \f$ + * \param &velocity: The value of the velocity \f$ v_i \f$ + * \param &velocity_gradient: The value of the spatial gradient of the velocity \f$ v_{i,j} \f$ + * \param &mass_change_rate: The rate of change of the mass \f$ c \f$ + */ + + computeBalanceOfMass( density, density_dot, density_gradient, velocity, velocity_gradient, mass_change_rate ); + + dCdRho = 0; + + dCdRhoDot = 1; + + dCdGradRho = velocity; + + dCdV = density_gradient; + + std::fill( std::begin( dCdGradV ), std::end( dCdGradV ), 0 ); + + for ( unsigned int i = 0; i < dim; i++ ){ + + dCdRho += velocity_gradient[ dim * i + i ]; + + dCdGradV[ dim * i + i ] = density; + + } + + } + } } diff --git a/src/cpp/tardigrade_balance_of_mass.h b/src/cpp/tardigrade_balance_of_mass.h index 941b323..e2a7a99 100644 --- a/src/cpp/tardigrade_balance_of_mass.h +++ b/src/cpp/tardigrade_balance_of_mass.h @@ -31,6 +31,11 @@ namespace tardigradeBalanceEquations{ void computeBalanceOfMass( const floatType &density, const floatType &density_dot, const floatVector &density_gradient, const floatVector &velocity, const secondOrderTensor &velocity_gradient, floatType &mass_change_rate ); + void computeBalanceOfMass( const floatType &density, const floatType &density_dot, const floatVector &density_gradient, + const floatVector &velocity, const secondOrderTensor &velocity_gradient, floatType &mass_change_rate, + floatType &dCdRho, floatType &dCdRhoDot, floatVector &dCdGradRho, + floatVector &dCdV, secondOrderTensor &dCdGradV ); + } } diff --git a/src/cpp/tests/test_tardigrade_balance_of_mass.cpp b/src/cpp/tests/test_tardigrade_balance_of_mass.cpp index 22156c2..2704f7b 100644 --- a/src/cpp/tests/test_tardigrade_balance_of_mass.cpp +++ b/src/cpp/tests/test_tardigrade_balance_of_mass.cpp @@ -71,5 +71,134 @@ BOOST_AUTO_TEST_CASE( test_computeBalanceOfMass, * boost::unit_test::tolerance( BOOST_TEST( answer == result ); -} + floatType dCdRho, dCdRhoDot; + + floatVector dCdGradRho, dCdV; + + secondOrderTensor dCdGradV; + + tardigradeBalanceEquations::balanceOfMass::computeBalanceOfMass( density, density_dot, density_gradient, velocity, velocity_gradient, result, + dCdRho, dCdRhoDot, dCdGradRho , dCdV, dCdGradV ); + + BOOST_TEST( answer == result ); + + floatType eps = 1e-6; + + // Derivative w.r.t. density + for ( unsigned int i = 0; i < 1; i++ ){ + + floatType delta = eps * std::fabs( density ) + eps; + + floatType xp = density + delta; + + floatType xm = density - delta; + + floatType vp; + + floatType vm; + + tardigradeBalanceEquations::balanceOfMass::computeBalanceOfMass( xp, density_dot, density_gradient, velocity, velocity_gradient, vp ); + + tardigradeBalanceEquations::balanceOfMass::computeBalanceOfMass( xm, density_dot, density_gradient, velocity, velocity_gradient, vm ); + + BOOST_TEST( dCdRho == ( vp - vm ) / ( 2 * delta ) ); + + } + + // Derivative w.r.t. density dot + for ( unsigned int i = 0; i < 1; i++ ){ + + floatType delta = eps * std::fabs( density_dot ) + eps; + + floatType xp = density_dot + delta; + + floatType xm = density_dot - delta; + + floatType vp; + + floatType vm; + + tardigradeBalanceEquations::balanceOfMass::computeBalanceOfMass( density, xp, density_gradient, velocity, velocity_gradient, vp ); + + tardigradeBalanceEquations::balanceOfMass::computeBalanceOfMass( density, xm, density_gradient, velocity, velocity_gradient, vm ); + + BOOST_TEST( dCdRhoDot == ( vp - vm ) / ( 2 * delta ) ); + + } + + // Derivative w.r.t. density gradient + for ( unsigned int i = 0; i < 3; i++ ){ + + floatType delta = eps * std::fabs( density_gradient[ i ] ) + eps; + + floatVector xp = density_gradient; + + floatVector xm = density_gradient; + + xp[ i ] += delta; + + xm[ i ] -= delta; + floatType vp; + + floatType vm; + + tardigradeBalanceEquations::balanceOfMass::computeBalanceOfMass( density, density_dot, xp, velocity, velocity_gradient, vp ); + + tardigradeBalanceEquations::balanceOfMass::computeBalanceOfMass( density, density_dot, xm, velocity, velocity_gradient, vm ); + + BOOST_TEST( dCdGradRho[ i ] == ( vp - vm ) / ( 2 * delta ) ); + + } + + // Derivative w.r.t. velocity + for ( unsigned int i = 0; i < 3; i++ ){ + + floatType delta = eps * std::fabs( velocity[ i ] ) + eps; + + floatVector xp = velocity; + + floatVector xm = velocity; + + xp[ i ] += delta; + + xm[ i ] -= delta; + + floatType vp; + + floatType vm; + + tardigradeBalanceEquations::balanceOfMass::computeBalanceOfMass( density, density_dot, density_gradient, xp, velocity_gradient, vp ); + + tardigradeBalanceEquations::balanceOfMass::computeBalanceOfMass( density, density_dot, density_gradient, xm, velocity_gradient, vm ); + + BOOST_TEST( dCdV[ i ] == ( vp - vm ) / ( 2 * delta ) ); + + } + + // Derivative w.r.t. velocity gradient + for ( unsigned int i = 0; i < 9; i++ ){ + + floatType delta = eps * std::fabs( velocity_gradient[ i ] ) + eps; + + secondOrderTensor xp = velocity_gradient; + + secondOrderTensor xm = velocity_gradient; + + xp[ i ] += delta; + + xm[ i ] -= delta; + + floatType vp; + + floatType vm; + + tardigradeBalanceEquations::balanceOfMass::computeBalanceOfMass( density, density_dot, density_gradient, velocity, xp, vp ); + + tardigradeBalanceEquations::balanceOfMass::computeBalanceOfMass( density, density_dot, density_gradient, velocity, xm, vm ); + + BOOST_TEST( dCdGradV[ i ] == ( vp - vm ) / ( 2 * delta ) ); + + } + +}