00001
00009 #include <stdlib.h>
00010 #include <stdio.h>
00011 #include <errno.h>
00012 #include <math.h>
00013
00014 #include "voltdump.h"
00015 #include "node.h"
00016
00018
00020
00021 CLASS* voltdump::oclass = NULL;
00022
00023 voltdump::voltdump(MODULE *mod)
00024 {
00025 if (oclass==NULL)
00026 {
00027
00028 oclass = gl_register_class(mod,"voltdump",sizeof(voltdump),PC_BOTTOMUP);
00029 if (oclass==NULL)
00030 GL_THROW("unable to register object class implemented by %s",__FILE__);
00031
00032
00033 if (gl_publish_variable(oclass,
00034 PT_char32,"group",PADDR(group),PT_DESCRIPTION,"the group ID to output data for (all nodes if empty)",
00035 PT_timestamp,"runtime",PADDR(runtime),PT_DESCRIPTION,"the time to check voltage data",
00036 PT_char32,"filename",PADDR(filename),PT_DESCRIPTION,"the file to dump the voltage data into",
00037 PT_int32,"runcount",PADDR(runcount),PT_ACCESS,PA_REFERENCE,PT_DESCRIPTION,"the number of times the file has been written to",
00038 NULL)<1) GL_THROW("unable to publish properties in %s",__FILE__);
00039
00040 }
00041 }
00042
00043
00044 int voltdump::create(void)
00045 {
00046 memset(group, 0, sizeof(char32));
00047 runtime = TS_NEVER;
00048 runcount = 0;
00049 return 1;
00050 }
00051
00052 int voltdump::init(OBJECT *parent)
00053 {
00054 return 1;
00055 }
00056
00057 int voltdump::isa(char *classname)
00058 {
00059 return strcmp(classname,"voltdump")==0;
00060 }
00061
00062 void voltdump::dump(TIMESTAMP t){
00063 char namestr[64];
00064 char timestr[64];
00065 FINDLIST *nodes = NULL;
00066 OBJECT *obj = NULL;
00067 FILE *outfile = NULL;
00068 node *pnode;
00069
00070
00071
00072 if(group[0] == 0){
00073 nodes = gl_find_objects(FL_NEW,FT_MODULE,SAME,"powerflow",FT_END);
00074 } else {
00075 nodes = gl_find_objects(FL_NEW,FT_MODULE,SAME,"powerflow",AND,FT_GROUPID,SAME,group,FT_END);
00076 }
00077
00078 if(nodes == NULL){
00079 gl_warning("no nodes were found to dump");
00080 return;
00081 }
00082
00083 outfile = fopen(filename, "w");
00084 if(outfile == NULL){
00085 gl_error("voltdump unable to open %s for output", filename);
00086 return;
00087 }
00088
00089
00090
00091
00092
00093 gl_printtime(t, timestr, 64);
00094 fprintf(outfile,"# %s run at %s on %i nodes\n", filename, timestr, nodes->hit_count);
00095 fprintf(outfile,"node_name,voltA_real,voltA_imag,voltB_real,voltB_imag,voltC_real,voltC_imag\n");
00096 while (obj=gl_find_next(nodes,obj)){
00097 if(gl_object_isa(obj, "node", "powerflow")){
00098 pnode = OBJECTDATA(obj,node);
00099 if(obj->name == NULL){
00100 sprintf(namestr, "%s:%i", obj->oclass->name, obj->id);
00101 }
00102 fprintf(outfile,"%s,%f,%f,%f,%f,%f,%f\n",(obj->name ? obj->name : namestr),pnode->voltage[0].Re(),pnode->voltage[0].Im(),pnode->voltage[1].Re(),pnode->voltage[1].Im(),pnode->voltage[2].Re(),pnode->voltage[2].Im());
00103 }
00104 }
00105 fclose(outfile);
00106 }
00107
00108 int voltdump::commit(TIMESTAMP t){
00109 if(runtime == 0){
00110 runtime = t;
00111 }
00112 if((t == runtime || runtime == TS_NEVER) && (runcount < 1)){
00113
00114 dump(t);
00115 ++runcount;
00116 }
00117 return 1;
00118 }
00119
00121
00123
00131 EXPORT int create_voltdump(OBJECT **obj, OBJECT *parent)
00132 {
00133 try
00134 {
00135 *obj = gl_create_object(voltdump::oclass);
00136 if (*obj!=NULL)
00137 {
00138 voltdump *my = OBJECTDATA(*obj,voltdump);
00139 gl_set_parent(*obj,parent);
00140 return my->create();
00141 }
00142 }
00143 catch (const char *msg)
00144 {
00145 gl_error("create_voltdump: %s", msg);
00146 }
00147 return 0;
00148 }
00149
00150 EXPORT int init_voltdump(OBJECT *obj)
00151 {
00152 voltdump *my = OBJECTDATA(obj,voltdump);
00153 try {
00154 return my->init(obj->parent);
00155 }
00156 catch (const char *msg)
00157 {
00158 GL_THROW("%s (voltdump:%d): %s", obj->name, obj->id, msg);
00159 return 0;
00160 }
00161 }
00162
00163 EXPORT TIMESTAMP sync_voltdump(OBJECT *obj, TIMESTAMP t1, PASSCONFIG pass)
00164 {
00165 voltdump *my = OBJECTDATA(obj,voltdump);
00166 TIMESTAMP rv;
00167 obj->clock = t1;
00168 rv = my->runtime > t1 ? my->runtime : TS_NEVER;
00169 return rv;
00170 }
00171
00172 EXPORT int commit_voltdump(OBJECT *obj){
00173 voltdump *my = OBJECTDATA(obj,voltdump);
00174 try {
00175 return my->commit(obj->clock);
00176 } catch(const char *msg){
00177 GL_THROW("%s (voltdump:%d): %s", obj->name, obj->id, msg);
00178 return 0;
00179 }
00180 }
00181
00182 EXPORT int isa_voltdump(OBJECT *obj, char *classname)
00183 {
00184 return OBJECTDATA(obj,voltdump)->isa(classname);
00185 }
00186