Skip to content

Commit

Permalink
FEAT: Added the Jacobians of the balance of mass
Browse files Browse the repository at this point in the history
  • Loading branch information
NateAM committed Jun 11, 2024
1 parent a4507f5 commit 2e43606
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 1 deletion.
39 changes: 39 additions & 0 deletions src/cpp/tardigrade_balance_of_mass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}

}

}

}
Expand Down
5 changes: 5 additions & 0 deletions src/cpp/tardigrade_balance_of_mass.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

}

}
Expand Down
131 changes: 130 additions & 1 deletion src/cpp/tests/test_tardigrade_balance_of_mass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );

}

}

0 comments on commit 2e43606

Please sign in to comment.