Skip to content

Commit

Permalink
Merge pull request #96 from yoctoyotta1024/rainmassmoms
Browse files Browse the repository at this point in the history
New observer for monitoring mass moments of raindrop size distribution during motion and microphysics
  • Loading branch information
yoctoyotta1024 authored Jul 19, 2024
2 parents e4c121e + 7f587cb commit b6a3453
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 9 deletions.
46 changes: 45 additions & 1 deletion libs/observers/sdmmonitor/monitor_massmoments.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Author: Clara Bayley (CB)
* Additional Contributors:
* -----
* Last Modified: Friday 21st June 2024
* Last Modified: Friday 19th July 2024
* Modified By: CB
* -----
* License: BSD 3-Clause "New" or "Revised" License
Expand Down Expand Up @@ -75,6 +75,50 @@ struct MonitorMassMomentViews {
}
};

struct MonitorRainMassMomentViews {
Buffer<uint64_t>::mirrorviewd_buffer d_mom0; // view on device for monitoring 0th mass moment
Buffer<float>::mirrorviewd_buffer d_mom1; // view on device for monitoring 1st mass moment
Buffer<float>::mirrorviewd_buffer d_mom2; // view on device for monitoring 2nd mass moment

/**
* @brief Parallel loop to fill device views with zero value
*/
void reset_views() const {
Kokkos::parallel_for(
"reset_views", Kokkos::RangePolicy(0, d_mom0.extent(0)),
KOKKOS_CLASS_LAMBDA(const size_t jj) {
d_mom0(jj) = 0;
d_mom1(jj) = 0.0;
d_mom2(jj) = 0.0;
});
}

/**
* @brief Write the 0th, 1st and 2nd moments of the raindroplet mass distribution to data views.
*
* Calculates the current mass moments of the raindrop distribution and overwrites the current
* values for the raindrop mass moments (d_mom0, d_mom1 and d_mom2) stored since the data
* views were last reset.
*
* _Note:_ possible conversion of raindrop mass moments at one timestep from double precision
* (8 bytes double) to single precision (4 bytes float) in output.
*
* @param team_member Kokkkos team member in TeamPolicy parallel loop over gridboxes
* @param supers (sub)View of all the superdrops in one gridbox
*/
KOKKOS_FUNCTION
void fetch_massmoments(const TeamMember& team_member, const viewd_constsupers supers) const {
calculate_rainmassmoments(team_member, supers, d_mom0, d_mom1, d_mom2);
}

explicit MonitorRainMassMomentViews(const size_t ngbxs)
: d_mom0("d_monitor_mom0_raindrops", ngbxs),
d_mom1("d_monitor_mom1_raindrops", ngbxs),
d_mom2("d_monitor_mom2_raindrops", ngbxs) {
reset_views();
}
};

/* struct satisfies SDMMonitor concept in order to make observer for monitoring mass moments
* according to the templated MonitorViewsType e.g. 0th, 1st adn 2nd mass moments of the droplet or
raindroplet distributions after microphysics or motion */
Expand Down
60 changes: 54 additions & 6 deletions libs/observers/sdmmonitor/monitor_massmoments_observer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Author: Clara Bayley (CB)
* Additional Contributors:
* -----
* Last Modified: Friday 21st June 2024
* Last Modified: Friday 19th July 2024
* Modified By: CB
* -----
* License: BSD 3-Clause "New" or "Revised" License
Expand Down Expand Up @@ -56,17 +56,42 @@ struct MonitorMassMomentXarrays {
mom2_motion(create_massmom2_xarray(dataset, "massmom2_motion", maxchunk, ngbxs)) {}
};

template <typename Store>
struct MonitorRainMassMomentXarrays {
XarrayZarrArray<Store, uint64_t> mom0_microphys; /**< 0th mass moment from microphysics Xarray */
XarrayZarrArray<Store, float> mom1_microphys; /**< 1st mass moment from microphysics Xarray */
XarrayZarrArray<Store, float> mom2_microphys; /**< 2nd mass moment from microphysics Xarray */
XarrayZarrArray<Store, uint64_t> mom0_motion; /**< 0th mass moment from motion Xarray */
XarrayZarrArray<Store, float> mom1_motion; /**< 1st mass moment from motion Xarray */
XarrayZarrArray<Store, float> mom2_motion; /**< 2nd mass moment from motion Xarray */

MonitorRainMassMomentXarrays(const Dataset<Store> &dataset, const size_t maxchunk,
const size_t ngbxs)
: mom0_microphys(
create_massmom0_xarray(dataset, "massmom0_raindrops_microphys", maxchunk, ngbxs)),
mom1_microphys(
create_massmom1_xarray(dataset, "massmom1_raindrops_microphys", maxchunk, ngbxs)),
mom2_microphys(
create_massmom2_xarray(dataset, "massmom2_raindrops_microphys", maxchunk, ngbxs)),
mom0_motion(create_massmom0_xarray(dataset, "massmom0_raindrops_motion", maxchunk, ngbxs)),
mom1_motion(create_massmom1_xarray(dataset, "massmom1_raindrops_motion", maxchunk, ngbxs)),
mom2_motion(create_massmom2_xarray(dataset, "massmom2_raindrops_motion", maxchunk, ngbxs)) {
}
};

/**
* @class DoMonitorMassMomentsObs
* @brief Class for functionality to observe data from a mass moments monitor of a SDM
* process at the start of each timestep and write it to a Zarr array in an Xarray dataset.
* @tparam Store Type of store for dataset.
* @tparam MonitorXarraysType Type for xarrays for mass moments in dataset.
* @tparam MonitorViewsType Type for views which calculate mass moments for xarrays.
*/
template <typename Store, typename MonitorViewsType>
template <typename Store, typename MonitorXarraysType, typename MonitorViewsType>
class DoMonitorMassMomentsObs {
private:
Dataset<Store> &dataset; /**< Dataset to write time data to. */
std::shared_ptr<MonitorMassMomentXarrays<Store>> xzarrs_ptr; /**< Pointer to arrays in dataset. */
Dataset<Store> &dataset; /**< Dataset to write time data to. */
std::shared_ptr<MonitorXarraysType<Store>> xzarrs_ptr; /**< Pointer to arrays in dataset. */
MonitorMassMoments<MonitorViewsType> monitor;

/**
Expand Down Expand Up @@ -106,7 +131,7 @@ class DoMonitorMassMomentsObs {
*/
DoMonitorMassMomentsObs(Dataset<Store> &dataset, const size_t maxchunk, const size_t ngbxs)
: dataset(dataset),
xzarrs_ptr(std::make_shared<MonitorMassMomentXarrays<Store>>(dataset, maxchunk, ngbxs)),
xzarrs_ptr(std::make_shared<MonitorXarraysType<Store>>(dataset, maxchunk, ngbxs)),
monitor(ngbxs) {}

/**
Expand Down Expand Up @@ -172,7 +197,30 @@ inline Observer auto MonitorMassMomentsObserver(const unsigned int interval,
Dataset<Store> &dataset, const size_t maxchunk,
const size_t ngbxs) {
const auto do_obs =
DoMonitorMassMomentsObs<Store, MonitorMassMomentViews>(dataset, maxchunk, ngbxs);
DoMonitorMassMomentsObs<Store, MonitorMassMomentXarrays, MonitorMassMomentViews>(
dataset, maxchunk, ngbxs);
return ConstTstepObserver(interval, do_obs);
}

/**
* @brief Constructs an observer which writes data monitoring the mass moments of the raindrop's
* distributions during microphysics and super-droplet motion to arrays with a constant observation
* timestep "interval".
*
* @tparam Store Type of store for dataset.
* @param interval Observation timestep.
* @param dataset Dataset to write time data to.
* @param maxchunk Maximum number of elements in a chunk (1-D vector size).
* @param ngbxs The number of gridboxes.
* @return Constructed type satisfying observer concept.
*/
template <typename Store>
inline Observer auto MonitorRainMassMomentsObserver(const unsigned int interval,
Dataset<Store> &dataset, const size_t maxchunk,
const size_t ngbxs) {
const auto do_obs =
DoMonitorMassMomentsObs<Store, MonitorRainMassMomentXarrays, MonitorRainMassMomentViews>(
dataset, maxchunk, ngbxs);
return ConstTstepObserver(interval, do_obs);
}

Expand Down
6 changes: 4 additions & 2 deletions roughpaper/src/main_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Author: Clara Bayley (CB)
* Additional Contributors:
* -----
* Last Modified: Friday 21st June 2024
* Last Modified: Friday 19th July 2024
* Modified By: CB
* -----
* License: BSD 3-Clause "New" or "Revised" License
Expand Down Expand Up @@ -209,8 +209,10 @@ inline Observer auto create_sdmmonitor_observer(const unsigned int interval,
const size_t ngbxs) {
const Observer auto obs_cond = MonitorCondensationObserver(interval, dataset, maxchunk, ngbxs);
const Observer auto obs_massmoms = MonitorMassMomentsObserver(interval, dataset, maxchunk, ngbxs);
const Observer auto obs_rainmassmoms =
MonitorRainMassMomentsObserver(interval, dataset, maxchunk, ngbxs);

return obs_cond >> obs_massmoms;
return obs_cond >> obs_massmoms >> obs_rainmassmoms;
}

template <typename Store>
Expand Down

0 comments on commit b6a3453

Please sign in to comment.