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