Skip to content

Commit

Permalink
Fix AD directions in tests
Browse files Browse the repository at this point in the history
For the tests, some units need to initialize the AD directions with
zero. This is not done sometimes when multiple tests are run: The number
of AD directions used might still be set from the previous test, leading
to uninitialized value errors.
Thus, we set the AD directions before configuring the units.

Co-authored-by: Jan Breuer <[email protected]>
  • Loading branch information
ronald-jaepel and jbreue16 committed Oct 22, 2024
1 parent a0a53d0 commit 302b487
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
18 changes: 14 additions & 4 deletions test/ColumnTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,8 @@ namespace column

void testJacobianAD(cadet::JsonParameterProvider& jpp, const double absTolFDpattern)
{
cadet::ad::setDirections(cadet::ad::getMaxDirections()); // AD directions needed in createAndConfigureUnit but requiredADdirs not know before configureModelDiscretization (which is called in configureUnit)

cadet::IModelBuilder* const mb = cadet::createModelBuilder();
REQUIRE(nullptr != mb);

Expand Down Expand Up @@ -889,6 +891,9 @@ namespace column

void testJacobianForwardBackward(cadet::JsonParameterProvider& jpp, const double absTolFDpattern)
{
// Enable AD
cadet::ad::setDirections(cadet::ad::getMaxDirections()); // AD directions needed in createAndConfigureUnit but requiredADdirs not know before configureModelDiscretization (which is called in configureUnit)

cadet::IModelBuilder* const mb = cadet::createModelBuilder();
REQUIRE(nullptr != mb);

Expand All @@ -897,9 +902,6 @@ namespace column

cadet::IUnitOperation* const unitAna = unitoperation::createAndConfigureUnit(jpp, *mb);
cadet::IUnitOperation* const unitAD = unitoperation::createAndConfigureUnit(jpp, *mb);

// Enable AD
cadet::ad::setDirections(cadet::ad::getMaxDirections());
unitAD->useAnalyticJacobian(false);

cadet::active* adRes = new cadet::active[unitAD->numDofs()];
Expand Down Expand Up @@ -1223,10 +1225,10 @@ namespace column
{
if (hasBinding)
cadet::test::setBindingMode(jpp, isKinetic);
cadet::ad::setDirections(cadet::ad::getMaxDirections()); // AD directions already needed in createAndConfigureUnit
cadet::IUnitOperation* const unit = createAndConfigureUnit(*mb, jpp);

// Enable AD
cadet::ad::setDirections(cadet::ad::getMaxDirections());
cadet::active* adRes = new cadet::active[unit->numDofs()];
const AdJacobianParams adParams{adRes, nullptr, 0};
unit->prepareADvectors(adParams);
Expand Down Expand Up @@ -1495,6 +1497,8 @@ namespace column
// Use some test case parameters
cadet::JsonParameterProvider jpp = createColumnWithTwoCompLinearBinding(uoType, spatialMethod);
cadet::test::setBindingMode(jpp, isKinetic);
if (adEnabled)
cadet::ad::setDirections(cadet::ad::getMaxDirections());
cadet::IUnitOperation* const unit = createAndConfigureUnit(*mb, jpp);

// Fill state vector with given initial values
Expand Down Expand Up @@ -1530,6 +1534,8 @@ namespace column
// Use some test case parameters
cadet::JsonParameterProvider jpp = createColumnWithSMA(uoType, spatialMethod);
cadet::test::setBindingMode(jpp, isKinetic);
if (adEnabled)
cadet::ad::setDirections(cadet::ad::getMaxDirections());
cadet::IUnitOperation* const unit = createAndConfigureUnit(*mb, jpp);

// Fill state vector with given initial values
Expand Down Expand Up @@ -1564,6 +1570,8 @@ namespace column
// Use some test case parameters
cadet::JsonParameterProvider jpp = linearBinding ? createColumnWithTwoCompLinearBinding(uoType, spatialMethod) : createColumnWithSMA(uoType, spatialMethod);
cadet::test::setBindingMode(jpp, isKinetic);
if (adEnabled)
cadet::ad::setDirections(cadet::ad::getMaxDirections()); // needed in configure() of DG unit operations
cadet::IUnitOperation* const unit = createAndConfigureUnit(*mb, jpp);

unit->setSensitiveParameter(cadet::makeParamId("INIT_C", 0, 0, cadet::ParTypeIndep, cadet::BoundStateIndep, cadet::ReactionIndep, cadet::SectionIndep), 0, 1.0);
Expand Down Expand Up @@ -1596,6 +1604,8 @@ namespace column
{
// Use some test case parameters
cadet::JsonParameterProvider jpp = createColumnWithSMA(uoType, spatialMethod);
if (adEnabled)
cadet::ad::setDirections(cadet::ad::getMaxDirections());
cadet::IUnitOperation* const unit = createAndConfigureUnit(*mb, jpp);

unitoperation::testInletDofJacobian(unit, adEnabled);
Expand Down
5 changes: 3 additions & 2 deletions test/ModelSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ namespace

TEST_CASE("ModelSystem Jacobian AD vs analytic", "[ModelSystem],[Jacobian],[AD]")
{
cadet::ad::setDirections(cadet::ad::getMaxDirections());
cadet::IModelBuilder* const mb = cadet::createModelBuilder();
REQUIRE(nullptr != mb);

Expand Down Expand Up @@ -625,7 +626,6 @@ TEST_CASE("ModelSystem Jacobian AD vs analytic", "[ModelSystem],[Jacobian],[AD]"
delete[] secContArray;

// Enable AD
cadet::ad::setDirections(cadet::ad::getMaxDirections());
for (unsigned int i = 0; i < sysAD->numModels(); ++i)
sysAD->getUnitOperationModel(i)->useAnalyticJacobian(false);

Expand Down Expand Up @@ -759,6 +759,8 @@ TEST_CASE("ModelSystem sensitivity Jacobians", "[ModelSystem],[Sensitivity]")
const double absTol = 5e-8;
const double relTol = 5e-6; // std::numeric_limits<float>::epsilon() * 100.0;

cadet::ad::setDirections(cadet::ad::getMaxDirections());

cadet::IModelBuilder* const mb = cadet::createModelBuilder();
REQUIRE(nullptr != mb);

Expand Down Expand Up @@ -796,7 +798,6 @@ TEST_CASE("ModelSystem sensitivity Jacobians", "[ModelSystem],[Sensitivity]")
delete[] secContArray;

// Enable AD
cadet::ad::setDirections(cadet::ad::getMaxDirections());
cadet::active* adRes = new cadet::active[sys->numDofs()];
sys->prepareADvectors(cadet::AdJacobianParams{adRes, nullptr, 0});

Expand Down
6 changes: 3 additions & 3 deletions test/UnitOperationTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ namespace unitoperation

void testJacobianAD(cadet::JsonParameterProvider& jpp, const double absTolFDpattern)
{
// Enable AD
cadet::ad::setDirections(cadet::ad::getMaxDirections());

cadet::IModelBuilder* const mb = cadet::createModelBuilder();
REQUIRE(nullptr != mb);

cadet::IUnitOperation* const unitAna = createAndConfigureUnit(jpp, *mb);
cadet::IUnitOperation* const unitAD = createAndConfigureUnit(jpp, *mb);

// Enable AD
cadet::ad::setDirections(cadet::ad::getMaxDirections());
unitAD->useAnalyticJacobian(false);

cadet::active* adRes = new cadet::active[unitAD->numDofs()];
Expand Down

0 comments on commit 302b487

Please sign in to comment.