power_system.cc

Go to the documentation of this file.
00001 
00008 #include "power_system.h"
00009 #include <DSimEngine2/dsim_connection.h>
00010 
00011 power_system :: power_system(dsim_model_setup *setup) : dsim_model(setup)
00012 {
00013     printf("power system starting up\n");
00014 }
00015 
00016 void power_system :: initialize_data()
00017 {
00018         dsim_model::initialize_data();
00019     
00020     // The power network
00021     power_network_dsim = create_managed_network("power", "Power system network" );
00022     
00023     // The variable in the network
00024         power_network_dsim->declare_variable("power", sd_type_double, "Power input or output");
00025     
00026     double power = 0;
00027     net_power_dsim = create_output("net_power", sd_type_double, &power,1, 1, "W", "Net power");
00028     net_power_dsim.mark_telemetry();
00029     
00030     int n = 0;
00031     number_connections_dsim = create_output("number_connections", sd_type_int, &n, 1, 1, "", "Number of connections");
00032     number_connections_dsim.mark_telemetry();   
00033 
00034         configure_timestep(true, false, false);
00035 }
00036 
00037 void power_system :: initialization_complete()
00038 {
00039     dsim_model::initialization_complete();
00040 }
00041 
00042 void power_system :: pre_calculate()
00043 {
00044         dsim_model::initialize_timestep();      
00045     
00046     // This is to iterate through the network
00047     dsim_connection_iterator iterator = power_network_dsim->connection_iterator();
00048     
00049     double net_power = 0;
00050     int c_count = power_network_dsim->connection_count();
00051 
00052     // If there are any connections find the power consumed or produced
00053     for (int k = 0; k < c_count; k++ )
00054     {
00055         dsim_connection *c = iterator.connection(); 
00056         std::string variable_path = std::string(c->destination_path()) + ":power";
00057         dsim_variable var = request_variable(variable_path.c_str());
00058         dsim_value v = var.value(); // or var.cointegrated_value() if desired
00059         if (v.valid())
00060         {
00061             net_power += v.double_value();
00062         }
00063         iterator.next();
00064     }
00065     
00066 
00067     net_power_dsim.set_value_as_double(net_power);
00068     number_connections_dsim.set_value_as_int(c_count);
00069 }
00070 
00071 
00072 extern "C"
00073 {
00074     dsim_model *PowerSystem(void *setup);
00075     
00076         dsim_model *PowerSystem(void *setup)
00077         {
00078                 return new power_system((dsim_model_setup *)setup);
00079         }
00080 }
 All Classes Files Functions Variables