00001 #include "GridWise.h"
00002 #include <iostream>
00003 using namespace std;
00004 using namespace adevs;
00005
00011 const int GridFriendlyLoad::sensor_input = 1;
00012 const int GridFriendlyLoad::base_load = 2;
00013
00014 GridFriendlyLoad::GridFriendlyLoad(int BusID, double gfl_fraction, ElectricalData* init_data):
00015 Atomic<PortValue<BasicEvent*> >(),
00016 BusID(BusID),
00017 ybase(init_data->getAdmittance(BusID)),
00018 gfl_fraction(gfl_fraction),
00019 phase(ON)
00020 {
00021 }
00022
00023 void GridFriendlyLoad::delta_int()
00024 {
00025 if (phase == SHED) phase = OFF;
00026 else phase = ON;
00027 }
00028
00029 void GridFriendlyLoad::delta_ext(double e, const Bag<PortValue<BasicEvent*> >& xb)
00030 {
00031 if (phase == RESTORE) trestore -= e;
00032 Bag<PortValue<BasicEvent*> >::const_iterator iter = xb.begin();
00033 for (; iter != xb.end(); iter++)
00034 {
00035 if ((*iter).port == sensor_input)
00036 {
00037 if (phase == ON) phase = SHED;
00038 else if (phase == OFF) { phase = RESTORE; trestore = 16.0; }
00039 else if (phase == RESTORE) phase = OFF;
00040 else assert(false);
00041 }
00042 else if ((*iter).port == base_load)
00043 {
00044 LoadEvent* load_event = dynamic_cast<LoadEvent*>((*iter).value);
00045 if (load_event->getBusID() == BusID)
00046 {
00047 ybase = load_event->getAdmittance();
00048 }
00049 }
00050 }
00051 }
00052
00053 void GridFriendlyLoad::delta_conf(const Bag<PortValue<BasicEvent*> >& xb)
00054 {
00055 delta_int();
00056 delta_ext(0.0,xb);
00057 }
00058
00059 void GridFriendlyLoad::output_func(Bag<PortValue<BasicEvent*> >& yb)
00060 {
00061 Complex new_y = ybase;
00062 if (phase == SHED)
00063 {
00064
00065 new_y *= (1.0-gfl_fraction);
00066 }
00067 else assert(phase == RESTORE);
00068
00069 LoadEvent* load_event = new LoadEvent(BusID,new_y);
00070 yb.insert(PortValue<BasicEvent*>(GridFriendlyNetwork::load_change,load_event));
00071 }
00072
00073 double GridFriendlyLoad::ta()
00074 {
00075
00076 if (phase == SHED) return 0.0;
00077
00078 else if (phase == RESTORE) return trestore;
00079 else return DBL_MAX;
00080 }
00081
00082 void GridFriendlyLoad::gc_output(Bag<PortValue<BasicEvent*> >& g)
00083 {
00084 Bag<PortValue<BasicEvent*> >::iterator iter = g.begin();
00085 for (; iter != g.end(); iter++)
00086 {
00087 delete (*iter).value;
00088 }
00089 }