00001
00002
00003
00004
00005
00006
00007
00008
00009
00016 #ifndef __DSIM_MODEL__
00017 #define __DSIM_MODEL__
00018
00019 #include "dsim_variable.h"
00020 #include "dsim_value.h"
00021 #include "dsim_network.h"
00022 #include <vector>
00023 #include <map>
00024 #include <string>
00025
00026 class dsim_integrator;
00027 class dsim_simulation;
00028 class dsim_model_setup;
00029
00030 class dsim_outlet
00031 {
00032 public:
00033 dsim_outlet() :
00034 name(),
00035 units(),
00036 description(),
00037 type(sd_type_invalid),
00038 dependent(false),
00039 variable(NULL)
00040 {
00041 }
00042 dsim_outlet(const char *oname,int otype,const char *ounits,const char *odescription,dsim_variable *ovariable,bool odependent) :
00043 name(oname),
00044 units(ounits),
00045 description(odescription),
00046 type(otype),
00047 dependent(odependent),
00048 variable(ovariable)
00049 {
00050 }
00051 dsim_outlet(const dsim_outlet &base) :
00052 name(base.name),
00053 units(base.units),
00054 description(base.description),
00055 type(base.type),
00056 dependent(base.dependent),
00057 variable(base.variable)
00058 {
00059 }
00060 dsim_outlet &operator=(const dsim_outlet &base)
00061 {
00062 name = base.name;
00063 units = base.units;
00064 description = base.description;
00065 type = base.type;
00066 variable = base.variable;
00067 dependent = base.dependent;
00068 return *this;
00069 }
00070
00071 std::string name;
00072 std::string units;
00073 std::string description;
00074 int type;
00075 bool dependent;
00076 dsim_variable *variable;
00077 };
00078
00079 class dsim_target
00080 {
00081 public:
00082 dsim_target() :
00083 multiple(false)
00084 {
00085 }
00086 dsim_target(const char *tname,const char *tdescription,bool tmultiple)
00087 {
00088 name = tname;
00089 description = tdescription;
00090 multiple = tmultiple;
00091 }
00092 dsim_target(const dsim_target &base)
00093 {
00094 name = base.name;
00095 description = base.description;
00096 target_path = base.target_path;
00097 multiple = base.multiple;
00098 target_set = base.target_set;
00099 }
00100 dsim_target &operator=(const dsim_target &base)
00101 {
00102 name = base.name;
00103 description = base.description;
00104 target_path = base.target_path;
00105 multiple = base.multiple;
00106 target_set = base.target_set;
00107 return *this;
00108 }
00109
00110 std::string name;
00111 std::string description;
00112 std::string target_path;
00113 std::vector<std::string> target_set;
00114 bool multiple;
00115 };
00116
00130 class dsim_model
00131 {
00132 public:
00134 dsim_model(dsim_model_setup *setup);
00136 virtual ~dsim_model();
00137
00138
00147
00148 const char *name() const;
00150 const char *path() const;
00152 dsim_model *parent() const;
00154 unsigned int child_count() const;
00156 dsim_model *child_at_index(unsigned int index) const;
00158 dsim_model *child_with_name(const char *name) const;
00160
00161
00174
00175 virtual void *describe_setup(void *ds);
00177 void describe_setup_command(void *ds,const char *command,const char *description);
00179 virtual bool parse_setup(const char *command,const char *data);
00181 virtual void initialize_data();
00183 virtual void initialization_complete();
00185
00186
00203
00204 virtual void initialize_timestep();
00206 virtual void pre_calculate();
00208 virtual void post_calculate();
00210 virtual void complete_timestep();
00212 virtual void rhs(double t,double jd);
00214
00215
00224
00225 dsim_variable request_local_variable(const char *name,bool quiet = false);
00227 dsim_value current_value_for_variable(const char *name);
00229 bool connect_outlet(const char *name,const dsim_variable &source,const char *source_object = NULL);
00230
00231
00232
00242
00243 void set_attribute(const std::string &ame,const std::string &value);
00245 bool has_attribute(const std::string &name);
00247 std::string get_attribute(const std::string &name);
00249 std::map<std::string,std::string>::iterator attribute_iterator();
00251 std::map<std::string,std::string>::iterator end_attribute_iterator();
00253
00254
00268
00269 virtual dsim_value handle_message(const std::string &sender_path,const std::string &message_name,const dsim_value &argument);
00271 virtual std::vector<dsim_value> handle_messages(const std::string &sender_path,const std::string &message_name,const std::vector<dsim_value> &arguments);
00273
00275 inline bool should_pre_calculate() { return _call_pre_calculate; }
00277 inline bool should_integrate() { return _call_rhs; }
00279 inline bool should_post_calculate() { return _call_post_calculate; }
00280 protected:
00281
00286 void configure_timestep(bool call_pre,bool call_rhs,bool call_post);
00287
00288
00301
00302 dsim_variable create_parameter(const char *name,int data_type,void *init_value,int rows,int cols,const char *units,const char *description);
00304 dsim_variable create_input(const char *name,int data_type,void *init_value,int rows,int cols,const char *units,const char *description);
00306 dsim_variable create_output(const char *name,int data_type,void *init_value,int rows,int cols,const char *units,const char *description);
00308 dsim_variable create_state(const char *name,int data_type,void *init_value,int rows,int cols,const char *units,const char *description);
00310 dsim_variable create_integrated_state(const char *name,int data_type,void *init_value,int rows,int cols,const char *units,const char *description);
00311
00313 void create_outlet(dsim_variable &outlet,const char *name,int data_type,const char *units,const char *description,bool dependent = false);
00315 dsim_variable request_variable(const char *path,bool quiet = false,bool store = true);
00317 dsim_variable request_hierarchy_variable(const char *path,bool quiet = false);
00319
00320
00325
00326 void create_target(const char *toarget_name,const char *target_description,bool multiple = false);
00328 const char *lookup_target(const char *target_name);
00330 bool set_target(const char *target_name,const char *target_path);
00332 const std::vector<std::string> &target_destinations(const std::string &target_name);
00334 dsim_value send_message(const char *destination,const char *message_name,const dsim_value &argument);
00336
00337
00350
00351 dsim_network *create_managed_network(const char *name,const char *description);
00353 dsim_network *managed_network_with_name(const char *name);
00354
00355
00356
00358 double sim_jd() const;
00360 double sim_time() const;
00362 double sim_dt() const;
00363
00364
00366 void error(const char *msg,...) const;
00368 void warning(const char *msg,...) const;
00370 void log(const char *msg,...) const;
00371
00372 private:
00373
00374 void set_parent(dsim_model *parent);
00375
00376 dsim_variable create_variable(const char *name,dsim_variable::dsim_variable_usage usage,int data_type,void *init_value,int rows,int cols,const char *units,const char *description);
00377
00378
00379 void do_integration_step(int step,double t,double dt);
00380
00381
00382 static dsim_model *load_model(const char *type,const char *library,dsim_model_setup *setup);
00383
00384
00385 bool setup_set_value(int line,const char *variable,const char *value);
00386 bool setup_set_connection(int line,const char *variable,const char *source);
00387
00388 void annotate_description(ds_dictionary *desc);
00389
00390 dsim_simulation *_simulation;
00391
00392 std::string _name;
00393 std::string _path;
00394
00395 bool _call_pre_calculate,_call_post_calculate,_call_rhs;
00396
00397 dsim_model *_parent;
00398 std::vector<dsim_model *> _children;
00399
00400 std::map<std::string,dsim_variable> _variables;
00401 std::vector<dsim_variable> _integrated_variables;
00402 std::map<std::string,dsim_outlet> _outlets;
00403 std::map<std::string,dsim_target> _targets;
00404 std::map<std::string,dsim_network *> _managed_networks;
00405 std::map<std::string,std::string> _attributes;
00406 std::map<std::string,dsim_variable> _stored_variables;
00407
00408
00409 dsim_integrator *_integrator;
00410
00411 friend class dsim_simulation;
00412 };
00413
00419 typedef dsim_model *(*dsim_model_factory_t)(dsim_model_setup *);
00420
00421 #endif