00001
00013 #include <stdlib.h>
00014 #include <stdio.h>
00015 #include <errno.h>
00016 #include <math.h>
00017
00018 #include "plugload.h"
00019
00021
00023 CLASS* plugload::oclass = NULL;
00024 CLASS* plugload::pclass = NULL;
00025
00026 plugload::plugload(MODULE *module) : residential_enduse(module)
00027 {
00028
00029 if (oclass==NULL)
00030 {
00031
00032 oclass = gl_register_class(module,"plugload",sizeof(plugload),PC_BOTTOMUP|PC_AUTOLOCK);
00033 if (oclass==NULL)
00034 throw "unable to register class plugload";
00035 else
00036 oclass->trl = TRL_QUALIFIED;
00037
00038
00039 if (gl_publish_variable(oclass,
00040 PT_INHERIT, "residential_enduse",
00041 PT_double,"circuit_split",PADDR(circuit_split),
00042 PT_double,"demand[unit]",PADDR(shape.load),
00043 PT_double,"installed_power[kW]",PADDR(shape.params.analog.power), PT_DESCRIPTION, "installed plugs capacity",
00044 PT_complex,"actual_power[kVA]",PADDR(plugs_actual_power),PT_DESCRIPTION,"actual power demand",
00045 NULL)<1)
00046 GL_THROW("unable to publish properties in %s",__FILE__);
00047 }
00048 }
00049
00050 plugload::~plugload()
00051 {
00052 }
00053
00054 int plugload::create()
00055 {
00056 int res = residential_enduse::create();
00057
00058
00059 load.name = oclass->name;
00060 load.power = load.admittance = load.current = load.total = complex(0,0,J);
00061 load.power_fraction = load.current_fraction = load.impedance_fraction = 0;
00062 load.heatgain_fraction = 0.90;
00063 load.power_factor = 0.90;
00064
00065 load.voltage_factor = 1.0;
00066 shape.load = gl_random_uniform(RNGSTATE,0, 0.1);
00067 return res;
00068 }
00069
00070 int plugload::init(OBJECT *parent)
00071 {
00072 OBJECT *hdr = OBJECTHDR(this);
00073 hdr->flags |= OF_SKIPSAFE;
00074
00075 load.breaker_amps = 40;
00076
00077 if ( (load.power_fraction + load.current_fraction + load.impedance_fraction) == 0.0)
00078 {
00079 load.power_fraction = 1.0;
00080 load.current_fraction = 0.0;
00081 load.impedance_fraction = 0.0;
00082 }
00083
00084 return residential_enduse::init(parent);
00085 }
00086
00087 int plugload::isa(char *classname)
00088 {
00089 return (strcmp(classname,"plugload")==0 || residential_enduse::isa(classname));
00090 }
00091
00092 TIMESTAMP plugload::sync(TIMESTAMP t0, TIMESTAMP t1)
00093 {
00094 TIMESTAMP t2 = TS_NEVER;
00095 double temp_voltage_magnitude;
00096 double val = 0.0;
00097
00098 if (pCircuit!=NULL)
00099 {
00100
00101 temp_voltage_magnitude = (pCircuit->pV->get_complex()).Mag();
00102
00103 load.voltage_factor = temp_voltage_magnitude / default_line_voltage;
00104 }
00105
00106 t2 = residential_enduse::sync(t0,t1);
00107
00108 if (pCircuit->status==BRK_CLOSED)
00109 {
00110 if (shape.type == MT_UNKNOWN)
00111 {
00112 if(shape.load < 0.0){
00113 gl_error("plugload demand cannot be negative, capping");
00114 shape.load = 0.0;
00115 }
00116 load.power = load.power_fraction * shape.load;
00117 load.current = load.current_fraction * shape.load;
00118 load.admittance = load.impedance_fraction * shape.load;
00119 if(fabs(load.power_factor) < 1 && load.power_factor != 0.0){
00120 val = (load.power_factor < 0 ? -1.0 : 1.0) * load.power.Re() * sqrt(1/(load.power_factor * load.power_factor) - 1);
00121 } else {
00122 val = 0;
00123 }
00124 load.power.SetRect(load.power.Re(), val);
00125 }
00126 }
00127 else
00128 load.power = load.current = load.admittance = complex(0,0,J);
00129
00130 gl_enduse_sync(&(residential_enduse::load),t1);
00131
00132 plugs_actual_power = load.power + (load.current + load.admittance * load.voltage_factor) * load.voltage_factor;
00133 return t2;
00134 }
00135
00137
00139
00140 EXPORT int create_plugload(OBJECT **obj, OBJECT *parent)
00141 {
00142 try
00143 {
00144 *obj = gl_create_object(plugload::oclass);
00145 if (*obj!=NULL)
00146 {
00147 plugload *my = OBJECTDATA(*obj,plugload);;
00148 gl_set_parent(*obj,parent);
00149 my->create();
00150 return 1;
00151 }
00152 else
00153 return 0;
00154 }
00155 CREATE_CATCHALL(plugload);
00156 }
00157
00158 EXPORT int init_plugload(OBJECT *obj)
00159 {
00160 try
00161 {
00162 plugload *my = OBJECTDATA(obj,plugload);
00163 return my->init(obj->parent);
00164 }
00165 INIT_CATCHALL(plugload);
00166 }
00167
00168 EXPORT int isa_plugload(OBJECT *obj, char *classname)
00169 {
00170 if(obj != 0 && classname != 0){
00171 return OBJECTDATA(obj,plugload)->isa(classname);
00172 } else {
00173 return 0;
00174 }
00175 }
00176
00177 EXPORT TIMESTAMP sync_plugload(OBJECT *obj, TIMESTAMP t0)
00178 {
00179 try
00180 {
00181 plugload *my = OBJECTDATA(obj, plugload);
00182 TIMESTAMP t1 = my->sync(obj->clock, t0);
00183 obj->clock = t0;
00184 return t1;
00185 }
00186 SYNC_CATCHALL(plugload);
00187 }
00188