Permalink
Browse files

Merge pull request #1328 from wthrowe/registration_update

Modify registration recommendations
  • Loading branch information...
kidder committed Feb 11, 2019
2 parents bbb9c63 + c0b0241 commit 3a59de603be61a337d78bc51f6c5524f605cc386
@@ -23,9 +23,21 @@ class ConstGlobalCache;
/// \endcond

namespace StepChoosers {
template <size_t Dim, typename Frame, typename StepChooserRegistrars>
class Cfl;

namespace Registrars {
template <size_t Dim, typename Frame>
struct Cfl {
template <typename StepChooserRegistrars>
using f = StepChoosers::Cfl<Dim, Frame, StepChooserRegistrars>;
};
} // namespace Registrars

/// Suggests a step size based on the CFL stability criterion.
template <size_t Dim, typename Frame, typename StepChooserRegistrars>
template <size_t Dim, typename Frame,
typename StepChooserRegistrars =
tmpl::list<Registrars::Cfl<Dim, Frame>>>
class Cfl : public StepChooser<StepChooserRegistrars> {
public:
/// \cond
@@ -75,14 +87,6 @@ class Cfl : public StepChooser<StepChooserRegistrars> {
double safety_factor_ = std::numeric_limits<double>::signaling_NaN();
};

namespace Registrars {
template <size_t Dim, typename Frame>
struct Cfl {
template <typename StepChooserRegistrars>
using f = StepChoosers::Cfl<Dim, Frame, StepChooserRegistrars>;
};
} // namespace Registrars

/// \cond
template <size_t Dim, typename Frame, typename StepChooserRegistrars>
PUP::able::PUP_ID Cfl<Dim, Frame, StepChooserRegistrars>::my_PUP_ID =
@@ -21,9 +21,15 @@ class ConstGlobalCache;
/// \endcond

namespace StepChoosers {
template <typename StepChooserRegistrars>
class Constant;

namespace Registrars {
using Constant = Registration::Registrar<StepChoosers::Constant>;
} // namespace Registrars

/// Suggests a constant step size.
template <typename StepChooserRegistrars = tmpl::list<>>
template <typename StepChooserRegistrars = tmpl::list<Registrars::Constant>>
class Constant : public StepChooser<StepChooserRegistrars> {
public:
/// \cond
@@ -54,10 +60,6 @@ class Constant : public StepChooser<StepChooserRegistrars> {
double value_ = std::numeric_limits<double>::signaling_NaN();
};

namespace Registrars {
using Constant = Registration::Registrar<StepChoosers::Constant>;
} // namespace Registrars

/// \cond
template <typename StepChooserRegistrars>
PUP::able::PUP_ID Constant<StepChooserRegistrars>::my_PUP_ID = 0; // NOLINT
@@ -25,9 +25,15 @@ struct TimeStep;
/// \endcond

namespace StepChoosers {
template <typename StepChooserRegistrars>
class Increase;

namespace Registrars {
using Increase = Registration::Registrar<StepChoosers::Increase>;
} // namespace Registrars

/// Suggests increasing the step size by a constant ratio.
template <typename StepChooserRegistrars = tmpl::list<>>
template <typename StepChooserRegistrars = tmpl::list<Registrars::Increase>>
class Increase : public StepChooser<StepChooserRegistrars> {
public:
/// \cond
@@ -64,10 +70,6 @@ class Increase : public StepChooser<StepChooserRegistrars> {
double factor_ = std::numeric_limits<double>::signaling_NaN();
};

namespace Registrars {
using Increase = Registration::Registrar<StepChoosers::Increase>;
} // namespace Registrars

/// \cond
template <typename StepChooserRegistrars>
PUP::able::PUP_ID Increase<StepChooserRegistrars>::my_PUP_ID = 0; // NOLINT
@@ -20,10 +20,17 @@ struct TimeId;
/// \endcond

namespace Triggers {
template <typename TriggerRegistrars>
class EveryNSlabs;

namespace Registrars {
using EveryNSlabs = Registration::Registrar<Triggers::EveryNSlabs>;
} // namespace Registrars

/// \ingroup EventsAndTriggersGroup
/// \ingroup TimeGroup
/// Trigger every N time slabs after a given offset.
template <typename TriggerRegistrars = tmpl::list<>>
template <typename TriggerRegistrars = tmpl::list<Registrars::EveryNSlabs>>
class EveryNSlabs : public Trigger<TriggerRegistrars> {
public:
/// \cond
@@ -69,10 +76,6 @@ class EveryNSlabs : public Trigger<TriggerRegistrars> {
uint64_t offset_{0};
};

namespace Registrars {
using EveryNSlabs = Registration::Registrar<Triggers::EveryNSlabs>;
} // namespace Registrars

/// \cond
template <typename TriggerRegistrars>
PUP::able::PUP_ID EveryNSlabs<TriggerRegistrars>::my_PUP_ID = 0; // NOLINT
@@ -22,12 +22,19 @@ struct TimeValue;
/// \endcond

namespace Triggers {
template <typename TriggerRegistrars>
class PastTime;

namespace Registrars {
using PastTime = Registration::Registrar<Triggers::PastTime>;
} // namespace Registrars

/// \ingroup EventsAndTriggersGroup
/// \ingroup TimeGroup
/// Trigger when the simulation is past a certain time (after that
/// time if time is running forward, before that time if time is
/// running backward).
template <typename TriggerRegistrars = tmpl::list<>>
template <typename TriggerRegistrars = tmpl::list<Registrars::PastTime>>
class PastTime : public Trigger<TriggerRegistrars> {
public:
/// \cond
@@ -59,10 +66,6 @@ class PastTime : public Trigger<TriggerRegistrars> {
double trigger_time_{std::numeric_limits<double>::signaling_NaN()};
};

namespace Registrars {
using PastTime = Registration::Registrar<Triggers::PastTime>;
} // namespace Registrars

/// \cond
template <typename TriggerRegistrars>
PUP::able::PUP_ID PastTime<TriggerRegistrars>::my_PUP_ID = 0; // NOLINT
@@ -22,10 +22,17 @@ struct TimeId;
/// \endcond

namespace Triggers {
template <typename TriggerRegistrars>
class SpecifiedSlabs;

namespace Registrars {
using SpecifiedSlabs = Registration::Registrar<Triggers::SpecifiedSlabs>;
} // namespace Registrars

/// \ingroup EventsAndTriggersGroup
/// \ingroup TimeGroup
/// Trigger at specified numbers of slabs after the simulation start.
template <typename TriggerRegistrars = tmpl::list<>>
template <typename TriggerRegistrars = tmpl::list<Registrars::SpecifiedSlabs>>
class SpecifiedSlabs : public Trigger<TriggerRegistrars> {
public:
/// \cond
@@ -64,10 +71,6 @@ class SpecifiedSlabs : public Trigger<TriggerRegistrars> {
std::unordered_set<uint64_t> slabs_;
};

namespace Registrars {
using SpecifiedSlabs = Registration::Registrar<Triggers::SpecifiedSlabs>;
} // namespace Registrars

/// \cond
template <typename TriggerRegistrars>
PUP::able::PUP_ID SpecifiedSlabs<TriggerRegistrars>::my_PUP_ID = 0; // NOLINT
@@ -87,7 +87,7 @@ template <typename Result, typename Classes, typename Base, typename Callable,
}
/// \endcond

/// \ingroup Utilities
/// \ingroup UtilitiesGroup
/// \brief Call a functor with the derived type of a base class pointer.
///
/// \details Calls functor with obj cast to type `T*` where T is the
@@ -22,6 +22,13 @@
/// A concrete base class type with a specific set of derived classes
/// is constructed like
/// \snippet Test_Registration.cpp registrar_use
///
/// It is frequently useful to default the registrar list in a derived
/// class to the registrar for that class. This ensures that any
/// methods in the base class using the registrar list (such as those
/// using `DEFINE_FAKE_VIRTUAL()` or `call_with_dynamic_type()`) work
/// as expected on an explicitly constructed derived class with the
/// list omitted.
namespace Registration {
/// A template for defining a registrar.
///
@@ -32,8 +32,9 @@ class TimeStepper;
namespace {
constexpr size_t dim = 1;
using frame = Frame::Grid;
using registrars = tmpl::list<StepChoosers::Registrars::Cfl<dim, frame>>;
using Cfl = StepChoosers::Cfl<dim, frame, registrars>;
using StepChooserType =
StepChooser<tmpl::list<StepChoosers::Registrars::Cfl<dim, frame>>>;
using Cfl = StepChoosers::Cfl<dim, frame>;

struct CharacteristicSpeed : db::SimpleTag {
static std::string name() noexcept { return "CharacteristicSpeed"; }
@@ -68,8 +69,7 @@ double get_suggestion(const size_t stepper_order, const double safety_factor,
const double grid_spacing = get<Tags::MinimumGridSpacing<dim, frame>>(box);

const Cfl cfl{safety_factor};
const std::unique_ptr<StepChooser<registrars>> cfl_base =
std::make_unique<Cfl>(cfl);
const std::unique_ptr<StepChooserType> cfl_base = std::make_unique<Cfl>(cfl);

const double result = cfl(grid_spacing, box, cache);
CHECK(cfl_base->desired_step(box, cache) == result);
@@ -81,15 +81,15 @@ double get_suggestion(const size_t stepper_order, const double safety_factor,
} // namespace

SPECTRE_TEST_CASE("Unit.Time.StepChoosers.Cfl", "[Unit][Time]") {
Parallel::register_derived_classes_with_charm<StepChooser<registrars>>();
Parallel::register_derived_classes_with_charm<StepChooserType>();

CHECK(get_suggestion(1, 1., 1., {0., 2., 3., 5.}) == approx(1.));
CHECK(get_suggestion(2, 1., 1., {0., 2., 3., 5.}) < 1.);
CHECK(get_suggestion(1, 2., 1., {0., 2., 3., 5.}) == approx(2.));
CHECK(get_suggestion(1, 1., 2., {0., 2., 3., 5.}) == approx(0.5));
CHECK(get_suggestion(1, 1., 1., {0., 2., 2.5, 5.}) == approx(0.5));

test_factory_creation<StepChooser<registrars>>(
test_factory_creation<StepChooserType>(
" Cfl:\n"
" SafetyFactor: 5.0");
}
@@ -22,18 +22,19 @@ struct Metavariables {
using const_global_cache_tag_list = tmpl::list<>;
};

using registrars = tmpl::list<StepChoosers::Registrars::Constant>;
using Constant = StepChoosers::Constant<registrars>;
using StepChooserType =
StepChooser<tmpl::list<StepChoosers::Registrars::Constant>>;
using Constant = StepChoosers::Constant<>;
} // namespace

SPECTRE_TEST_CASE("Unit.Time.StepChoosers.Constant", "[Unit][Time]") {
Parallel::register_derived_classes_with_charm<StepChooser<registrars>>();
Parallel::register_derived_classes_with_charm<StepChooserType>();

const Parallel::ConstGlobalCache<Metavariables> cache{{}};
const auto box = db::create<db::AddSimpleTags<>>();

const Constant constant{5.4};
const std::unique_ptr<StepChooser<registrars>> constant_base =
const std::unique_ptr<StepChooserType> constant_base =
std::make_unique<Constant>(constant);

CHECK(constant(cache) == 5.4);
@@ -42,12 +43,12 @@ SPECTRE_TEST_CASE("Unit.Time.StepChoosers.Constant", "[Unit][Time]") {
CHECK(serialize_and_deserialize(constant_base)->desired_step(box, cache) ==
5.4);

test_factory_creation<StepChooser<registrars>>(" Constant: 5.4");
test_factory_creation<StepChooserType>(" Constant: 5.4");
}

// [[OutputRegex, Requested step magnitude should be positive]]
SPECTRE_TEST_CASE("Unit.Time.StepChoosers.Constant.bad_create",
"[Unit][Time]") {
ERROR_TEST();
test_factory_creation<StepChooser<registrars>>(" Constant: -5.4");
test_factory_creation<StepChooserType>(" Constant: -5.4");
}
@@ -28,18 +28,19 @@ struct Metavariables {
} // namespace

SPECTRE_TEST_CASE("Unit.Time.StepChoosers.Increase", "[Unit][Time]") {
using registrars = tmpl::list<StepChoosers::Registrars::Increase>;
using Increase = StepChoosers::Increase<registrars>;
using StepChooserType =
StepChooser<tmpl::list<StepChoosers::Registrars::Increase>>;
using Increase = StepChoosers::Increase<>;

Parallel::register_derived_classes_with_charm<StepChooser<registrars>>();
Parallel::register_derived_classes_with_charm<StepChooserType>();

const Parallel::ConstGlobalCache<Metavariables> cache{{}};
for (const auto& sign : {1, -1}) {
const auto step = sign * Slab(0., 1.).duration() / 4;
const auto box = db::create<db::AddSimpleTags<Tags::TimeStep>>(step);

const Increase increase{5.};
const std::unique_ptr<StepChooser<registrars>> increase_base =
const std::unique_ptr<StepChooserType> increase_base =
std::make_unique<Increase>(increase);

CHECK(increase(step, cache) == 1.25);
@@ -49,7 +50,7 @@ SPECTRE_TEST_CASE("Unit.Time.StepChoosers.Increase", "[Unit][Time]") {
1.25);
}

test_factory_creation<StepChooser<registrars>>(
test_factory_creation<StepChooserType>(
" Increase:\n"
" Factor: 5.0");
}
@@ -26,7 +26,14 @@ class Base {
virtual int func() const noexcept = 0;
};

template <typename Registrars = tmpl::list<>>
template <typename Registrars>
class Derived1;

namespace Registrars {
using Derived1 = Registration::Registrar<::Derived1>;
} // namespace Registrars

template <typename Registrars = tmpl::list<Registrars::Derived1>>
class Derived1 : public Base<Registrars> {
public:
static constexpr OptionString help = "help";
@@ -35,33 +42,28 @@ class Derived1 : public Base<Registrars> {
};
/// [registrar_structure]

/// [registrar]
template <typename SomeArg, typename Registrars>
class Derived2;

namespace Registrars {
using Derived1 = Registration::Registrar<::Derived1>;
template <typename SomeArg>
using Derived2 = Registration::Registrar<::Derived2, SomeArg>;
} // namespace Registrars

/// [registrar]
template <typename SomeArg, typename Registrars = tmpl::list<>>
template <typename SomeArg,
typename Registrars = tmpl::list<Registrars::Derived2<SomeArg>>>
class Derived2 : public Base<Registrars> {
public:
static constexpr OptionString help = "help";
using options = tmpl::list<>;
int func() const noexcept override { return 2; }
};

namespace Registrars {
template <typename SomeArg>
using Derived2 = Registration::Registrar<::Derived2, SomeArg>;
} // namespace Registrars
/// [registrar]

/// [custom_registrar]
template <int N, typename Registrars = tmpl::list<>>
class Derived3 : public Base<Registrars> {
public:
static constexpr OptionString help = "help";
using options = tmpl::list<>;
int func() const noexcept override { return N; }
};
template <int N, typename Registrars>
class Derived3;

namespace Registrars {
template <int N>
@@ -70,6 +72,14 @@ struct Derived3 {
using f = ::Derived3<N, RegistrarList>;
};
} // namespace Registrars

template <int N, typename Registrars = tmpl::list<Registrars::Derived3<N>>>
class Derived3 : public Base<Registrars> {
public:
static constexpr OptionString help = "help";
using options = tmpl::list<>;
int func() const noexcept override { return N; }
};
/// [custom_registrar]
} // namespace

0 comments on commit 3a59de6

Please sign in to comment.