Skip to content

Commit

Permalink
using preddcorrmotion for general motion with nghbrs
Browse files Browse the repository at this point in the history
  • Loading branch information
yoctoyotta1024 committed Dec 19, 2023
1 parent 09fbc9d commit 4adf108
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 83 deletions.
151 changes: 68 additions & 83 deletions libs/cartesiandomain/cartesianmotion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,88 +35,89 @@
#include "./cartesianboundaryconds.hpp"
#include "superdrops/superdrop.hpp"
#include "superdrops/terminalvelocity.hpp"
#include "gridboxes/predcorr.hpp"
#include "gridboxes/predcorrmotion.hpp"

KOKKOS_FUNCTION unsigned int
change_to_coord3nghbr(const CartesianMaps &gbxmaps,
unsigned int idx,
Superdrop &drop);

KOKKOS_FUNCTION unsigned int
change_to_coord1nghbr(const CartesianMaps &gbxmaps,
unsigned int idx,
Superdrop &drop);
KOKKOS_FUNCTION unsigned int
change_to_coord2nghbr(const CartesianMaps &gbxmaps,
unsigned int idx,
Superdrop &drop);

KOKKOS_FUNCTION void
check_inbounds_or_outdomain(const unsigned int idx,
const Kokkos::pair<double, double> bounds,
const double coord);

template <VelocityFormula TV>
struct CartesianMotion
/* satisfies motion concept for motion of a superdroplet
using a predictor-corrector method to update a superdroplet's
coordinates and then updating it's sdgbxindex using the
UpdateSdgbxindex struct for a cartesian domain */
struct CartesianCheckBounds
/* wrapper of operator for use of function
in PredCorrMotion's CheckBounds type */
{
const unsigned int interval; // integer timestep for movement
PredCorr<CartesianMaps, TV> superdrop_coords;

CartesianMotion(const unsigned int motionstep,
const std::function<double(unsigned int)> int2time,
const TV i_terminalv)
: interval(motionstep),
superdrop_coords(interval, int2time, i_terminalv) {}
KOKKOS_FUNCTION void
operator()(const unsigned int idx,
const Kokkos::pair<double, double> bounds,
const double coord);
/* raise error if superdrop not either out of domain
or within bounds (ie. lower_bound <= coord < upper_bound) */
};

KOKKOS_INLINE_FUNCTION
unsigned int next_step(const unsigned int t_sdm) const
struct CartesianChangeIfNghbr
/* wrapper of functions for use in PredCorrMotion's
ChangeToNghbr type for deciding if a superdroplet should move
to a neighbouring gbx in a cartesian domain and then updating the
superdroplet appropriately. Struct has three functions, one
for each direction (coord3 = z, coord1 = x, coord2 = y). For each,
the superdrop's coord is compared to gridbox bounds given by gbxmaps
for the current gbxindex 'idx'. If superdrop coord lies outside
bounds, forward or backward neighbour functions are called to
update sdgbxindex (and possibly other superdrop attributes) */
{
KOKKOS_FUNCTION unsigned int
coord3(const CartesianMaps &gbxmaps,
unsigned int idx,
Superdrop &drop)
{
return ((t_sdm / interval) + 1) * interval;
change_to_coord3nghbr(gbxmaps, idx, drop);
}

KOKKOS_INLINE_FUNCTION
bool on_step(const unsigned int t_sdm) const
KOKKOS_FUNCTION unsigned int
coord1(const CartesianMaps &gbxmaps,
unsigned int idx,
Superdrop &drop)
{
return t_sdm % interval == 0;
change_to_coord1nghbr(gbxmaps, idx, drop);
}

KOKKOS_INLINE_FUNCTION void
superdrop_gbx(const unsigned int gbxindex,
const CartesianMaps &gbxmaps,
Superdrop &drop) const
/* function satisfies requirements of
"superdrop_gbx" in the motion concept to update a
superdroplet if it should move between gridboxes in a
cartesian domain. For each direction (z, then x, then y),
superdrop coord is compared to gridbox bounds given by gbxmaps
for the current gbxindex 'idx'. If superdrop coord lies outside
bounds, forward or backward neighbour functions are called as
appropriate to update sdgbxindex (and possibly other attributes) */
KOKKOS_FUNCTION unsigned int
coord2(const CartesianMaps &gbxmaps,
unsigned int idx,
Superdrop &drop)
{
unsigned int idx(gbxindex);

idx = change_to_coord3nghbr(gbxmaps, idx, drop);
check_inbounds_or_outdomain(idx, gbxmaps.coord3bounds(idx),
drop.get_coord3());

idx = change_to_coord1nghbr(gbxmaps, idx, drop);
check_inbounds_or_outdomain(idx, gbxmaps.coord1bounds(idx),
drop.get_coord1());

idx = change_to_coord2nghbr(gbxmaps, idx, drop);
check_inbounds_or_outdomain(idx, gbxmaps.coord2bounds(idx),
drop.get_coord2());

drop.set_sdgbxindex(idx);
change_to_coord2nghbr(gbxmaps, idx, drop);
}
};

template <VelocityFormula TV>
inline Motion<CartesianMaps> auto
CartesianMotion(const unsigned int motionstep,
const std::function<double(unsigned int)> int2time,
const TV terminalv)
/* returned type satisfies motion concept for motion of a
superdroplet using a predictor-corrector method to update
a superdroplet's coordinates and then updating it's
sdgbxindex as appropriate for a cartesian domain */
{
return PredCorrMotion(motionstep, int2time, terminalv,
CartesianChangeIfNghbr{},
CartesianCheckBounds{});
}

/* ----- ----- TODO: move functions below to .cpp file ----- ----- */

KOKKOS_FUNCTION void
CartesianCheckBounds::operator()(const unsigned int idx,
const Kokkos::pair<double, double> bounds,
const double coord);
/* raise error if superdrop not either out of domain
or within bounds (ie. lower_bound <= coord < upper_bound) */
{
const bool bad_gbxindex((idx != outofbounds_gbxindex()) &&
((coord < bounds.first) | (coord >= bounds.second)));

assert((!bad_gbxindex) && "SD not in previous gbx nor a neighbour."
" Try reducing the motion timestep to"
" satisfy CFL criteria, or use "
" 'update_ifoutside' to update sd_gbxindex");
}

KOKKOS_FUNCTION int
flag_sdgbxindex(const unsigned int idx,
const Kokkos::pair<double, double> bounds,
Expand Down Expand Up @@ -182,22 +183,6 @@ beyonddomain_forwards_coord2(const CartesianMaps &gbxmaps,
const unsigned int nghbr,
Superdrop &drop);

KOKKOS_FUNCTION void
check_inbounds_or_outdomain(const unsigned int idx,
const Kokkos::pair<double, double> bounds,
const double coord)
/* raise error if superdrop not either out of domain
or within bounds (ie. lower_bound <= coord < upper_bound) */
{
const bool bad_gbxindex((idx != outofbounds_gbxindex()) &&
((coord < bounds.first) | (coord >= bounds.second)));

assert((!bad_gbxindex) && "SD not in previous gbx nor a neighbour."
" Try reducing the motion timestep to"
" satisfy CFL criteria, or use "
" 'update_ifoutside' to update sd_gbxindex");
}

int flag_sdgbxindex(const unsigned int idx,
const Kokkos::pair<double, double> bounds,
const double coord)
Expand Down
64 changes: 64 additions & 0 deletions libs/gridboxes/predcorrmotion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,68 @@
#include "superdrops/superdrop.hpp"
#include "superdrops/terminalvelocity.hpp"

template <GridboxMaps GbxMaps, VelocityFormula TV,
typename ChangeToNghbr, typename CheckBounds>
struct PredCorrMotion
/* satisfies motion concept for motion of a superdroplet
using a predictor-corrector method to update a superdroplet's
coordinates and then updating it's sdgbxindex using
the appropriate templated type */
{
const unsigned int interval; // integer timestep for movement
PredCorr<GbxMaps, TV> superdrop_coords;
ChangeToNghbr change_if_nghbr;
CheckBounds check_bounds;

PredCorrMotion(const unsigned int motionstep,
const std::function<double(unsigned int)> int2time,
const TV i_terminalv,
ChangeToNghbr i_change_if_nghbr,
CheckBounds i_check_bounds)
: interval(motionstep),
superdrop_coords(interval, int2time, i_terminalv),
change_if_nghbr(i_change_if_nghbr),
check_bounds(i_check_bounds) {}

KOKKOS_INLINE_FUNCTION
unsigned int next_step(const unsigned int t_sdm) const
{
return ((t_sdm / interval) + 1) * interval;
}

KOKKOS_INLINE_FUNCTION
bool on_step(const unsigned int t_sdm) const
{
return t_sdm % interval == 0;
}

KOKKOS_INLINE_FUNCTION void
superdrop_gbx(const unsigned int gbxindex,
const CartesianMaps &gbxmaps,
Superdrop &drop) const
/* function satisfies requirements of
"superdrop_gbx" in the motion concept to update a
superdroplet if it should move between gridboxes.
For each direction (coord3, then coord1, then coord2),
superdrop and idx may be changed if superdrop coord
lies outside bounds of gridbox in that direction */
{
unsigned int idx(gbxindex);

idx = change_if_nghbr.coord3(gbxmaps, idx, drop);
check_bounds(idx, gbxmaps.coord3bounds(idx),
drop.get_coord3());

idx = change_if_nghbr.coord1(gbxmaps, idx, drop);
check_bounds(idx, gbxmaps.coord1bounds(idx),
drop.get_coord1());

idx = change_if_nghbr.coord2(gbxmaps, idx, drop);
check_bounds(idx, gbxmaps.coord2bounds(idx),
drop.get_coord2());

drop.set_sdgbxindex(idx);
}
};

#endif // PREDCORRMOTION_HPP

0 comments on commit 4adf108

Please sign in to comment.