STATUS test_init(void) { OBJECT *obj; output_verbose("initializing objects..."); for (obj=object_get_first(); obj!=NULL; obj=object_get_next(obj)) { if (object_init(obj)==FAILED) { output_error("object %s initialization failed", object_name(obj)); return FAILED; } } return SUCCESS; }
size_t stream(FILE *fileptr,int opts) { stream_pos = 0; fp = fileptr; flags = opts; output_debug("starting stream on file %d with options %x", fileno(fp), flags); try { // header stream("GLD30"); // runtime classes try { stream(class_get_first_runtime()); } catch (int) {}; // modules try { stream(module_get_first()); } catch (int) {} // objects try { stream(object_get_first()); } catch (int) {}; // globals try { stream(global_getnext(NULL)); } catch (int) {}; // module data struct s_stream *s; for ( s=stream_list ; s!=NULL ; s=s->next ) { s->call((int)flags,(STREAMCALLBACK)stream_callback); } output_debug("done processing stream on file %d with options %x", fileno(fp), flags); return stream_pos; } catch (const char *msg) { output_error("stream() unexpected %s at offset %lld", msg, (int64)stream_pos); return -1; } catch (...) { output_error("stream() failed as offset %lld", (int64)stream_pos); return -1; } }
/** endDocument() is called when the end of the file is reached. Any post-processing within the parser should occur here. */ void gld_loadHndl::endDocument(){ OBJECT *tempobj = object_get_first(); /* resolve_list(); */ if(load_resolve_all() == FAILED){ output_error("XML_Load: unable to resolve object linkings!"); load_state = false; } /* "establish ranks" */ for (; obj!=NULL; obj=obj->next) object_set_parent(obj,obj->parent); output_verbose("XML_Load: %d object%s loaded.", object_get_count(), object_get_count() > 0 ? "s" : ""); if(object_count > 0) if(object_get_count() != this->object_count) output_warning("XML_Load: we expected %i objects instead!", this->object_count); output_verbose("XML_Load: %d class%s loaded.", class_get_count(), class_get_count() > 0 ? "es" : ""); if(class_count > 0) if(class_get_count() != this->class_count) output_warning("XML_Load: we expected %i classes instead!", this->class_count); }
/** sanitize Sanitizes a gridlabd model by clear names and position from object headers @returns 0 on success, -2 on error **/ extern "C" int sanitize(int argc, char *argv[]) { OBJECT *obj; FILE *fp; double delta_latitude, delta_longitude; // lat/lon change if ( strcmp(global_sanitizeoffset,"")==0 ) { delta_latitude = random_uniform(NULL,-5,+5); delta_longitude = random_uniform(NULL,-180,+180); } else if ( global_sanitizeoffset=="destroy" ) delta_latitude = delta_longitude = QNAN; else if ( sscanf(global_sanitizeoffset.get_string(),"%lf%*[,/]%lf",&delta_latitude,&delta_longitude)!=2 ) { output_error("sanitize_offset lat/lon '%s' is not valid", global_sanitizeoffset.get_string()); return -2; } // sanitize object names for ( obj=object_get_first() ; obj!=NULL ; obj=object_get_next(obj) ) { if ( obj->name!=NULL && (global_sanitizeoptions&SO_NAMES)==SO_NAMES ) sanitize_name(obj); if ( isfinite(obj->latitude) && (global_sanitizeoptions&SO_GEOCOORDS)==SO_GEOCOORDS ) { obj->latitude += delta_latitude; if ( obj->latitude<-90 ) obj->latitude = -90; if ( obj->latitude>+90 ) obj->latitude = +90; } if ( isfinite(obj->longitude) && (global_sanitizeoptions&SO_GEOCOORDS)==SO_GEOCOORDS ) obj->longitude = fmod(obj->longitude+delta_longitude,360); } // dump object name index if ( strcmp(global_sanitizeindex,".xml")==0 ) { strcpy(global_sanitizeindex,global_modelname); char *ext = strrchr(global_sanitizeindex,'.'); if ( ext && strcmp(ext,".glm")==0 ) strcpy(ext,"-index.xml"); else strcat(global_sanitizeindex,"-index.xml"); } else if ( strcmp(global_sanitizeindex,".txt")==0 ) { strcpy(global_sanitizeindex,global_modelname); char *ext = strrchr(global_sanitizeindex,'.'); if ( ext && strcmp(ext,".glm")==0 ) strcpy(ext,"-index.txt"); else strcat(global_sanitizeindex,"-index.txt"); } else if ( global_sanitizeindex[0]=='.' ) { output_error("sanitization index file spec '%s' is not recognized", global_sanitizeindex.get_string()); return -2; } if ( strcmp(global_sanitizeindex,"")!=0 ) { char *ext = strrchr(global_sanitizeindex,'.'); bool use_xml = (ext && strcmp(ext,".xml")==0) ; fp = fopen(global_sanitizeindex,"w"); if ( fp ) { SAFENAME *item; if ( use_xml ) { fprintf(fp,"<data>\n"); fprintf(fp,"\t<modelname>%s</modelname>\n",global_modelname); fprintf(fp,"\t<geographic_offsets>\n"); fprintf(fp,"\t\t<latitude>%.6f</latitude>\n",delta_latitude); fprintf(fp,"\t\t<longitude>%.6f</longitude>\n",delta_longitude); fprintf(fp,"\t</geographic_offsets>\n"); fprintf(fp,"\t<safename_list>\n"); for ( item=safename_list ; item!=NULL ; item=item->next ) fprintf(fp,"\t\t<name>\n\t\t\t<safe>%s</safe>\n\t\t\t<unsafe>%s</unsafe>\n\t\t</name>\n", item->name, item->old); fprintf(fp,"\t</safename_list>\n"); fprintf(fp,"</data>\n"); } else { fprintf(fp,"modelname\t= %s\n", global_modelname); fprintf(fp,"\n[POSITIONS]\n"); fprintf(fp,"latitude\t= %.6f\n",delta_latitude); fprintf(fp,"longitude\t= %.6f\n",delta_longitude); fprintf(fp,"\n[NAMES]\n"); for ( item=safename_list ; item!=NULL ; item=item->next ) fprintf(fp,"%s\t= %s\n", item->name, item->old); } fclose(fp); } } return 0; }
int kml_document(FILE *fp) { CLASS *oclass, *openclass=NULL; MODULE *mod; char buffer[1024]; time_t now = time(NULL); kml_write("%s"," <Document>\n"); kml_write(" <name>%s</name>\n", global_modelname); kml_write(" <description>GridLAB-D results for %s</description>\n", convert_from_timestamp(global_clock,buffer,sizeof(buffer))?buffer:"unknown date/time"); /* for each module */ for (mod=module_get_first(); mod!=NULL; mod=mod->next) { if (mod->kmldump) mod->kmldump(kml_write,NULL); /* dump styles */ } /* scan each class in the model */ for (oclass=class_get_first_class(); oclass!=NULL; oclass=oclass->next) { OBJECT *obj; /* scan each object in the model */ for (obj=object_get_first(); obj!=NULL; obj=obj->next) { int has_location = !(isnan(obj->latitude) || isnan(obj->longitude)); MODULE *mod; /* class does not match current object */ if (obj->oclass!=oclass) continue; /* first instance of this class needs folder */ else if (openclass==NULL) { kml_write(" <Folder><name>Class %s</name>\n", oclass->name); kml_write(" <description>Module %s",oclass->module->name); if (oclass->module->minor!=0 || oclass->module->major!=0) kml_write(" (V%d.%02d)",oclass->module->major,oclass->module->minor); kml_write("</description>\n",oclass->module->name); openclass=oclass; } /* module overrides KML output */ mod = (MODULE*)(obj->oclass->module); if (mod->kmldump!=NULL) (*(mod->kmldump))(kml_write,obj); else if (has_location) { /* basic KML output of published variables */ PROPERTY *prop; kml_write(" <Placemark>\n"); if (obj->name) kml_write(" <name>%s</name>\n", obj->name); else kml_write(" <name>%s %d</name>\n", obj->oclass->name, obj->id); kml_write(" <description>\n"); kml_write(" <![CDATA[\n"); kml_write(" <TABLE><TR>\n"); for (prop=oclass->pmap;prop!=NULL && prop->oclass==oclass; prop=prop->next) { char *value = object_property_to_string(obj,prop->name, buffer, 1023); if (value!=NULL) kml_write("<TR><TH ALIGN=LEFT>%s</TH><TD ALIGN=RIGHT>%s</TD></TR>", prop->name, value); } kml_write(" </TR></TABLE>\n"); kml_write(" ]]>\n"); kml_write(" </description>\n"); kml_write(" <Point>\n"); kml_write(" <coordinates>%f,%f</coordinates>\n",obj->longitude,obj->latitude); kml_write(" </Point>\n"); kml_write(" </Placemark>\n"); } } /* close folder if any */ if (openclass!=NULL) { kml_write(" </Folder>\n"); openclass=NULL; } } kml_write(" </Document>\n"); return 0; }
STATUS original_test_start(int argc, char *argv[]) { OBJECT *obj[6]; MODULE *network; CLASS *node, *link; MODULE *tape; CLASS *player, *recorder, *collector; network = module_load("network",argc,argv); if (network==NULL) { #ifndef WIN32 fprintf(stderr,"%s\n",dlerror()); #else perror("network module load failed"); #endif return FAILED; } output_verbose("network module loaded ok"); node = class_get_class_from_classname("node"); if (node==NULL) { output_fatal("network module does not implement class node"); /* TROUBLESHOOT The <b>network</b> module test can't find the <b>node</b> class definition. This is probably caused by either an internal system error or a version of the network module that doesn't implement node object as expected (or at all). */ return FAILED; } output_verbose("class node implementation loaded ok"); link = class_get_class_from_classname("link"); if (node==NULL || link==NULL) { output_fatal("network module does not implement class link"); /* TROUBLESHOOT The <b>network</b> module test can't find the <b>link</b> class definition. This is probably caused by either an internal system error or a version of the network module that doesn't implement link object as expected (or at all). */ return FAILED; } output_verbose("class link implementation loaded ok"); tape = module_load("tape",argc,argv); if (tape==NULL) { #ifndef WIN32 fprintf(stderr,"%s\n",dlerror()); #else perror("tape module load failed"); #endif return FAILED; } player = class_get_class_from_classname("player"); if (player==NULL) { output_fatal("tape module does not implement class player"); /* TROUBLESHOOT The <b>tape</b> module test can't find the <b>player</b> class definition. This is probably caused by either an internal system error or a version of the tape module that doesn't implement player object as expected (or at all). */ return FAILED; } recorder = class_get_class_from_classname("recorder"); if (recorder==NULL) { output_fatal("tape module does not implement class recorder"); /* TROUBLESHOOT The <b>tape</b> module test can't find the <b>recorder</b> class definition. This is probably caused by either an internal system error or a version of the tape module that doesn't implement recorder object as expected (or at all). */ return FAILED; } collector = class_get_class_from_classname("collector"); if (collector==NULL) { output_fatal("tape module does not implement class collector"); /* TROUBLESHOOT The <b>tape</b> module test can't find the <b>collector</b> class definition. This is probably caused by either an internal system error or a version of the tape module that doesn't implement collector object as expected (or at all). */ return FAILED; } if (module_import(network,"../test/pnnl2bus.cdf")<=0) return FAILED; /* tape player */ if ((*player->create)(&obj[3],object_get_first())==FAILED) { output_fatal("player creation failed"); /* TROUBLESHOOT The <b>tape</b> module test can't create a <b>player</b> object. This is probably caused by either an internal system error or a version of the tape module that doesn't implement player object as expected (or at all). */ return FAILED; } object_set_value_by_name(obj[3],"loop","3600"); /* 18000 is about 12y at 1h steps */ object_set_value_by_name(obj[3],"property","S"); /* tape recorder */ if ((*recorder->create)(&obj[4],object_get_first())==FAILED) { output_fatal("recorder creation failed"); /* TROUBLESHOOT The <b>tape</b> module test can't create a <b>recorder</b> object. This is probably caused by either an internal system error or a version of the tape module that doesn't implement recorder object as expected (or at all). */ return FAILED; } object_set_value_by_name(obj[4],"property","V,S"); object_set_value_by_name(obj[4],"interval","0"); object_set_value_by_name(obj[4],"limit","1000"); /* tape collector */ if ((*collector->create)(&obj[5],NULL)==FAILED) { output_fatal("collector creation failed"); /* TROUBLESHOOT The <b>tape</b> module test can't create a <b>collector</b> object. This is probably caused by either an internal system error or a version of the tape module that doesn't implement collector object as expected (or at all). */ return FAILED; } object_set_value_by_name(obj[5],"property","count(V.mag),min(V.mag),avg(V.mag),std(V.mag),max(V.mag),min(V.ang),avg(V.ang),std(V.ang),max(V.ang)"); object_set_value_by_name(obj[5],"interval","0"); object_set_value_by_name(obj[5],"limit","1000"); object_set_value_by_name(obj[5],"group","class=node;"); module_check(network); return SUCCESS; }
int module_saveobj_xml(FILE *fp, MODULE *mod){ /**< the stream to write to */ unsigned count = 0; char buffer[1024]; PROPERTY *prop = NULL; OBJECT *obj; CLASS *oclass=NULL; CLASS *pclass = NULL; for(obj = object_get_first(); obj != NULL; obj = obj->next){ char32 oname = "(unidentified)"; if(obj->oclass->module != mod){ continue; } if(obj->name != NULL){ strcpy(oname, obj->name); } else { sprintf(oname, "%s:%i", obj->oclass->name, obj->id); } if ((oclass == NULL) || (obj->oclass != oclass)) oclass = obj->oclass; count += fprintf(fp,"\t\t<object type=\"%s\" id=\"%i\" name=\"%s\">\n", obj->oclass->name, obj->id, oname); /* dump internal properties */ if (obj->parent!=NULL){ if(obj->parent->name != NULL){ strcpy(oname, obj->parent->name); } else { sprintf(oname, "%s:%i", obj->parent->oclass->name, obj->parent->id); } count += fprintf(fp,"\t\t\t<parent>%s</parent>\n", oname); } else { count += fprintf(fp,"\t\t\t<parent>root</parent>\n"); } count += fprintf(fp,"\t\t\t<rank>%d</rank>\n", obj->rank); count += fprintf(fp,"\t\t\t<clock>\n", obj->clock); count += fprintf(fp,"\t\t\t\t <timestamp>%s</timestamp>\n", convert_from_timestamp(obj->clock,buffer,sizeof(buffer))>0?buffer:"(invalid)"); count += fprintf(fp,"\t\t\t</clock>\n"); /* why do latitude/longitude have 2 values? I currently only store as float in the schema... -dc */ if (!isnan(obj->latitude)) count += fprintf(fp,"\t\t\t<latitude>%s</latitude>\n", convert_from_latitude(obj->latitude,buffer,sizeof(buffer))?buffer:"(invalid)"); else count += fprintf(fp, "\t\t\t<latitude>NONE</latitude>\n"); if (!isnan(obj->longitude)) count += fprintf(fp,"\t\t\t<longitude>%s</longitude>\n",convert_from_longitude(obj->longitude,buffer,sizeof(buffer))?buffer:"(invalid)"); else count += fprintf(fp,"\t\t\t<longitude>NONE</longitude>\n"); /* dump properties */ for (prop=oclass->pmap;prop!=NULL && prop->oclass==oclass;prop=prop->next) { char *value = NULL; if((prop->access != PA_PUBLIC) && (prop->access != PA_REFERENCE)) continue; value = object_property_to_string(obj,prop->name); if (value!=NULL){ count += fprintf(fp, "\t\t\t<%s>%s</%s>\n", prop->name, value, prop->name); } } pclass = oclass->parent; while(pclass != NULL){ /* inherited properties */ for (prop=pclass->pmap;prop!=NULL && prop->oclass==pclass;prop=prop->next){ char *value = object_property_to_string(obj,prop->name); if (value!=NULL){ count += fprintf(fp, "\t\t\t<%s>%s</%s>\n", prop->name, value, prop->name); } } pclass = pclass->parent; } count += fprintf(fp,"\t\t</object>\n"); } return count; }
/* initialize modules */ int link_initall(void) { output_debug("link_initall(): link startup in progress..."); glxlink *mod; for ( mod=glxlink::get_first() ; mod!=NULL ; mod=mod->get_next() ) { LINKLIST *item; output_debug("link_initall(): setting up %s link", mod->get_target()); // set default global list (if needed) if ( mod->get_globals()==NULL ) { GLOBALVAR *var = NULL; while ( (var=global_getnext(var))!=NULL ) { if ( var->prop!=NULL && var->prop->name!=NULL ) { LINKLIST *item = mod->add_global(var->prop->name); if ( item!=NULL ) item->data = (void*)var; else output_error("link_initall(): unable to link %s", var->prop->name); } else output_warning("link_initall(): a variable property definition is null"); } } else { // link global variables for ( item=mod->get_globals() ; item!=NULL ; item=mod->get_next(item) ) { if ( strcmp(item->name,"")==0 ) continue; item->data = (void*)global_find(item->name); if ( item->data==NULL ) output_error("link_initall(target='%s'): global '%s' is not found", mod->get_target(), item->name); } } // link objects if ( mod->get_objects()==NULL ) { // set default object list OBJECT *obj = NULL; for ( obj=object_get_first() ; obj!=NULL ; obj=object_get_next(obj) ) { // add named objects LINKLIST *item = NULL; if ( obj->name!=NULL ) item = mod->add_object(obj->name); else { char id[256]; sprintf(id,"%s:%d",obj->oclass->name,obj->id); item = mod->add_object(id); } item->data = (void*)obj; } } else { LINKLIST *item; // link global variables for ( item=mod->get_objects() ; item!=NULL ; item=mod->get_next(item) ) { if ( strcmp(item->name,"")==0 ) continue; OBJECT *obj = NULL; item->data = (void*)object_find_name(item->name); if ( item->data==NULL) output_error("link_initall(target='%s'): object '%s' is not found", mod->get_target(), item->name); } } // link exports for ( item=mod->get_exports() ; item!=NULL ; item=mod->get_next(item) ) { char objname[64], propname[64], varname[64]; if ( sscanf(item->name,"%[^.].%s %s",objname,propname,varname)==3 ) { OBJECTPROPERTY *objprop = (OBJECTPROPERTY*)malloc(sizeof(OBJECTPROPERTY)); objprop->obj = object_find_name(objname); if ( objprop->obj ) { objprop->prop = class_find_property(objprop->obj->oclass,propname); if ( objprop->prop==NULL ) output_error("link_initall(target='%s'): export '%s' property not found", mod->get_target(), item->name); else { item->data = objprop; strcpy(item->name,varname); } } else output_error("link_initall(target='%s'): export '%s' object not found", mod->get_target(), item->name); } else output_error("link_initall(target='%s'): '%s' is not a valid export specification", mod->get_target(), item->name); } // link imports for ( item=mod->get_imports() ; item!=NULL ; item=mod->get_next(item) ) { char objname[64], propname[64], varname[64]; if ( sscanf(item->name,"%[^.].%s %s",objname,propname,varname)==3 ) { OBJECTPROPERTY *objprop = (OBJECTPROPERTY*)malloc(sizeof(OBJECTPROPERTY)); objprop->obj = object_find_name(objname); if ( objprop->obj ) { objprop->prop = class_find_property(objprop->obj->oclass,propname); if ( objprop->prop==NULL ) output_error("link_initall(target='%s'): import '%s' property not found", mod->get_target(), item->name); else { item->data = objprop; strcpy(item->name,varname); } } else output_error("link_initall(target='%s'): import '%s' object not found", mod->get_target(), item->name); } else output_error("link_initall(target='%s'): '%s' is not a valid import specification", mod->get_target(), item->name); } // initialize link module if ( !mod->do_init() ) { output_error("link_initall(): link startup failed"); link_termall(); return 0; } } output_debug("link_initall(): link startup done ok"); atexit((void(*)(void))link_termall); return 1; }
/** Initialize the delta mode code This call must be completed before the first call to any delta mode code. If the call fails, no delta mode code can be executed. Failure does not affect whether event mode code can run. @return SUCCESS or FAILED **/ STATUS delta_init(void) { OBJECT *obj, **pObj; char temp_name_buff[64]; unsigned int n, toprank = 0; OBJECT ***ranklist; int *rankcount; MODULE *module; clock_t t = clock(); /* count qualified modules */ for ( module=module_get_first() ; module!=NULL ; module=module_get_next(module) ) { if ( 0 != module->deltadesired ){ // this could probably be counted in module_init()... delta_modulecount++; if (profile.module_list[0]!=0) strcat(profile.module_list,","); strcat(profile.module_list,module->name); } } /* if none, stop here */ if ( delta_modulecount==0 ){ goto Success; } /* allocate memory for qualified module list */ delta_modulelist = (MODULE**)malloc(sizeof(MODULE**)*delta_modulecount); if(0 == delta_modulelist){ output_error("unable to allocate memory for deltamode module list"); /* TROUBLESHOOT Deltamode operation requires more memory than is available. Try freeing up memory by making more heap available or making the model smaller. */ return FAILED; } /* build qualified module list */ delta_modulecount = 0; global_deltamode_updateorder[0]='\0'; for ( module=module_get_first() ; module!=NULL ; module=module_get_next(module) ) { if ( 0 != module->deltadesired ) { if ( delta_modulecount>0 ) strcat(global_deltamode_updateorder,","); strcat(global_deltamode_updateorder,module->name); delta_modulelist[delta_modulecount++] = module; } } /* count qualified objects */ for ( obj=object_get_first() ; obj!=NULL ; obj=object_get_next(obj) ) { if ( obj->flags&OF_DELTAMODE ) { if ( !obj->oclass->update ) output_debug("object '%s' requested deltamode updates but the class '%s' does not export the update function", object_name(obj, temp_name_buff, 63), obj->oclass->name); delta_objectcount++; if ( obj->rank > toprank ){ toprank = obj->rank; } } } /* if none, stop here */ if ( delta_objectcount==0 ){ goto Success; } /* allocate final object list */ delta_objectlist = (OBJECT**)malloc(sizeof(OBJECT*)*delta_objectcount); if ( delta_objectlist==NULL) { output_error("unable to allocate memory for deltamode object list"); /* TROUBLESHOOT Deltamode operation requires more memory than is available. Try freeing up memory by making more heap available or making the model smaller. */ return FAILED; } /* allocate rank lists */ ranklist = (OBJECT***)malloc(sizeof(OBJECT**)*(toprank+1)); if ( 0 == ranklist ){ output_error("unable to allocate memory for deltamode ranklist"); /* TROUBLESHOOT Deltamode operation requires more memory than is available. Try freeing up memory by making more heap available or making the model smaller. */ return FAILED; } memset(ranklist,0,sizeof(OBJECT**)*(toprank+1)); /* allocate rank counts */ rankcount = (int*)malloc(sizeof(int)*(toprank+1)); if ( 0 == rankcount ){ output_error("unable to allocate memory for deltamode rankcount"); /* TROUBLESHOOT Deltamode operation requires more memory than is available. Try freeing up memory by making more heap available or making the model smaller. */ return FAILED; } memset(rankcount,0,sizeof(int)*(toprank+1)); /* count qualified objects in each rank */ for ( obj=object_get_first() ; obj!=NULL ; obj=object_get_next(obj) ) { if ( obj->flags&OF_DELTAMODE ){ rankcount[obj->rank]++; } } /* allocate rank lists */ for ( n=0 ; n<=toprank ; n++) { if ( rankcount[n]>0 ) { ranklist[n] = (OBJECT**)malloc(sizeof(OBJECT*)*rankcount[n]); if ( !ranklist[n] ){ output_error("unable to allocate memory for deltamode rankcount %i", n); /* TROUBLESHOOT Deltamode operation requires more memory than is available. Try freeing up memory by making more heap available or making the model smaller. */ return FAILED; } rankcount[n] = 0; /* clear for index recount */ } else ranklist[n] = NULL; } /* assign qualified objects to rank lists */ for ( obj=object_get_first() ; obj!=NULL ; obj=object_get_next(obj) ) { if ( obj->flags&OF_DELTAMODE ){ ranklist[obj->rank][rankcount[obj->rank]++] = obj; } } /* build final object list */ pObj = delta_objectlist; for ( n=0 ; n<=toprank ; n++) { int m; for ( m=0 ; m<rankcount[n] ; m++ ){ *pObj++ = ranklist[n][m]; } if ( ranklist[n]!=NULL ){ free(ranklist[n]); ranklist[n] = NULL; } } /* release memory */ free(rankcount); rankcount = NULL; free(ranklist); ranklist = NULL; Success: profile.t_init += clock() - t; return SUCCESS; }