Skip to content

Commit

Permalink
Add IMEX initialization mutator
Browse files Browse the repository at this point in the history
  • Loading branch information
wthrowe committed Dec 15, 2023
1 parent 2f509ab commit 28531a8
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Evolution/Imex/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ spectre_target_headers(
INCLUDE_DIRECTORY ${CMAKE_SOURCE_DIR}/src
HEADERS
GuessResult.hpp
Initialize.hpp
Mode.hpp
NamespaceDocs.hpp
SolveImplicitSector.hpp
Expand Down
57 changes: 57 additions & 0 deletions src/Evolution/Imex/Initialize.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Distributed under the MIT License.
// See LICENSE.txt for details.

#pragma once

#include "Evolution/Imex/Protocols/ImexSystem.hpp"
#include "Evolution/Imex/Tags/ImplicitHistory.hpp"
#include "Evolution/Imex/Tags/Mode.hpp"
#include "Evolution/Imex/Tags/SolveFailures.hpp"
#include "Evolution/Imex/Tags/SolveTolerance.hpp"
#include "Time/History.hpp"
#include "Time/Tags/HistoryEvolvedVariables.hpp"
#include "Utilities/Gsl.hpp"
#include "Utilities/MakeWithValue.hpp"
#include "Utilities/ProtocolHelpers.hpp"
#include "Utilities/TMPL.hpp"

namespace imex {
/// Create the IMEX structures and options.
template <typename System, typename = typename System::implicit_sectors>
struct Initialize;

/// \cond
template <typename System, typename... Sectors>
struct Initialize<System, tmpl::list<Sectors...>> {
static_assert(tt::assert_conforms_to_v<System, protocols::ImexSystem>);

using example_tensor_tag =
tmpl::front<typename tmpl::front<tmpl::list<Sectors...>>::tensors>;

using const_global_cache_tags = tmpl::list<Tags::Mode, Tags::SolveTolerance>;
using mutable_global_cache_tags = tmpl::list<>;
using simple_tags_from_options = tmpl::list<>;
using simple_tags = tmpl::list<Tags::ImplicitHistory<Sectors>...,
Tags::SolveFailures<Sectors>...>;
using compute_tags = tmpl::list<>;

using return_tags = simple_tags;
using argument_tags =
tmpl::list<::Tags::HistoryEvolvedVariables<>, example_tensor_tag>;

static void apply(
const gsl::not_null<
typename Tags::ImplicitHistory<Sectors>::type*>... histories,
const gsl::not_null<
typename Tags::SolveFailures<Sectors>::type*>... solve_failures,
const TimeSteppers::History<typename System::variables_tag::type>&
explicit_history,
const typename example_tensor_tag::type& example_tensor) {
const auto order = explicit_history.integration_order();
expand_pack((histories->integration_order(order), 0)...);
expand_pack(*solve_failures = make_with_value<Scalar<DataVector>>(
example_tensor, 0.0)...);
}
};
/// \endcond
} // namespace imex
1 change: 1 addition & 0 deletions tests/Unit/Evolution/Imex/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set(LIBRARY "Test_Imex")

set(LIBRARY_SOURCES
Test_GuessResult.cpp
Test_Initialize.cpp
Test_Mode.cpp
Test_SolveImplicitSector.cpp
)
Expand Down
95 changes: 95 additions & 0 deletions tests/Unit/Evolution/Imex/Test_Initialize.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Distributed under the MIT License.
// See LICENSE.txt for details.

#include "Framework/TestingFramework.hpp"

#include "DataStructures/DataBox/DataBox.hpp"
#include "DataStructures/DataBox/Prefixes.hpp"
#include "DataStructures/DataBox/Tag.hpp"
#include "DataStructures/DataVector.hpp"
#include "DataStructures/Tensor/Tensor.hpp"
#include "DataStructures/Tensor/TypeAliases.hpp"
#include "DataStructures/Variables.hpp"
#include "DataStructures/VariablesTag.hpp"
#include "Evolution/Imex/GuessResult.hpp"
#include "Evolution/Imex/Initialize.hpp"
#include "Evolution/Imex/Protocols/ImexSystem.hpp"
#include "Evolution/Imex/Protocols/ImplicitSector.hpp"
#include "Evolution/Imex/Tags/ImplicitHistory.hpp"
#include "Time/Tags/HistoryEvolvedVariables.hpp"
#include "Utilities/Gsl.hpp"
#include "Utilities/ProtocolHelpers.hpp"
#include "Utilities/TMPL.hpp"

namespace {
template <typename Var>
struct Sector : tt::ConformsTo<imex::protocols::ImplicitSector> {
using tensors = tmpl::list<Var>;

struct initial_guess {
using return_tags = tmpl::list<Var>;
using argument_tags = tmpl::list<>;
static imex::GuessResult apply(
gsl::not_null<Scalar<DataVector>*> var,
const Variables<tmpl::list<Var>>& inhomogeneous_terms,
double implicit_weight);
};

struct SolveAttempt {
struct source {
using return_tags = tmpl::list<Tags::Source<Var>>;
using argument_tags = tmpl::list<Var>;
static void apply(gsl::not_null<Scalar<DataVector>*> source);
};

using jacobian = imex::NoJacobianBecauseSolutionIsAnalytic;

using tags_from_evolution = tmpl::list<>;
using simple_tags = tmpl::list<>;
using compute_tags = tmpl::list<>;
using source_prep = tmpl::list<>;
using jacobian_prep = tmpl::list<>;
};
using solve_attempts = tmpl::list<SolveAttempt>;
};

struct Var1 : db::SimpleTag {
using type = Scalar<DataVector>;
};

struct Var2 : db::SimpleTag {
using type = Scalar<DataVector>;
};

struct System : tt::ConformsTo<imex::protocols::ImexSystem> {
using variables_tag = Tags::Variables<tmpl::list<Var1, Var2>>;
using implicit_sectors = tmpl::list<Sector<Var1>, Sector<Var2>>;
};
} // namespace

SPECTRE_TEST_CASE("Unit.Evolution.Imex.Initialize", "[Unit][Evolution]") {
using initialize_imex = imex::Initialize<System>;
using explicit_history_tag =
Tags::HistoryEvolvedVariables<System::variables_tag>;
auto box =
db::create<db::AddSimpleTags<System::variables_tag, explicit_history_tag,
initialize_imex::simple_tags>>();
db::mutate<System::variables_tag, explicit_history_tag>(
[](const gsl::not_null<System::variables_tag::type*> variables,
const gsl::not_null<explicit_history_tag::type*> explicit_history) {
variables->initialize(5);
explicit_history->integration_order(3);
},
make_not_null(&box));

db::mutate_apply<initialize_imex>(make_not_null(&box));

CHECK(db::get<imex::Tags::ImplicitHistory<Sector<Var1>>>(box)
.integration_order() == 3);
CHECK(db::get<imex::Tags::ImplicitHistory<Sector<Var2>>>(box)
.integration_order() == 3);
CHECK(db::get<imex::Tags::SolveFailures<Sector<Var1>>>(box) ==
Scalar<DataVector>(DataVector(5, 0.0)));
CHECK(db::get<imex::Tags::SolveFailures<Sector<Var2>>>(box) ==
Scalar<DataVector>(DataVector(5, 0.0)));
}

0 comments on commit 28531a8

Please sign in to comment.