00001
00009 #include <stdlib.h>
00010 #include <stdio.h>
00011 #include <errno.h>
00012 #include <math.h>
00013
00014 #include "voltdump.h"
00015
00017
00019
00020 CLASS* voltdump::oclass = NULL;
00021
00022 voltdump::voltdump(MODULE *mod)
00023 {
00024 if (oclass==NULL)
00025 {
00026
00027 oclass = gl_register_class(mod,"voltdump",sizeof(voltdump),PC_BOTTOMUP|PC_AUTOLOCK);
00028 if (oclass==NULL)
00029 throw "unable to register class voltdump";
00030 else
00031 oclass->trl = TRL_PROVEN;
00032
00033
00034 if (gl_publish_variable(oclass,
00035 PT_char32,"group",PADDR(group),PT_DESCRIPTION,"the group ID to output data for (all nodes if empty)",
00036 PT_timestamp,"runtime",PADDR(runtime),PT_DESCRIPTION,"the time to check voltage data",
00037 PT_char256,"filename",PADDR(filename),PT_DESCRIPTION,"the file to dump the voltage data into",
00038 PT_char256,"file",PADDR(filename),PT_DESCRIPTION,"the file to dump the voltage data into",
00039 PT_int32,"runcount",PADDR(runcount),PT_ACCESS,PA_REFERENCE,PT_DESCRIPTION,"the number of times the file has been written to",
00040 PT_enumeration, "mode", PADDR(mode),PT_DESCRIPTION,"dumps the voltages in either polar or rectangular notation",
00041 PT_KEYWORD, "RECT", (enumeration)VDM_RECT,
00042 PT_KEYWORD, "POLAR", (enumeration)VDM_POLAR,
00043 NULL)<1) GL_THROW("unable to publish properties in %s",__FILE__);
00044
00045 }
00046 }
00047
00048
00049 int voltdump::create(void)
00050 {
00051 group.erase();
00052 runtime = TS_NEVER;
00053 runcount = 0;
00054 mode = VDM_RECT;
00055 return 1;
00056 }
00057
00058 int voltdump::init(OBJECT *parent)
00059 {
00060 return 1;
00061 }
00062
00063 int voltdump::isa(char *classname)
00064 {
00065 return strcmp(classname,"voltdump")==0;
00066 }
00067
00068 void voltdump::dump(TIMESTAMP t){
00069 char namestr[64];
00070 char timestr[64];
00071 FINDLIST *nodes = NULL;
00072 OBJECT *obj = NULL;
00073 FILE *outfile = NULL;
00074
00075 gld_property *node_voltage_value_link[3];
00076 complex node_voltage_values[3];
00077
00078 if(group[0] == 0){
00079 nodes = gl_find_objects(FL_NEW,FT_MODULE,SAME,"powerflow",FT_END);
00080 } else {
00081 nodes = gl_find_objects(FL_NEW,FT_MODULE,SAME,"powerflow",AND,FT_GROUPID,SAME,group.get_string(),FT_END);
00082 }
00083
00084 if(nodes == NULL){
00085 gl_warning("no nodes were found to dump");
00086 return;
00087 }
00088
00089 outfile = fopen(filename, "w");
00090 if(outfile == NULL){
00091 gl_error("voltdump unable to open %s for output", filename.get_string());
00092 return;
00093 }
00094
00095
00096
00097
00098
00099 gl_printtime(t, timestr, 64);
00100 fprintf(outfile,"# %s run at %s on %i nodes\n", filename.get_string(), timestr, nodes->hit_count);
00101 if (mode == VDM_RECT)
00102 fprintf(outfile,"node_name,voltA_real,voltA_imag,voltB_real,voltB_imag,voltC_real,voltC_imag\n");
00103 else if (mode == VDM_POLAR)
00104 fprintf(outfile,"node_name,voltA_mag,voltA_angle,voltB_mag,voltB_angle,voltC_mag,voltC_angle\n");
00105
00106 obj = 0;
00107 while (obj=gl_find_next(nodes,obj)){
00108 if(gl_object_isa(obj, "node", "powerflow")){
00109
00110
00111
00112 node_voltage_value_link[0] = new gld_property(obj,"voltage_A");
00113
00114
00115 if ((node_voltage_value_link[0]->is_valid() != true) || (node_voltage_value_link[0]->is_complex() != true))
00116 {
00117 GL_THROW("voltdump - Unable to map voltage property of node:%d - %s",obj->id,(obj->name ? obj->name : "Unnamed"));
00118
00119
00120
00121
00122 }
00123
00124
00125 node_voltage_value_link[1] = new gld_property(obj,"voltage_B");
00126
00127
00128 if ((node_voltage_value_link[1]->is_valid() != true) || (node_voltage_value_link[1]->is_complex() != true))
00129 {
00130 GL_THROW("voltdump - Unable to map voltage property of node:%d - %s",obj->id,(obj->name ? obj->name : "Unnamed"));
00131
00132 }
00133
00134
00135 node_voltage_value_link[2] = new gld_property(obj,"voltage_C");
00136
00137
00138 if ((node_voltage_value_link[2]->is_valid() != true) || (node_voltage_value_link[2]->is_complex() != true))
00139 {
00140 GL_THROW("voltdump - Unable to map voltage property of node:%d - %s",obj->id,(obj->name ? obj->name : "Unnamed"));
00141
00142 }
00143
00144
00145 node_voltage_values[0] = node_voltage_value_link[0]->get_complex();
00146 node_voltage_values[1] = node_voltage_value_link[1]->get_complex();
00147 node_voltage_values[2] = node_voltage_value_link[2]->get_complex();
00148
00149 if(obj->name == NULL){
00150 sprintf(namestr, "%s:%i", obj->oclass->name, obj->id);
00151 }
00152 if(mode == VDM_RECT){
00153 fprintf(outfile,"%s,%f,%f,%f,%f,%f,%f\n",(obj->name ? obj->name : namestr),node_voltage_values[0].Re(),node_voltage_values[0].Im(),node_voltage_values[1].Re(),node_voltage_values[1].Im(),node_voltage_values[2].Re(),node_voltage_values[2].Im());
00154 } else if(mode == VDM_POLAR){
00155 fprintf(outfile,"%s,%f,%f,%f,%f,%f,%f\n",(obj->name ? obj->name : namestr),node_voltage_values[0].Mag(),node_voltage_values[0].Arg(),node_voltage_values[1].Mag(),node_voltage_values[1].Arg(),node_voltage_values[2].Mag(),node_voltage_values[2].Arg());
00156 }
00157
00158
00159 delete node_voltage_value_link[0];
00160 delete node_voltage_value_link[1];
00161 delete node_voltage_value_link[2];
00162 }
00163 }
00164 fclose(outfile);
00165
00166
00167 gl_free(nodes);
00168 }
00169
00170 TIMESTAMP voltdump::commit(TIMESTAMP t){
00171 if(runtime == 0){
00172 runtime = t;
00173 }
00174 if((t >= runtime || runtime == TS_NEVER) && (runcount < 1)){
00175
00176 dump(t);
00177 ++runcount;
00178 }
00179 return TS_NEVER;
00180 }
00181
00183
00185
00193 EXPORT int create_voltdump(OBJECT **obj, OBJECT *parent)
00194 {
00195 try
00196 {
00197 *obj = gl_create_object(voltdump::oclass);
00198 if (*obj!=NULL)
00199 {
00200 voltdump *my = OBJECTDATA(*obj,voltdump);
00201 gl_set_parent(*obj,parent);
00202 return my->create();
00203 }
00204 else
00205 return 0;
00206 }
00207 CREATE_CATCHALL(voltdump);
00208 }
00209
00210 EXPORT int init_voltdump(OBJECT *obj)
00211 {
00212 try {
00213 voltdump *my = OBJECTDATA(obj,voltdump);
00214 return my->init(obj->parent);
00215 }
00216 INIT_CATCHALL(voltdump);
00217 }
00218
00219 EXPORT TIMESTAMP sync_voltdump(OBJECT *obj, TIMESTAMP t1, PASSCONFIG pass)
00220 {
00221 try
00222 {
00223 voltdump *my = OBJECTDATA(obj,voltdump);
00224 TIMESTAMP rv;
00225 obj->clock = t1;
00226 rv = my->runtime > t1 ? my->runtime : TS_NEVER;
00227 return rv;
00228 }
00229 SYNC_CATCHALL(voltdump);
00230 }
00231
00232 EXPORT TIMESTAMP commit_voltdump(OBJECT *obj, TIMESTAMP t1, TIMESTAMP t2){
00233 try {
00234 voltdump *my = OBJECTDATA(obj,voltdump);
00235 return my->commit(t1);
00236 }
00237 I_CATCHALL(commit,voltdump);
00238 }
00239
00240 EXPORT int isa_voltdump(OBJECT *obj, char *classname)
00241 {
00242 return OBJECTDATA(obj,voltdump)->isa(classname);
00243 }
00244