LandscapeDNDC 1.37.0
Loading...
Searching...
No Matches
synth-base-streamdata.h
1
8
9#ifndef LDNDC_DATASYNTH_COMMON_STREAMDATA_H_
10#define LDNDC_DATASYNTH_COMMON_STREAMDATA_H_
11
12#include "synthesizers/synth.h"
13#include "time/cbm_date.h"
14
15#define SYNTH_REC_SIZE_(__ic__) ((size_t)__ic__::record::RECORD_SIZE)
16
17#define SYNTH_REC_GET_(__ic__,__reg__,__item__) (__reg__)[__ic__::record::RECORD_ITEM_ ## __item__]
18
19#define SYNTH_REC_DECL_GET_(__ic__,__reg__,__t__,__item__) \
20 element_type const __reg__##_##__t__( \
21 SYNTH_REC_GET_(__ic__,_scratch->rec_##__t__,__item__))/*;*/
22
23#define SYNTH_REC_DECL_GET_AND_CHK_VALID_(__ic__,__reg__,__t__,__item__) \
24 SYNTH_REC_DECL_GET_(__ic__,__reg__,__t__,__item__); \
25 if ( cbm::is_invalid( __reg__##_##__t__)) \
26 { \
27 CBM_LogError( "dependency was invalid:", #__item__); \
28 return LDNDC_ERR_FAIL; \
29 }
30
31#define SYNTH_INVALIDATE_BUFFER_(__ic__,__buf__) \
32 cbm::mem_set( \
33 __buf__, static_cast< size_t >( \
34 __ic__::record::RECORD_SIZE), \
35 ldndc::invalid_t< __ic__::record::item_type >::value)/*;*/
36
37#ifdef _DEBUG
38# define SYNTH_CHK_MATCHING_(__ic__,__item__) \
39 if ( __ic__::record::RECORD_ITEM_ ## __item__ != this->record_item_type()) \
40 { \
41 CBM_LogFatal( "types not matching ..", \
42 __ic__::RECORD_ITEM_NAMES[__ic__::record::RECORD_ITEM_##__item__],\
43 "vs", \
44 __ic__::RECORD_ITEM_NAMES[this->record_item_type()]); \
45 }
46#else
47# define SYNTH_CHK_MATCHING_(__ic__,__item__) /*no op*/
48#endif
49
50#define SYNTH_CALL_LOOP_TEST_AND_SET_(__ic__,__item__) \
51 SYNTH_CHK_MATCHING_(__ic__,__item__) \
52 if ( _scratch->call_mask_test( __ic__::record::RECORD_ITEM_ ## __item__)) \
53 { \
54 CBM_LogError( "detected loop in ",#__ic__," synthesizer calls (", #__item__, ")"); \
55 return LDNDC_ERR_FAIL; \
56 } \
57 _scratch->call_mask_set( __ic__::record::RECORD_ITEM_ ## __item__)/*;*/
58#define SYNTH_RETURN_SUCCESS_(__ic__,__item__) \
59 _scratch->call_mask_unset( __ic__::record::RECORD_ITEM_ ## __item__); \
60 return LDNDC_ERR_OK/*;*/
61
62
63#define SYNTH_RECORD_ITEM_COMMON_DECL_(__ic__,__class__,__item_enum__) \
64 __class__() {} \
65 enum \
66 { \
67 R = record::RECORD_ITEM_## __item_enum__ \
68 }; \
69 record::record_item_e record_item_type() \
70 const \
71 { \
72 return (record::record_item_e)R; \
73 } \
74 \
75 ~__class__(); \
76 \
77 lerr_t synthesize( \
78 element_type * /* subday data item (output) */, \
79 element_type * /* day data item (output) */, \
80 \
81 scratch_t * /* scratch object */, \
82 \
83 ldate_t const * /* current date */, \
84 unsigned int /* subday to synthesize */, \
85 \
86 __ic__##_info_t const * /* meta info */) const;
87
88
89template < typename, unsigned int >
90class lvector_t;
91
92namespace ldndc{ namespace synth
93{
94 template < typename _SD >
95 struct streamdata_scratch_base_t
96 {
97 virtual ~streamdata_scratch_base_t() = 0;
98 /* holds updated data items */
99 /* subday */
100 typename _SD::element_type rec_s[_SD::RECORD_SIZE];
101 /* day */
102 typename _SD::element_type rec_d[_SD::RECORD_SIZE];
103
104 unsigned int src_res, tgt_res;
105 unsigned int t_res_inout;
106 unsigned int subday;
107
108 /* shifted according to latitude */
109 unsigned int julian_day;
110
111 double day_fraction()
112 const
113 {
114 return 1.0 / (double)this->tgt_res;
115 }
116 double t_scale()
117 const
118 {
119 return 24.0 / (double)this->tgt_res/* FIXME t_res_inout ??? */;
120 }
121
122 virtual lerr_t initialize(
123 typename _SD::boundary_data_type const * /* boundary info */,
124 ltime_t const * /* input date */,
125 ltime_t const * /* output date */) = 0;
126
127 virtual lerr_t configure(
128 typename _SD::boundary_data_type const * /* boundary info */,
129 ldate_t const * /* current date */,
130 unsigned int /* current target subday */) = 0;
131
132 /* helper to detect loops in nested synthesizer calls (no distinction between subdaily/daily) */
133 lflags_t call_mask;
134 void call_mask_set(
135 typename _SD::record_item_e _e)
136 {
137 this->call_mask |= (lflags_t)1 << _e;
138 }
139 void call_mask_unset(
140 typename _SD::record_item_e _e)
141 {
142 this->call_mask &= ~((lflags_t)1 << _e);
143 }
144 bool call_mask_test(
145 typename _SD::record_item_e _e) const
146 {
147 return (( this->call_mask & ((lflags_t)1 << _e)) != 0);
148 }
149 };
150
151 template < typename _SD >
152 streamdata_scratch_base_t< _SD >::~streamdata_scratch_base_t()
153 {
154 }
155
156 template < typename _SD, typename _SCRATCH_T >
157 struct streamdata_item_base_t
158 {
159 typedef typename _SD::element_type element_type;
160
161 virtual ~streamdata_item_base_t() = 0;
162
163 virtual typename _SD::record_item_e record_item_type() const = 0;
164
165 virtual lerr_t synthesize(
166 typename _SD::element_type * /* subday data item (output) */,
167 typename _SD::element_type * /* day data item (output) */,
168
169 _SCRATCH_T * /* scratch object */,
170
171 ldate_t const * /* current date */,
172 unsigned int /* subday to synthesize */,
173
174 typename _SD::boundary_data_type const * /* boundary info */) const = 0;
175 };
176
177 template < typename _SD, typename _SCRATCH_T >
178 streamdata_item_base_t< _SD, _SCRATCH_T >::~streamdata_item_base_t()
179 {
180 }
181
182 template < typename _SD >
183 class streamdata_synth_base_t : public lsynth_base_t
184 {
185 public:
186 streamdata_synth_base_t();
187 virtual ~streamdata_synth_base_t() = 0;
188
193 virtual lerr_t configure(
194 ldate_t const * /* target date */,
195 unsigned int = ldndc::invalid_t< unsigned int >::value /* target subday */) = 0;
196
201 virtual lerr_t synthesize_record(
202 typename _SD::element_type const * /* subday data buffer (input) */, typename _SD::element_type * /* subday data buffer (output) */,
203 typename _SD::element_type const * /* day data buffer (input) */, typename _SD::element_type * /* day data buffer (output) */,
204
205 ldate_t const * /* current date */,
206
207 lvector_t< unsigned int, 1 > * = NULL /* modification count */) = 0;
208
216 virtual lerr_t check_record(
217 typename _SD::element_type const * /* subday data buffer (input) */,
218 typename _SD::element_type const * /* day data buffer (input) */) const = 0;
219 };
220
221 template < typename _SD >
222 streamdata_synth_base_t< _SD >::streamdata_synth_base_t()
223 : lsynth_base_t()
224 {
225 }
226
227 template < typename _SD >
228 streamdata_synth_base_t< _SD >::~streamdata_synth_base_t()
229 {
230 }
231
232} /* namespace synth */
233} /* namespace ldndc */
234
235
236#endif /* !LDNDC_DATASYNTH_COMMON_STREAMDATA_H_ */
237
Spatially explicit groundwater model.
Definition airchemistryput.h:15