commercial/multizone.cpp

Go to the documentation of this file.
00001 
00016 #include <stdlib.h>
00017 #include <stdio.h>
00018 #include <errno.h>
00019 
00020 #include "lock.h"
00021 #include "office.h"
00022 #include "multizone.h"
00023 
00024 CLASS *multizone::oclass = NULL;
00025 multizone *multizone::defaults = NULL;
00026 
00027 static PASSCONFIG passconfig = PC_BOTTOMUP;
00028 static PASSCONFIG clockpass = PC_BOTTOMUP;
00029 
00030 /* Class registration is only called once to register the class with the core */
00031 multizone::multizone(MODULE *module)
00032 {
00033     if (oclass==NULL)
00034     {
00035         oclass = gl_register_class(module,"multizone",passconfig);
00036         if (oclass==NULL)
00037             GL_THROW("unable to register object class implemented by %s", __FILE__);
00038 
00039         if (gl_publish_variable(oclass,
00040             /* TODO: add your published properties here */
00041             PT_object, "from", PADDR(from),
00042             PT_object, "to", PADDR(to),
00043             PT_double, "ua", PADDR(ua),
00044             NULL)<1) GL_THROW("unable to publish properties in %s",__FILE__);
00045         defaults = this;
00046         /* TODO: set the default values of all properties here */
00047         memset(this,0,sizeof(multizone));
00048     }
00049 }
00050 
00051 /* Object creation is called once for each object that is created by the core */
00052 int multizone::create(void)
00053 {
00054     memcpy(this,defaults,sizeof(multizone));
00055     return 1; /* return 1 on success, 0 on failure */
00056 }
00057 
00058 /* Object initialization is called once after all object have been created */
00059 int multizone::init(OBJECT *parent)
00060 {
00061     OBJECT *obj = OBJECTHDR(this);
00062     if (from==NULL)
00063         gl_error("%s (multizone:%d): from zone is not specified", obj->name?obj->name:"unnamed",obj->id);
00064     else if (!gl_object_isa(from,"office"))
00065         gl_error("%s (multizone:%d): from object is not an commercial office space", obj->name?obj->name:"unnamed",obj->id);
00066     if (to==NULL)
00067         gl_error("%s (multizone:%d): to zone is not specified", obj->name?obj->name:"unnamed",obj->id);
00068     else if (!gl_object_isa(to,"office"))
00069         gl_error("%s (multizone:%d): to object is not an commercial office space", obj->name?obj->name:"unnamed",obj->id);
00070     if (ua<=0)
00071         gl_error("%s (multizone:%d): ua must be positive (value is %.2f)", obj->name?obj->name:"unnamed",obj->id,ua);
00072     gl_set_rank(from,obj->rank+1);
00073     gl_set_rank(to,obj->rank+1);
00074     return 1; /* return 1 on success, 0 on failure */
00075 }
00076 
00077 /* Presync is called when the clock needs to advance on the first top-down pass */
00078 TIMESTAMP multizone::presync(TIMESTAMP t0, TIMESTAMP t1)
00079 {
00080     TIMESTAMP t2 = TS_NEVER;
00081     return t2; /* return t2>t1 on success, t2=t1 for retry, t2<t1 on failure */
00082 }
00083 
00084 /* Sync is called when the clock needs to advance on the bottom-up pass */
00085 TIMESTAMP multizone::sync(TIMESTAMP t0, TIMESTAMP t1)
00086 {
00087     if (t1>t0 && t0>0)
00088     {
00089         office *pFrom = OBJECTDATA(from,office);
00090         office *pTo = OBJECTDATA(to,office);
00091     
00092         /* initial delta T */
00093         double dT = pFrom->zone.current.air_temperature - pTo->zone.current.air_temperature;
00094 
00095         /* rate of change of delta T */
00096         double ddT = pFrom->zone.current.temperature_change - pTo->zone.current.temperature_change;
00097 
00098         /* mean delta T */
00099         double DT = dT + ddT/2;
00100 
00101         /* mean heat transfer */
00102         double dQ = ua * DT * (t1-t0)/TS_SECOND/3600;
00103     
00104         LOCK_OBJECT(from);
00105         pFrom->Qz -= dQ*(t1-t0);
00106         UNLOCK_OBJECT(from);
00107     
00108         LOCK_OBJECT(to);
00109         pTo->Qz += dQ;
00110         UNLOCK_OBJECT(to);
00111 
00112         if (ddT!=0)
00113 
00114             /* time for 1 deg temperature change */
00115             return (TIMESTAMP)(t1+fabs(1/ddT)*3600*TS_SECOND); 
00116         else
00117             return TS_NEVER;
00118     }
00119 
00120     return TS_NEVER; /* return t2>t1 on success, t2=t1 for retry, t2<t1 on failure */
00121 }
00122 
00123 /* Postsync is called when the clock needs to advance on the second top-down pass */
00124 TIMESTAMP multizone::postsync(TIMESTAMP t0, TIMESTAMP t1)
00125 {
00126     TIMESTAMP t2 = TS_NEVER;
00127     return t2; /* return t2>t1 on success, t2=t1 for retry, t2<t1 on failure */
00128 }
00129 
00131 // IMPLEMENTATION OF CORE LINKAGE
00133 
00134 EXPORT int create_multizone(OBJECT **obj, OBJECT *parent)
00135 {
00136     try
00137     {
00138         *obj = gl_create_object(multizone::oclass,sizeof(multizone));
00139         if (*obj!=NULL)
00140         {
00141             multizone *my = OBJECTDATA(*obj,multizone);
00142             gl_set_parent(*obj,parent);
00143             return my->create();
00144         }
00145     }
00146     catch (char *msg)
00147     {
00148         gl_error("create_multizone: %s", msg);
00149     }
00150     return 1;
00151 }
00152 
00153 EXPORT int init_multizone(OBJECT *obj, OBJECT *parent)
00154 {
00155     try
00156     {
00157         if (obj!=NULL)
00158             return OBJECTDATA(obj,multizone)->init(parent);
00159     }
00160     catch (char *msg)
00161     {
00162         gl_error("init_multizone(obj=%d;%s): %s", obj->id, obj->name?obj->name:"unnamed", msg);
00163     }
00164     return 1;
00165 }
00166 
00167 EXPORT TIMESTAMP sync_multizone(OBJECT *obj, TIMESTAMP t1, PASSCONFIG pass)
00168 {
00169     TIMESTAMP t2 = TS_NEVER;
00170     multizone *my = OBJECTDATA(obj,multizone);
00171     try
00172     {
00173         switch (pass) {
00174         case PC_PRETOPDOWN:
00175             t2 = my->presync(obj->clock,t1);
00176             break;
00177         case PC_BOTTOMUP:
00178             t2 = my->sync(obj->clock,t1);
00179             break;
00180         case PC_POSTTOPDOWN:
00181             t2 = my->postsync(obj->clock,t1);
00182             break;
00183         default:
00184             GL_THROW("invalid pass request (%d)", pass);
00185             break;
00186         }
00187         if (pass==clockpass)
00188             obj->clock = t1;
00189     }
00190     catch (char *msg)
00191     {
00192         gl_error("sync_multizone(obj=%d;%s): %s", obj->id, obj->name?obj->name:"unnamed", msg);
00193     }
00194     return t2;
00195 }

GridLAB-DTM Version 1.0
An open-source project initiated by the US Department of Energy