00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00017 #ifndef __DSIM2_SIMULATION__
00018 #define __DSIM2_SIMULATION__
00019
00020 #include <DSUtils/DSUtils.h>
00021 #include <SDManip/SDManip.h>
00022 #include "dsim_value.h"
00023 #include "dsim_variable.h"
00024 #include "dsim_exceptions.h"
00025
00026 #include <vector>
00027 #include <map>
00028 #include <string>
00029
00030
00031 #ifdef __CYGWIN__
00032 #include <cstdarg>
00033 #endif
00034
00035 class dsim_variable_record;
00036 class dsim_model;
00037 class dsim_simulation;
00038 class dsim_integrator;
00039
00040
00052 typedef void *(*dsim_library_description_function_t)();
00054 void *dsim_create_library_manifest(const char *identifier,const char *library_name,const char *library_description);
00056 void dsim_add_integrator(void *manifest,const char *integrator_type,const char *class_name,const char *integrator_name,const char *integrator_description);
00058 void dsim_add_model(void *manifest,const char *model_type,const char *class_name,const char *model_name,const char *model_description);
00060
00061 extern const char *DSimLibraryManifestKey;
00062 extern const char *DSimLibraryNameKey;
00063 extern const char *DSimLibraryDescriptionKey;
00064 extern const char *DSimLibraryPathKey;
00065 extern const char *DSimLibraryIdentifierKey;
00066
00067 extern const char *DSimIntegratorsKey;
00068 extern const char *DSimIntegratorTypeKey;
00069 extern const char *DSimIntegratorClassKey;
00070 extern const char *DSimIntegratorNameKey;
00071 extern const char *DSimIntegratorDescriptionKey;
00072
00073 extern const char *DSimModelsKey;
00074 extern const char *DSimModelTypeKey;
00075 extern const char *DSimModelClassKey;
00076 extern const char *DSimModelNameKey;
00077 extern const char *DSimModelDescriptionKey;
00078
00079 extern const char *DSimSetupKey;
00080 extern const char *DSimSetupCommandKey;
00081 extern const char *DSimSetupDescriptionKey;
00082
00083 extern const char *DSimVariablesKey;
00084 extern const char *DSimVariableNameKey;
00085 extern const char *DSimVariableUsageKey;
00086 extern const char *DSimVariableUnitsKey;
00087 extern const char *DSimVariableDescriptionKey;
00088 extern const char *DSimVariableTypeKey;
00089 extern const char *DSimVariableInitValueKey;
00090 extern const char *DSimVariableRowsKey;
00091 extern const char *DSimVariableColumnsKey;
00092 extern const char *DSimVariableIsTelemetryKey;
00093 extern const char *DSimVariableIsCommandKey;
00094
00095 extern const char *DSimOutletsKey;
00096 extern const char *DSimOutletNameKey;
00097 extern const char *DSimOutletTypeKey;
00098 extern const char *DSimOutletUnitsKey;
00099 extern const char *DSimOutletDescriptionKey;
00100
00101 extern const char *DSimTargetsKey;
00102 extern const char *DSimTargetNameKey;
00103 extern const char *DSimTargetDescriptionKey;
00104 extern const char *DSimTargetMultipleKey;
00105
00106 extern const char *DSimNetworksKey;
00107 extern const char *DSimNetworkNameKey;
00108 extern const char *DSimNetworkDescriptionKey;
00109 extern const char *DSimNetworkVariablesKey;
00110 extern const char *DSimNetworkVariableNameKey;
00111 extern const char *DSimNetworkVariableTypeKey;
00112 extern const char *DSimNetworkVariableDescriptionKey;
00113
00121 class dsim_simulation
00122 {
00123 public:
00124 dsim_simulation(const char *filename,sd_data_type_manager *tm = NULL);
00125 ~dsim_simulation();
00126
00127
00128 static ds_dictionary *manifest_for_library(const char *path);
00129 static ds_dictionary *manifests_for_directory(const char *base_directory = NULL);
00130
00131
00132 void parse_setup_file(const char *filename);
00133 int parse_memory(const char *blob, int blob_len, bool report_errs=false);
00134
00135 void start_simulation();
00136 bool run_simulation_step();
00137 void pause_simulation();
00138 void resume_simulation();
00139 ds_date next_step_time();
00140
00141
00142 dsim_variable create_variable(const char *path,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);
00143 dsim_variable request_variable(const char *path);
00144
00145
00146 dsim_integrator *create_integrator(const char *name,const char *type,const char *library);
00147 dsim_integrator *lookup_integrator(const char *name);
00148
00149
00150 dsim_model *lookup_object(const char *path);
00151
00152
00153 enum dsim_log_priority { dsim_log_info=0, dsim_log_warning=1, dsim_log_error=2, dsim_log_none=3 };
00154 typedef void (*dsim_output_handler_t)(dsim_simulation::dsim_log_priority priority,const char *module,const char *msg,void *arg);
00155 void set_output_logfile(FILE *logfile);
00156 bool set_output_logfile(const char *filename);
00157 void set_output_loglevel(dsim_simulation::dsim_log_priority priority);
00158 void set_output_handler(dsim_output_handler_t new_handler,void *new_argument);
00159 void set_data_logfile(FILE *logfile);
00160 bool set_data_logfile(const char *filename);
00161 void log(const char *format,...);
00162 void warning(const char *format,...);
00163 void error(const char *format,...);
00164
00165 bool has_error() const { return sim_error; }
00166
00167
00168 std::map<std::string,dsim_variable>::iterator variables_iterator();
00169 std::map<std::string,dsim_variable>::iterator variables_iterator_end();
00170
00171 sd_data_type_manager *type_manager() { return _type_manager; }
00172 private:
00173
00174 void add_object(dsim_model *object);
00175 void add_object_dependency(const char *dependent_object,const char *independent_object);
00176 void parse_setup_file_v1(void *node);
00177 bool parse_integrator_v1(void *node);
00178 bool parse_object_pass1_v1(dsim_model *parent,void *node);
00179 bool parse_object_pass2_v1(dsim_model *parent,void *node);
00180
00181 sd_data_type_manager *_type_manager;
00182
00183 std::map<std::string,dsim_variable> variables;
00184 std::vector<dsim_model *> object_list;
00185 std::map<std::string,dsim_model *> object_map;
00186 std::map<std::string,dsim_integrator *> integrators;
00187 ds_dependency_graph dep_graph;
00188
00189
00190 dsim_value send_message(dsim_model *sender,const char *destination,const char *message_name,const dsim_value &argument);
00191
00192
00193 void voutput(dsim_simulation::dsim_log_priority priority,const char *module,const char *format,va_list arg);
00194 FILE *data_log, *output_log;
00195 bool close_data, close_output;
00196 enum dsim_simulation::dsim_log_priority min_output_priority;
00197 dsim_output_handler_t output_handler;
00198 void *output_argument;
00199
00200
00201 void add_logged_variable(const char *path);
00202 void write_log_header();
00203 void write_timestep_log(double ts);
00204 void close_log();
00205 std::vector<dsim_variable> logged_variables;
00206 std::string log_header;
00207
00208
00209 dsim_variable sim_t;
00210 dsim_variable sim_dt;
00211 double sim_max_dt;
00212 dsim_variable sim_scale;
00213 dsim_variable sim_jd;
00214 dsim_variable sim_paused;
00215 dsim_variable sim_start_jd;
00216 dsim_variable sim_end_time;
00217 dsim_variable sim_start_time;
00218 dsim_variable sim_name;
00219
00220 bool sim_error;
00221 ds_date last_step_time;
00222
00223
00224 void set_thread_count(unsigned int threads);
00225 void start_workers();
00226 void simulation_work_thread();
00227 void do_simulation_stage();
00228 void wait_for_worker_close();
00229 bool step_simulation();
00230 pthread_mutex_t work_mutex;
00231 pthread_cond_t work_cond;
00232 unsigned int worker_count;
00233 unsigned int max_workers;
00241 int work_op;
00242 int work_stage;
00243 int work_integ_step;
00244 bool worker_shutdown;
00245
00246
00247 friend void *dsim_worker_thread(void *obj);
00248 friend class dsim_model;
00249 friend class dsim_integrator;
00250 friend class dsim_variable_record;
00251 };
00252
00253 #endif