-
Notifications
You must be signed in to change notification settings - Fork 5
/
time_observer.hpp
147 lines (133 loc) · 4.96 KB
/
time_observer.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright (c) 2024 MPI-M, Clara Bayley
*
*
* ----- CLEO -----
* File: time_observer.hpp
* Project: observers
* Created Date: Wednesday 24th January 2024
* Author: Clara Bayley (CB)
* Additional Contributors:
* -----
* Last Modified: Saturday 25th May 2024
* Modified By: CB
* -----
* License: BSD 3-Clause "New" or "Revised" License
* https://opensource.org/licenses/BSD-3-Clause
* -----
* File Description:
* Observer to output time at the start of each observation timestep as a coordinate of an Xarray
* dataset
*/
#ifndef LIBS_OBSERVERS_TIME_OBSERVER_HPP_
#define LIBS_OBSERVERS_TIME_OBSERVER_HPP_
#include <Kokkos_Core.hpp>
#include <concepts>
#include <functional>
#include <iostream>
#include <memory>
#include <string>
#include <utility>
#include "../kokkosaliases.hpp"
#include "./consttstep_observer.hpp"
#include "./observers.hpp"
#include "gridboxes/gridbox.hpp"
#include "superdrops/sdmmonitor.hpp"
#include "zarr/dataset.hpp"
#include "zarr/xarray_zarr_array.hpp"
/**
* @class DoTimeObs
* @brief Template class for functionality to observe time at the start of each timestep and write
* it to a Zarr array as a coordinate of an Xarray dataset.
* @tparam Store Type of store for dataset.
*/
template <typename Store>
class DoTimeObs {
private:
Dataset<Store> &dataset; /**< Dataset to write time data to. */
std::shared_ptr<XarrayZarrArray<Store, float>>
xzarr_ptr; /**< Pointer to time array in dataset. */
std::function<double(unsigned int)>
step2dimlesstime; /**< Function to convert timesteps to real time [assumed seconds]. */
/**
* @brief Increment size of time dimension in dataset and write out current time of model (assumed
* seconds) to the array in the dataset.
*
* _Note:_ conversion of time from double precision (8 bytes double) to single precision (4 bytes
* float) in output.
*
* @param t_mdl Current model time.
*/
void at_start_step(const unsigned int t_mdl) const {
const auto ntimes = size_t{dataset.get_dimension("time") + 1};
const auto timedim = std::pair<std::string, size_t>({"time", ntimes});
dataset.set_dimension(timedim);
const auto time = static_cast<float>(step2dimlesstime(t_mdl));
dataset.write_to_array(xzarr_ptr, time);
}
public:
/**
* @brief Constructor for DoTimeObs.
* @param dataset Dataset to write time data to.
* @param maxchunk Maximum number of elements in a chunk (1-D vector size).
* @param step2dimlesstime Function to convert model timesteps to a real time [assumed seconds].
*/
DoTimeObs(Dataset<Store> &dataset, const size_t maxchunk,
const std::function<double(unsigned int)> step2dimlesstime)
: dataset(dataset),
xzarr_ptr(std::make_shared<XarrayZarrArray<Store, float>>(
dataset.template create_coordinate_array<float>("time", "s", dlc::TIME0, maxchunk, 0))),
step2dimlesstime(step2dimlesstime) {}
/**
* @brief Destructor for DoTimeObs.
*/
~DoTimeObs() { dataset.write_arrayshape(xzarr_ptr); }
/**
* @brief Placeholder for before timestepping functionality and to make class satisfy observer
* concept.
*/
void before_timestepping(const viewd_constgbx d_gbxs) const {
std::cout << "observer includes time observer\n";
}
/**
* @brief Placeholder for after timestepping functionality and to make class satisfy observer
* concept.
*/
void after_timestepping() const {}
/**
* @brief Adapter to call at start step function which writes the current time of the model
* (assumed seconds) to the array in the dataset.
*
* @param t_mdl Current model timestep.
* @param d_gbxs View of gridboxes on device.
* @param totsupers View of superdrops on device.
*/
void at_start_step(const unsigned int t_mdl, const viewd_constgbx d_gbxs,
const viewd_constsupers totsupers) const {
at_start_step(t_mdl);
}
/**
* @brief Get null monitor for SDM processes from observer.
*
* @return monitor 'mo' of the observer that does nothing
*/
SDMMonitor auto get_sdmmonitor() const { return NullSDMMonitor{}; }
};
/**
* @brief Constructs an observer which writes (real) time at start of each observation timestep to a
* 1-D array 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 step2dimlesstime Function to convert model timesteps to real time (assumed seconds).
* @return Constructed type satisfying observer concept.
*/
template <typename Store>
inline Observer auto TimeObserver(const unsigned int interval, Dataset<Store> &dataset,
const size_t maxchunk,
const std::function<double(unsigned int)> step2dimlesstime) {
return ConstTstepObserver(interval, DoTimeObs(dataset, maxchunk, step2dimlesstime));
}
#endif // LIBS_OBSERVERS_TIME_OBSERVER_HPP_