Skip to content

Commit

Permalink
Merge pull request #109 from UCBoulder/feat/add-ability-for-residuals…
Browse files Browse the repository at this point in the history
…-to-set-initial-iterate-value

Feat/add ability for residuals to set initial iterate value
  • Loading branch information
NateAM authored May 24, 2024
2 parents 3d2404f + f404774 commit 1579a69
Show file tree
Hide file tree
Showing 13 changed files with 308 additions and 28 deletions.
3 changes: 3 additions & 0 deletions docs/sphinx/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ New Features
- Added a mass-change deformation gradient evolution model (:pull:`104`). By `Nathan Miller`_.
- Added the calculation of the total derivative of the unknown vector w.r.t. the additional degrees of freedom (:pull:`104`). By `Nathan Miller`_.
- Added storage for the derivative of the residual w.r.t. the additional dof (:pull:`104`). By `Nathan Miller`_.
- Added the ability to initialize the unknown vector (:pull:`109`). By `Nathan Miller`_.
- Added function that returns the size of the unknown vector (:pull:`109`). By `Nathan Miller`_.

Breaking Changes
================
Expand Down Expand Up @@ -44,6 +46,7 @@ Internal Changes
- Improved performance of the linear elasticity subroutine (:pull:`99`). By `Nathan Miller`_.
- Using new error_tools check for error function (:pull:`100`). By `Nathan Miller`_.
- Changed Jacobian, dRdF, and dRdD to row-major vectors (:pull:`101`). By `Nathan Miller`_.
- Replaced queries to getUnknownVector purely to get the size of the vector (:pull:`109`). By `Nathan Miller`_.

Bug Fixes
=========
Expand Down
36 changes: 27 additions & 9 deletions src/cpp/tardigrade_hydra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ namespace tardigradeHydra{
* Form a left preconditioner comprised of the inverse of the maximum value of each row
*/

const unsigned int problem_size = getUnknownVector( )->size( );
const unsigned int problem_size = getNumUnknowns( );

_preconditioner.second = floatVector( problem_size, 0 );

Expand Down Expand Up @@ -1246,7 +1246,25 @@ namespace tardigradeHydra{

Xmat[ Xmat.size( ) - 1 ] = *nonLinearSolveStateVariables;

setX( tardigradeVectorTools::appendVectors( Xmat ) );
floatVector X = tardigradeVectorTools::appendVectors( Xmat );

for ( auto residual_ptr = getResidualClasses( )->begin( ); residual_ptr != getResidualClasses( )->end( ); residual_ptr++ ){

std::vector< unsigned int > indices;

std::vector< floatType > values;

( *residual_ptr )->suggestInitialIterateValues( indices, values );

for ( auto i = indices.begin( ); i != indices.end( ); i++ ){

X[ *i ] = values[ ( unsigned int )( i - indices.begin( ) ) ];

}

}

setX( X );

}

Expand Down Expand Up @@ -1397,7 +1415,7 @@ namespace tardigradeHydra{

unsigned int rank;

floatVector deltaX( getUnknownVector( )->size( ), 0 );
floatVector deltaX( getNumUnknowns( ), 0 );

Eigen::Map< Eigen::Vector< floatType, -1 > > dx_map( deltaX.data( ), getUnknownVector( )->size( ) );

Expand Down Expand Up @@ -1517,14 +1535,14 @@ namespace tardigradeHydra{
// Form the maps for dXdF
Eigen::Map< const Eigen::Matrix< floatType, -1, -1, Eigen::RowMajor > > dRdFmat( getFlatdRdF( )->data( ), getResidual( )->size( ), *getConfigurationUnknownCount( ) );

_flatdXdF.second = floatVector( getUnknownVector( )->size( ) * ( *getConfigurationUnknownCount( ) ) );
Eigen::Map< Eigen::Matrix< floatType, -1, -1, Eigen::RowMajor > > dXdFmat( _flatdXdF.second.data( ), getUnknownVector( )->size( ), ( *getConfigurationUnknownCount( ) ) );
_flatdXdF.second = floatVector( getNumUnknowns( ) * ( *getConfigurationUnknownCount( ) ) );
Eigen::Map< Eigen::Matrix< floatType, -1, -1, Eigen::RowMajor > > dXdFmat( _flatdXdF.second.data( ), getNumUnknowns( ), ( *getConfigurationUnknownCount( ) ) );

// Form the maps for dXdT
Eigen::Map< const Eigen::Matrix< floatType, -1, -1, Eigen::RowMajor > > dRdTmat( getdRdT( )->data( ), getResidual( )->size( ), 1 );

_flatdXdT.second = floatVector( getUnknownVector( )->size( ) );
Eigen::Map< Eigen::Matrix< floatType, -1, -1, Eigen::RowMajor > > dXdTmat( _flatdXdT.second.data( ), getUnknownVector( )->size( ), 1 );
_flatdXdT.second = floatVector( getNumUnknowns( ) );
Eigen::Map< Eigen::Matrix< floatType, -1, -1, Eigen::RowMajor > > dXdTmat( _flatdXdT.second.data( ), getNumUnknowns( ), 1 );

// Solve
tardigradeVectorTools::solverType< floatType > solver;
Expand Down Expand Up @@ -1594,8 +1612,8 @@ namespace tardigradeHydra{
Eigen::Map< const Eigen::Matrix< floatType, -1, -1, Eigen::RowMajor > > dRdAdditionalDOF( getFlatdRdAdditionalDOF( )->data( ), getResidual( )->size( ), getAdditionalDOF( )->size( ) );

// Form the map for dXdF
_flatdXdAdditionalDOF.second = floatVector( getUnknownVector( )->size( ) * getAdditionalDOF( )->size( ), 0 );
Eigen::Map< Eigen::Matrix< floatType, -1, -1, Eigen::RowMajor > > dXdAdditionalDOF( _flatdXdAdditionalDOF.second.data( ), getUnknownVector( )->size( ), getAdditionalDOF( )->size( ) );
_flatdXdAdditionalDOF.second = floatVector( getNumUnknowns( ) * getAdditionalDOF( )->size( ), 0 );
Eigen::Map< Eigen::Matrix< floatType, -1, -1, Eigen::RowMajor > > dXdAdditionalDOF( _flatdXdAdditionalDOF.second.data( ), getNumUnknowns( ), getAdditionalDOF( )->size( ) );

// Solve
tardigradeVectorTools::solverType< floatType > solver;
Expand Down
20 changes: 20 additions & 0 deletions src/cpp/tardigrade_hydra.h
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,23 @@ namespace tardigradeHydra{

}

virtual void suggestInitialIterateValues( std::vector< unsigned int > &indices,
std::vector< floatType > &values ){

/*!
* Function which is called which allows the residual to suggest initial values for given
* configurations. This is called when the unknown vector is being initialized. If more than
* one residual attempts to set the initial vector the last residual will override all of the others.
*
* \param &indices: The indices of the unknown vector to set
* \param &values: The values to be set in the unknown vector
*/

indices.clear( );
values.clear( );

}

// Getter functions

//! Get the number of equations the residual defined
Expand Down Expand Up @@ -617,6 +634,9 @@ namespace tardigradeHydra{
//! Get a reference to the number of state variables involved in the non-linear solve
const unsigned int* getNumNonLinearSolveStateVariables( ){ return &_numNonLinearSolveStateVariables; }

//! Get a reference to the number of terms in the unknown vector
virtual const unsigned int getNumUnknowns( ){ return ( *getNumConfigurations( ) ) * ( *getConfigurationUnknownCount( ) ) + *getNumNonLinearSolveStateVariables( ); }

//! Get a reference to the dimension
constexpr unsigned int getDimension( ){ return _dimension; }

Expand Down
2 changes: 1 addition & 1 deletion src/cpp/tardigrade_hydraLinearElasticity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ namespace tardigradeHydra{

const unsigned int num_unknown_config_vars = ( num_configs - 1 ) * sot_dim;

const unsigned int num_unknowns = hydra->getUnknownVector( )->size( );
const unsigned int num_unknowns = hydra->getNumUnknowns( );

// Form the Jacobian
floatVector jacobian = floatVector( sot_dim * num_unknowns, 0 );
Expand Down
2 changes: 1 addition & 1 deletion src/cpp/tardigrade_hydraMassChange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1403,7 +1403,7 @@ namespace tardigradeHydra{

const unsigned int sot_dim = hydra->getSOTDimension( );

const unsigned int num_unknowns = hydra->getUnknownVector( )->size( );
const unsigned int num_unknowns = hydra->getNumUnknowns( );

const unsigned int num_equations = *getNumEquations( );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7907,7 +7907,7 @@ namespace tardigradeHydra{

unsigned int numPlasticStrainLikeISVs = plasticStrainLikeISVs->size( );

const unsigned int numUnknowns = hydra->getUnknownVector( )->size( );
const unsigned int numUnknowns = hydra->getNumUnknowns( );

const unsigned int numISVs = get_plasticStateVariables( )->size( );

Expand Down Expand Up @@ -8358,7 +8358,7 @@ namespace tardigradeHydra{

const unsigned int numEquations = *getNumEquations( );

const unsigned int numUnknowns = hydra->getUnknownVector( )->size( );
const unsigned int numUnknowns = hydra->getNumUnknowns( );

const unsigned int numConfigurations = *hydra->getNumConfigurations( );

Expand Down
2 changes: 1 addition & 1 deletion src/cpp/tardigrade_hydraMicromorphicLinearElasticity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3545,7 +3545,7 @@ namespace tardigradeHydra{

const unsigned int num_configs = *hydra->getNumConfigurations( );

const unsigned int num_unknowns = hydra->getUnknownVector( )->size( );
const unsigned int num_unknowns = hydra->getNumUnknowns( );

floatVector jacobian( *getNumEquations( ) * num_unknowns, 0 );

Expand Down
2 changes: 1 addition & 1 deletion src/cpp/tardigrade_hydraPeryznaViscodamage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ namespace tardigradeHydra{

const unsigned int num_isvs = get_plasticStateVariables( )->size( );

const unsigned int num_unknowns = hydra->getUnknownVector( )->size( );
const unsigned int num_unknowns = hydra->getNumUnknowns( );

floatVector jacobian( *getNumEquations( ) * num_unknowns, 0 );

Expand Down
2 changes: 1 addition & 1 deletion src/cpp/tardigrade_hydraPeryznaViscoplasticity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2929,7 +2929,7 @@ namespace tardigradeHydra{

const unsigned int num_isvs = get_plasticStateVariables( )->size( );

const unsigned int num_unknowns = hydra->getUnknownVector( )->size( );
const unsigned int num_unknowns = hydra->getNumUnknowns( );

floatVector jacobian( *getNumEquations( ) * num_unknowns, 0 );

Expand Down
2 changes: 1 addition & 1 deletion src/cpp/tardigrade_hydraThermalExpansion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ namespace tardigradeHydra{

const unsigned int sot_dim = hydra->getSOTDimension( );

const unsigned int num_unknowns = hydra->getUnknownVector( )->size( );
const unsigned int num_unknowns = hydra->getNumUnknowns( );

floatVector jacobian( *getNumEquations( ) * num_unknowns, 0 );

Expand Down
Loading

0 comments on commit 1579a69

Please sign in to comment.