int netsize() /* **-------------------------------------------------------------- ** Input: none ** Output: returns error code ** Purpose: determines number of system components **-------------------------------------------------------------- */ { char line[MAXLINE+1]; /* Line from input data file */ char *tok; /* First token of line */ int sect,newsect; /* Input data sections */ int errcode = 0; /* Error code */ /* Initialize network component counts */ MaxJuncs = 0; MaxTanks = 0; MaxPipes = 0; MaxPumps = 0; MaxValves = 0; MaxControls = 0; MaxRules = 0; MaxCurves = 0; sect = -1; /* Add a default pattern 0 */ MaxPats = -1; addpattern(""); /* Make pass through data file counting number of each component */ while (fgets(line,MAXLINE,InFile) != NULL) { /* Skip blank lines & those beginning with a comment */ tok = strtok(line,SEPSTR); if (tok == NULL) continue; if (*tok == ';') continue; /* Check if line begins with a new section heading */ if (*tok == '[') { newsect = findmatch(tok,SectTxt); if (newsect >= 0) { sect = newsect; if (sect == _END) break; continue; } else continue; } /* Add to count of current component */ switch(sect) { case _JUNCTIONS: MaxJuncs++; break; case _RESERVOIRS: case _TANKS: MaxTanks++; break; case _PIPES: MaxPipes++; break; case _PUMPS: MaxPumps++; break; case _VALVES: MaxValves++; break; case _CONTROLS: MaxControls++; break; case _RULES: addrule(tok); break; /* See RULES.C */ case _PATTERNS: errcode = addpattern(tok); break; case _CURVES: errcode = addcurve(tok); break; } if (errcode) break; } MaxNodes = MaxJuncs + MaxTanks; MaxLinks = MaxPipes + MaxPumps + MaxValves; if (MaxPats < 1) MaxPats = 1; if (!errcode) { if (MaxJuncs < 1) errcode = 223; /* Not enough nodes */ else if (MaxTanks == 0) errcode = 224; /* No tanks */ } return(errcode); } /* End of netsize */
static Gdinfo *ReadGridFile(FileList *f) { register int i,j,k; char buf[BUFSIZ]; int nel,npt,nbnd,vid,vid1,eid,eid1,**bndpts,trip; Gdinfo *ginfo = (Gdinfo *)malloc(sizeof(Gdinfo)); Cinfo *con,*c,*c1; FILE *fp = f->in.fp; /* read boundary condition file */ if(f->mesh.name) ReadBcond(f->mesh.fp,ginfo); fgets(buf,BUFSIZ,fp); fgets(buf,BUFSIZ,fp); sscanf(buf,"%d%d%d%*d",&nel,&npt,&nbnd); ginfo->nel = nel; ginfo->npt = npt; ginfo->nbnd = nbnd; ginfo->elmtpts = imatrix(0,nel-1,0,2); ginfo->x = dvector(0,npt-1); ginfo->y = dvector(0,npt-1); bndpts = imatrix(0,nel-1,0,2); ginfo->elmtcon = (Cinfo**)malloc(nel*sizeof(Cinfo *)); ginfo->elmtcon[0] = (Cinfo *)calloc(3*nel,sizeof(Cinfo) ); for(k = 1; k < nel; ++k) ginfo->elmtcon[k] = ginfo->elmtcon[k-1]+3; con = (Cinfo *)calloc(npt,sizeof(Cinfo)); /* read element vertex co-ordinate indices */ fgets(buf,BUFSIZ,fp); for(k = 0; k < nel; ++k) { fgets (buf,BUFSIZ,fp); sscanf(buf,"%*d%d%d%d",ginfo->elmtpts[k], ginfo->elmtpts[k]+1,ginfo->elmtpts[k]+2); } /* read co-ordinates */ fgets(buf,BUFSIZ,fp); for(k = 0; k < npt; ++k) { fgets (buf,BUFSIZ,fp); sscanf(buf,"%*d%lf%lf",ginfo->x+k,ginfo->y+k); } /* read boundary info */ fgets(buf,BUFSIZ,fp); for(k = 0; k < nbnd; ++k) { fgets (buf,BUFSIZ,fp); sscanf(buf,"%d%d%*d%d",bndpts[k],bndpts[k]+1,bndpts[k]+2); } /* sort out connectivity */ /* get list of all elements at a vertex */ for(k = 0; k < nel; ++k) for(i = 0; i < 3; ++i) addcon(con+ginfo->elmtpts[k][i]-1,k+1,i); /* search through element list and fill out element connectivity */ for(i = 0; i < npt; ++i) { for(c = con[i].next; c; c = c->next) { vid = (c->vsid+1)%3; eid = c->elmtid; for(c1 = con[i].next; c1; c1 = c1->next) { vid1 = (c1->vsid+2)%3; eid1 = c1->elmtid; if(ginfo->elmtpts[eid-1][vid] == ginfo->elmtpts[eid1-1][vid1]) { ginfo->elmtcon[eid -1][c->vsid].type = 'E'; ginfo->elmtcon[eid -1][c->vsid].elmtid = eid1; ginfo->elmtcon[eid -1][c->vsid].vsid = vid1; ginfo->elmtcon[eid1-1][vid1 ].type = 'E'; ginfo->elmtcon[eid1-1][vid1 ].elmtid = eid; ginfo->elmtcon[eid1-1][vid1 ].vsid = c->vsid; } } } } /* check through list and match any missing element with boundary values */ for(k = 0; k < nel; ++k) for(i = 0; i < 3; ++i) if(!ginfo->elmtcon[k][i].elmtid) { vid = ginfo->elmtpts[k][i]; vid1 = ginfo->elmtpts[k][(i+1)%3]; /* check to see that this side is a boundary */ for(j = 0,trip=1; j < nbnd; ++j) if(vid == bndpts[j][0] && vid1 == bndpts[j][1]) { /* set boundary condition from given values if present */ if(f->mesh.name) { register int i1; Bndinfo *b; Curinfo *c; for(b=ginfo->bnd; b; b = b->next) if(b->region == bndpts[j][2]) { ginfo->elmtcon[k][i].type = b->type; switch(b->type) { case 'W': case 'O': case 'S': case 'B': case 'M': case 'I': case 'Z': break; case 'V': case 'F': dcopy(b->nbcs,b->data.val,1,ginfo->elmtcon[k][i].f,1); break; case 'v': case 'f': case 'm': for(i1=0; i1 < b->nbcs; ++i1) ginfo->elmtcon[k][i].str[i1] = b->data.str[i1]; break; } } for(c=ginfo->curve; c; c = c->next) { if(c->region == bndpts[j][2]) { /* reset co-ordinates */ switch(c->type) { case 'C': /* arc */ { double theta; theta = atan2(ginfo->y[vid-1]-c->info.arc.yc, ginfo->x[vid-1]-c->info.arc.xc); ginfo->x[vid-1] = fabs(c->info.arc.radius)*cos(theta) + c->info.arc.xc; ginfo->y[vid-1] = fabs(c->info.arc.radius)*sin(theta) + c->info.arc.yc; theta = atan2(ginfo->y[vid1-1]-c->info.arc.yc, ginfo->x[vid1-1]-c->info.arc.xc); ginfo->x[vid1-1] = fabs(c->info.arc.radius)*cos(theta) + c->info.arc.xc; ginfo->y[vid1-1] = fabs(c->info.arc.radius)*sin(theta) + c->info.arc.yc; break; } } /* add curved information about element to ginfo */ addcurve(ginfo,k+1,i,c->idtype); } } } else ginfo->elmtcon[k][i].type = 'W'; trip=0; } if(trip) fprintf(stderr,"side %d of element %d using coordinate indices" " (%d,%d) is not a boundary\n",i+1,k+1,vid,vid1); } /* free the con list */ for(k = 0; k < npt; ++k) { c = con[k].next; while(c) { c1 = c->next; free(c); c = c1; } } free(con); free_imatrix(bndpts,0,0); return ginfo; }