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