00001 #include "GridWise.h"
00002 using namespace adevs;
00003 using namespace std;
00004
00005 const int GridFriendlyNetwork::sensor_trip = 0;
00006 const int GridFriendlyNetwork::load_change = 1;
00007 const int GridFriendlyNetwork::base_load = 2;
00008
00009 GridFriendlyNetwork::GridFriendlyNetwork(ElectricalData* data, double fraction_of_load):
00010 Network<PortValue<BasicEvent*> >()
00011 {
00012
00013 for (unsigned i = 0; i < data->getNodeCount(); i++)
00014 {
00015 GridFriendlyLoad* load = new GridFriendlyLoad(i,fraction_of_load,data);
00016 loads[load] = load;
00017 load->setParent(this);
00018 }
00019 }
00020
00021 void GridFriendlyNetwork::getSensors(list<Sensor*>& sensors)
00022 {
00023
00024 map<GridFriendlyLoad*,GridFriendlyLoad*>::iterator iter = loads.begin();
00025 for (; iter != loads.end(); iter++)
00026 {
00027 sensors.push_back(new GridFriendlySensor((*iter).first));
00028 }
00029 }
00030
00031 void GridFriendlyNetwork::route(const PortValue<BasicEvent*>& x,
00032 Devs<PortValue<BasicEvent*> >* model,
00033 Bag<Event<adevs::PortValue<BasicEvent*> > >& r)
00034 {
00035
00036 if (model == this && x.port == sensor_trip)
00037 {
00038 SensorEvent* sensor_event = dynamic_cast<SensorEvent*>(x.value);
00039 PortValue<BasicEvent*> pv(GridFriendlyLoad::sensor_input,sensor_event);
00040 Event<PortValue<BasicEvent*> > e(sensor_event->getCause()->getDeviceModel(),pv);
00041 r.insert(e);
00042 }
00043
00044 else if (model == this && x.port == base_load)
00045 {
00046
00047 LoadEvent* load_event = dynamic_cast<LoadEvent*>(x.value);
00048 map<GridFriendlyLoad*,GridFriendlyLoad*>::iterator iter = loads.begin();
00049 for (; iter != loads.end(); iter++)
00050 {
00051 if ((*iter).first->getBusID() == load_event->getBusID())
00052 {
00053 PortValue<BasicEvent*> pv(GridFriendlyLoad::base_load,load_event);
00054 Event<PortValue<BasicEvent*> > e((*iter).first,pv);
00055 r.insert(e);
00056 return;
00057 }
00058 }
00059 }
00060
00061 else
00062 {
00063 PortValue<BasicEvent*> pv(load_change,x.value);
00064 Event<PortValue<BasicEvent*> > e(this,pv);
00065 r.insert(e);
00066 }
00067 }
00068
00069 void GridFriendlyNetwork::getComponents(Set<Devs<PortValue<BasicEvent*> >* >& c)
00070 {
00071
00072 map<GridFriendlyLoad*,GridFriendlyLoad*>::iterator iter = loads.begin();
00073 for (; iter != loads.end(); iter++)
00074 {
00075 c.insert((*iter).first);
00076 }
00077
00078 }
00079
00080 GridFriendlyNetwork::~GridFriendlyNetwork()
00081 {
00082
00083 map<GridFriendlyLoad*,GridFriendlyLoad*>::iterator iter = loads.begin();
00084 for (; iter != loads.end(); iter++)
00085 {
00086 delete (*iter).first;
00087 }
00088 }
00089