LandscapeDNDC 1.37.0
Loading...
Searching...
No Matches
mbe_legacymodel.h
1/*
2 * SPDX-FileCopyrightText: Karlsruhe Institute of Technology (KIT)
3 * SPDX-License-Identifier: LicenseRef-LandscapeDNDC
4 * This file is part of LandscapeDNDC. See LICENSE.
5 */
15
16#ifndef MOBILE_MODULE_H_
17#define MOBILE_MODULE_H_
18
19#include "ld_legacy.h"
20#include "legacy/mbe_setup.h"
21#include "legacy/mbe_plant.h"
22#include "legacy/state/mbe_state.h"
23
24#include <math/cbm_math.h>
25#include <time/cbm_time.h>
26#include <input/ecosystemtypes.h>
27#include <memory/cbm_mem.h>
28#include <memory/cbm_singleton.h>
29
30#include <list>
31
32/* oneleaf option has moved */
33
34#ifdef LDNDC_OPENMP
35# define CF_LMOD_QUERY_RETURN_(__rc__) /* no op */
36#else
37# define CF_LMOD_QUERY_RETURN_(__rc__) return __rc__;
38#endif
39#define CF_LMOD_QUERY(__cf__,__key__,__reg__,__dflt__) \
40{ \
41 lerr_t __rc ## __reg__ ( (__cf__)->query( info().id, __key__, &__reg__)); \
42 if (( __rc ## __reg__) == LDNDC_ERR_ATTRIBUTE_NOT_FOUND) \
43 { \
44 __reg__ = __dflt__; \
45 } \
46 else if (( __rc ## __reg__) == LDNDC_ERR_OK) \
47 { \
48 /* no op */ \
49 } \
50 else \
51 { \
52 /* fallback .. use default, and issue error message */ \
53 __reg__ = __dflt__; \
54 \
55 LOGERROR( "config query: invalid key or value"); \
56 CF_LMOD_QUERY_RETURN_(TOKENPASTE(__rc,__reg__)) \
57 } \
58}
59
60namespace cbm {
61 class io_kcomm_t; }
62namespace ldndc {
63 class config_file_t; }
64
65namespace ldndc {
66class MBE_LegacyModel;
67
68extern LDNDC_API lflags_t const TMODE_DEFAULTS;
70{
73
76
78 LMOD_FLAG_USER = 1u << 1,
79
88
91
94
95 LMOD_FLAG_CNT_,
96 LMOD_FLAG_CNT = 1 + 1 + cbm::log2_t< LMOD_FLAG_CNT_-1 >::LOG2
97};
98
99struct LDNDC_API module_info_t
100{
101 module_info_t(
102 char const * /*id*/, char const * /*name*/,
103 lflags_t /*timemode*/, lflags_t /*flags*/);
104
105 /* identifier */
106 char const * id;
107 /* human readable name */
108 char const * name;
109 /* valid time modes */
110 lflags_t timemode;
111
112 /* flags to control and specify behavior of model */
113 lflags_t mflags;
114};
115
116struct LDNDC_API MoBiLE_ModuleFactoryBase
117{
118 virtual ~MoBiLE_ModuleFactoryBase() = 0;
119
120 virtual module_info_t const & info() const = 0;
121 virtual mobile_module_options_t const & options_info() const = 0;
122
123 virtual MBE_LegacyModel * construct(
124 MoBiLE_State *, cbm::io_kcomm_t *,
125 cbm::sclock_t const *, timemode_e) const = 0;
126};
127
128template < class _M >
129struct MoBiLE_ModuleFactory : public MoBiLE_ModuleFactoryBase
130{
131 MoBiLE_ModuleFactory() : MoBiLE_ModuleFactoryBase()
132 { }
133
134 module_info_t const & info() const
135 { return _M::m_info(); }
136
137 mobile_module_options_t const & options_info() const
138 { return *_M::m_options_info(); }
139
140 MBE_LegacyModel * construct(
141 MoBiLE_State * _state, cbm::io_kcomm_t * _io,
142 cbm::sclock_t const * _clock, timemode_e _timemode)
143 const
144 { return static_cast< MBE_LegacyModel * >(
145 _M::new_instance( _state, _io, _clock, _timemode)); }
146};
147
148struct LDNDC_API MoBiLE_ModuleFactoryEnvelope
149{
150 bool exists() const
151 { return ( constructor != NULL); }
152
153 module_info_t const & info() const
154 { return constructor->info(); }
155
156 mobile_module_options_t const & options_info() const
157 { return constructor->options_info(); }
158
159 /* pointer to factory for class */
160 MoBiLE_ModuleFactoryBase const * constructor;
161};
162
163extern LDNDC_API MoBiLE_ModuleFactoryEnvelope const module_factories[];
164
165
166
167
168#define __MODULE_FACTORY(__m_type__) module_factory_ ## __m_type__
169#define MODULE_FACTORY(__m_type__) ldndc::__m_type__::__MODULE_FACTORY(__m_type__)
170
171#define __LMOD_EXPORT_MODULE_FACTORY(__m_type__) \
172 static MoBiLE_ModuleFactory< __m_type__ > const __MODULE_FACTORY(__m_type__)/*;*/
173
174
175#define __LMOD_MODULE_FACTORY(__m_type__) \
176 ldndc::MoBiLE_ModuleFactory< ldndc::__m_type__ > const MODULE_FACTORY(__m_type__)/*;*/
177
178
179#define EXPORT_MODULE_INFO(__m_id__,__m_name__) \
180 public: \
181 static module_info_t const & m_info() { return m_info_; } \
182 module_info_t const & info() const { return this->m_info(); } \
183 char const * name() const { return this->name_(); } \
184 char const * id() const { return this->id_(); } \
185 \
186 private: \
187 static char const * name_() { return __m_name__; } \
188 static char const * id_() { return __m_id__; } \
189 \
190 static module_info_t m_info_/*;*/
191
192
193#define MODULE_INFO(__m_type__,__m_timemode__,__m_flags__) \
194 module_info_t __m_type__::m_info_ = \
195 module_info_t( __m_type__::id_(), __m_type__::name_(), \
196 (__m_timemode__), (__m_flags__))/*;*/
197
198
199#define LDNDC_MOBILE_MODULE_OBJECT(__type__) \
200 public: \
201 static __type__ * allocate_instance() \
202 { \
203 return cbm::allocatable_t< __type__ >::allocate_instance(); \
204 } \
205 static __type__ * new_instance( \
206 MoBiLE_State * _state, \
207 cbm::io_kcomm_t * _io, \
208 cbm::sclock_t const *, \
209 timemode_e _timemode) \
210 { \
211 __type__ * this_instance = \
212 cbm::allocatable_t< __type__ >::allocate_instance(); \
213 \
214 if ( this_instance) \
215 { \
216 new ( this_instance) __type__( _state, _io, _timemode); \
217 } \
218 return this_instance; \
219 } \
220 \
221 virtual void delete_instance() \
222 { \
223 cbm::allocatable_t< __type__ >::delete_instance( this); \
224 } \
225 virtual void delete_instance() \
226 const \
227 { \
228 const_cast< __type__ * >( this)->delete_instance(); \
229 } \
230 private:
231
232#define LDNDC_MOBILE_MODULE_OBJECT_DEFN(__type__) LDNDC_OBJECT_DEFN(ldndc::__type__)
233
234#define EXPORT_MODULE_OPTIONS_INFO(__m_type__) \
235 public: \
236 static mobile_module_options_t const * m_options_info()\
237 { return &options_info_for(__m_type__)->options; } \
238 mobile_module_options_t const * options_info() \
239 const { return __m_type__::m_options_info(); }
240
241#define REGISTER_OPTION(__m_type__,__optkey__,__optvalue__)\
242 static ldndc::module_option_register_t< ldndc::__m_type__ > const \
243 __m_type__##_##__optkey__( #__optkey__, __optvalue__)/*;*/
244
245#define LMOD_EXPORT_MODULE_INFO(__m_type__,__m_id__,__m_name__) \
246 LDNDC_MOBILE_MODULE_OBJECT(__m_type__) \
247 public: \
248 __LMOD_EXPORT_MODULE_FACTORY(__m_type__); \
249 private: \
250 EXPORT_MODULE_OPTIONS_INFO(__m_type__); \
251 private: \
252 EXPORT_MODULE_INFO(__m_id__,__m_name__)
253
254
255#define LMOD_MODULE_INFO(__m_type__,__m_timemode__,__m_flags__) \
256 MODULE_INFO(ldndc::__m_type__,__m_timemode__,__m_flags__); \
257 LDNDC_MOBILE_MODULE_OBJECT_DEFN(__m_type__) \
258 /* also instantiate factory */ \
259 __LMOD_MODULE_FACTORY(__m_type__)
260
261
262template < typename _MODULE >
263struct module_options_info_t
264 : public ldndc::singleton_t< module_options_info_t< _MODULE > >
265{
266 mobile_module_options_t options;
267};
268#define options_info_for(__m_type__) \
269 module_options_info_t< __m_type__ >::get_instance()
270
271class LDNDC_API MBE_LegacyModelBase
272 : public cbm::object_t
273{
274public:
275 MBE_LegacyModelBase( lid_t const & _id)
276 : cbm::object_t( _id)
277 { }
278
279 cbm::sclock_t const * lclock() const
280 { return LD_RtCfg.clk; }
281 cbm::sclock_t const & lclock_ref() const
282 { return *this->lclock(); }
283};
284
285
286class LDNDC_API MBE_LegacyModel : public MBE_LegacyModelBase
287{
288protected:
289 explicit MBE_LegacyModel( MoBiLE_State *, timemode_e);
290public:
291 virtual ~MBE_LegacyModel() = 0;
292
293 /* the pure virtual methods in this block
294 * are implemented implicitely by the
295 * macro EXPORT_MODULE_INFO */
296 virtual char const * name() const = 0;
297 virtual char const * id() const = 0;
298 virtual module_info_t const & info() const = 0;
299 virtual mobile_module_options_t const *
300 options_info() const = 0;
301
302 timemode_e timemode() const;
303 /* test for existence of other legacy models in input */
304 bool have_legacymodel( char const * /*model ID*/) const;
305
310 lerr_t merge_options( mobile_module_options_t const &);
319 cbm::string_t const * get_option(
320 mobile_module_options_t::key_type const & /*option key*/) const;
325 cbm::string_t const * get_option( char const * /*option key*/) const;
330 template < typename _VALUE_TYPE >
331 _VALUE_TYPE get_option( mobile_module_options_t::key_type const & _option_key,
332 _VALUE_TYPE const & _default) const
333 {
334#ifdef _DEBUG
335 void const * check_registered_dummy =
336 this->get_option( _option_key);
337 LDNDC_FIX_UNUSED(check_registered_dummy);
338#endif
339 return this->m_options.get< _VALUE_TYPE >( _option_key, _default);
340 }
341
342 lerr_t set_option( char const * /*option key*/,
343 cbm::string_t const * /*option value*/);
344
345 /* get status */
347 MoBiLE_State * state()
348 { return this->m_state; }
349
350 inline bool active() const
351 { return this->m_active == 1; }
352
353 /* tag model as active */
354 void set_active_on()
355 { this->m_active = 1; }
356 /* tag model as inactive */
357 void set_active_off()
358 { this->m_active = 0; }
359
365 virtual lerr_t configure( config_file_t const *) = 0;
366
377 virtual lerr_t initialize() = 0;
378
379 virtual lerr_t activate()
380 { return LDNDC_ERR_OK; }
381
382 virtual lerr_t register_ports( cbm::io_kcomm_t *)
383 { return LDNDC_ERR_OK; }
384 virtual lerr_t unregister_ports( cbm::io_kcomm_t *)
385 { return LDNDC_ERR_OK; }
386
387
388 virtual lerr_t read()
389 { return LDNDC_ERR_OK; }
390
391 virtual lerr_t solve()
392 { return LDNDC_ERR_OK; }
393
394 virtual lerr_t integrate()
395 { return LDNDC_ERR_OK; }
396
397 virtual lerr_t write()
398 { return LDNDC_ERR_OK; }
399
400
409 virtual lerr_t finalize() = 0;
410
415 virtual lerr_t sleep() = 0;
416
421 virtual lerr_t wake() = 0;
422
423#ifdef _HAVE_SERIALIZE
424 virtual int create_checkpoint(
425 cbm::io_kcomm_t *) { return 0; }
426 virtual int restore_checkpoint(
427 cbm::io_kcomm_t *, ldate_t const *) { return 0; }
428#endif
429
430protected:
431 MoBiLE_State * m_state;
432
433private:
434 /* run model at this time interval */
435 timemode_e m_timemode;
436
437 /* model options */
438 mobile_module_options_t m_options;
439
440 /* model is skipped if tagged inactive (active_ == 0) */
441 mutable int m_active;
442
443 MBE_LegacyModel( MBE_LegacyModel const &);
444 MBE_LegacyModel & operator=( MBE_LegacyModel const &);
445
446private:
447 friend class LK_LegacyModel;
448 /* legacy model info */
449 mobile_modules_info_t const * __m_moduleinfos;
450 cbm::io_kcomm_t * __m_iokcomm;
451};
452
453/*
454 * supported options shall be registered before
455 * referencing them
456 */
457template < typename _MODULE >
458struct module_option_register_t
459{
460 module_option_register_t(
461 char const * _key, char const * _description)
462 {
463 mobile_module_options_t::key_type option_key =
464 mobile_module_options_t::key_type( registered_key_prefix) + _key;
465 mobile_module_options_t::value_type option_description =
466 _description ? _description : "";
467 module_options_info_t< _MODULE > * m_options =
468 module_options_info_t< _MODULE >::get_instance();
469 m_options->options.insert( option_key, option_description);
470 }
471};
472
473struct LDNDC_API MoBiLE_ModuleEnvelope
474{
475 MoBiLE_ModuleEnvelope( MBE_LegacyModel *, char const * /*module id*/);
476 MoBiLE_ModuleEnvelope( MoBiLE_ModuleEnvelope const &);
477
478 MBE_LegacyModel * module;
479 cbm::string_t const id;
480
481 MoBiLE_State * mbe_state;
482 timemode_e mbe_tmmode;
483 mobile_modules_info_t const * mbe_info;
484 cbm::io_kcomm_t * iokcomm;
485
486 MoBiLE_ModuleFactoryBase const * factory;
487
488 private:
489 MoBiLE_ModuleEnvelope & operator=(
490 MoBiLE_ModuleEnvelope const &);
491};
492
493typedef std::list< MoBiLE_ModuleEnvelope > module_container_t;
494
495} /* namespace ldndc */
496
497#endif /* !MOBILE_MODULE_H_ */
498
Spatially explicit groundwater model.
Definition airchemistryput.h:21
lmodule_flag_e
Definition mbe_legacymodel.h:70
@ LMOD_FLAG_FINALIZER
Definition mbe_legacymodel.h:85
@ LMOD_FLAG_TEST_MODULE
Definition mbe_legacymodel.h:93
@ LMOD_FLAG_MAINTENANCE
Definition mbe_legacymodel.h:87
@ LMOD_FLAG_INITIALIZER
Definition mbe_legacymodel.h:83
@ LMOD_FLAG_USER
Definition mbe_legacymodel.h:78
@ LMOD_FLAG_NONE
Definition mbe_legacymodel.h:72
@ LMOD_FLAG_CONTROL
Definition mbe_legacymodel.h:75
@ LMOD_FLAG_OUTPUT
Definition mbe_legacymodel.h:90
@ LMOD_FLAG_SYSTEM
Definition mbe_legacymodel.h:81
LDNDC_API lflags_t const TMODE_DEFAULTS
Definition mbe_legacymodel.cpp:14