int savefinaloutput() /* **-------------------------------------------------------------- ** Input: none ** Output: returns error code ** Purpose: saves time series statistics, reaction rates & ** epilog to output file. **-------------------------------------------------------------- */ { int errcode = 0; REAL4 *x; /* Save time series statistic if computed */ if (Tstatflag != SERIES && TmpOutFile != NULL) { x = (REAL4 *) calloc(MAX(Nnodes,Nlinks) + 1, sizeof(REAL4)); if ( x == NULL ) return 101; ERRCODE(savetimestat(x,NODEHDR)); ERRCODE(savetimestat(x,LINKHDR)); if (!errcode) Nperiods = 1; fclose(TmpOutFile); free(x); } /* Save avg. reaction rates & file epilog */ if (OutFile != NULL) { ERRCODE(savenetreacts(Wbulk,Wwall,Wtank,Wsource)); ERRCODE(saveepilog()); } return(errcode); }
static int scsi_cmd_run(struct udev *udev, struct scsi_cmd *cmd, int fd, unsigned char *buf, size_t bufsize) { int ret = 0; if (bufsize > 0) { cmd->sg_io.dxferp = buf; cmd->sg_io.dxfer_len = bufsize; cmd->sg_io.dxfer_direction = SG_DXFER_FROM_DEV; } else { cmd->sg_io.dxfer_direction = SG_DXFER_NONE; } if (ioctl(fd, SG_IO, &cmd->sg_io)) return -1; if ((cmd->sg_io.info & SG_INFO_OK_MASK) != SG_INFO_OK) { errno = EIO; ret = -1; if (cmd->sg_io.masked_status & CHECK_CONDITION) { ret = ERRCODE(cmd->_sense.u); if (ret == 0) ret = -1; } } return ret; }
int storesparse(int n) /* **-------------------------------------------------------------- ** Input: n = number of rows in solution matrix ** Output: returns error code ** Purpose: stores row indexes of non-zeros of each column of ** lower triangular portion of factorized matrix **-------------------------------------------------------------- */ { Padjlist alink; int i, ii, j, k, l, m; int errcode = 0; /* Allocate sparse matrix storage */ XLNZ = (int *) calloc(n+2, sizeof(int)); NZSUB = (int *) calloc(Ncoeffs+2, sizeof(int)); LNZ = (int *) calloc(Ncoeffs+2, sizeof(int)); ERRCODE(MEMCHECK(XLNZ)); ERRCODE(MEMCHECK(NZSUB)); ERRCODE(MEMCHECK(LNZ)); if (errcode) return(errcode); /* Generate row index pointers for each column of matrix */ k = 0; XLNZ[1] = 1; for (i=1; i<=n; i++) /* column */ { m = 0; ii = Order[i]; for (alink = Adjlist[ii]; alink != NULL; alink = alink->next) { j = Row[alink->node]; /* row */ l = alink->link; if (j > i && j <= n) { m++; k++; NZSUB[k] = j; LNZ[k] = l; } } XLNZ[i+1] = XLNZ[i] + m; } return(errcode); } /* End of storesparse */
int ordersparse(int n) /* **-------------------------------------------------------------- ** Input: n = number of rows in solution matrix ** Output: returns eror code ** Purpose: puts row indexes in ascending order in NZSUB **-------------------------------------------------------------- */ { int i, k; int *xlnzt, *nzsubt, *lnzt, *nzt; int errcode = 0; xlnzt = (int *) calloc(n+2, sizeof(int)); nzsubt = (int *) calloc(Ncoeffs+2, sizeof(int)); lnzt = (int *) calloc(Ncoeffs+2, sizeof(int)); nzt = (int *) calloc(n+2, sizeof(int)); ERRCODE(MEMCHECK(xlnzt)); ERRCODE(MEMCHECK(nzsubt)); ERRCODE(MEMCHECK(lnzt)); ERRCODE(MEMCHECK(nzt)); if (!errcode) { /* Count # non-zeros in each row */ for (i=1; i<=n; i++) nzt[i] = 0; for (i=1; i<=n; i++) { for (k=XLNZ[i]; k<XLNZ[i+1]; k++) nzt[NZSUB[k]]++; } xlnzt[1] = 1; for (i=1; i<=n; i++) xlnzt[i+1] = xlnzt[i] + nzt[i]; /* Transpose matrix twice to order column indexes */ transpose(n,XLNZ,NZSUB,LNZ,xlnzt,nzsubt,lnzt,nzt); transpose(n,xlnzt,nzsubt,lnzt,XLNZ,NZSUB,LNZ,nzt); } /* Reclaim memory */ free(xlnzt); free(nzsubt); free(lnzt); free(nzt); return(errcode); } /* End of ordersparse */
int saveoutput() /* **-------------------------------------------------------------- ** Input: none ** Output: returns error code ** Purpose: writes simulation results to output file **-------------------------------------------------------------- */ { int j; int errcode = 0; REAL4 *x = (REAL4 *) calloc(MAX(Nnodes,Nlinks) + 1, sizeof(REAL4)); if ( x == NULL ) return 101; /* Write out node results, then link results */ for (j=DEMAND; j<=QUALITY; j++) ERRCODE(nodeoutput(j,x,Ucf[j])); for (j=FLOW; j<=FRICTION; j++) ERRCODE(linkoutput(j,x,Ucf[j])); free(x); return(errcode); } /* End of saveoutput */
int allocsparse() /* **-------------------------------------------------------------- ** Input: none ** Output: returns error code ** Purpose: allocates memory for indexing the solution matrix **-------------------------------------------------------------- */ { int errcode = 0; Adjlist = (Padjlist *) calloc(Nnodes+1, sizeof(Padjlist)); Order = (int *) calloc(Nnodes+1, sizeof(int)); Row = (int *) calloc(Nnodes+1, sizeof(int)); Ndx = (int *) calloc(Nlinks+1, sizeof(int)); ERRCODE(MEMCHECK(Adjlist)); ERRCODE(MEMCHECK(Order)); ERRCODE(MEMCHECK(Row)); ERRCODE(MEMCHECK(Ndx)); return(errcode); }
int getdata() /* **---------------------------------------------------------------- ** Input: none ** Output: returns error code ** Purpose: reads in network data from disk file **---------------------------------------------------------------- */ { int errcode = 0; setdefaults(); /* Assign default data values */ initreport(); /* Initialize reporting options */ rewind(InFile); /* Rewind input file */ ERRCODE(readdata()); /* Read in network data */ if (!errcode) adjustdata(); /* Adjust data for default values */ if (!errcode) initunits(); /* Initialize units on input data */ ERRCODE(inittanks()); /* Initialize tank volumes */ if (!errcode) convertunits(); /* Convert units on input data */ return(errcode); } /* End of getdata */
/*! Run a SCSI MMC command. p_user_data internal CD structure. i_timeout_ms time in milliseconds we will wait for the command to complete. If this value is -1, use the default time-out value. i_cdb Size of p_cdb p_cdb CDB bytes. e_direction direction the transfer is to go. i_buf Size of buffer p_buf Buffer for data, both sending and receiving Return 0 if no error. */ int run_mmc_cmd_freebsd_cam( const void *p_user_data, unsigned int i_timeout_ms, unsigned int i_cdb, const mmc_cdb_t *p_cdb, cdio_mmc_direction_t e_direction, unsigned int i_buf, /*in/out*/ void *p_buf ) { const _img_private_t *p_env = p_user_data; int i_status; int direction = CAM_DEV_QFRZDIS; union ccb ccb; if (!p_env || !p_env->cam) return -2; memset(&ccb, 0, sizeof(ccb)); ccb.ccb_h.path_id = p_env->cam->path_id; ccb.ccb_h.target_id = p_env->cam->target_id; ccb.ccb_h.target_lun = p_env->cam->target_lun; ccb.ccb_h.timeout = i_timeout_ms; if (!i_buf) direction |= CAM_DIR_NONE; else direction |= (e_direction == SCSI_MMC_DATA_READ)?CAM_DIR_IN : CAM_DIR_OUT; memcpy(ccb.csio.cdb_io.cdb_bytes, p_cdb->field, i_cdb); ccb.csio.cdb_len = mmc_get_cmd_len(ccb.csio.cdb_io.cdb_bytes[0]); cam_fill_csio (&(ccb.csio), 1, NULL, direction | CAM_DEV_QFRZDIS, MSG_SIMPLE_Q_TAG, p_buf, i_buf, sizeof(ccb.csio.sense_data), ccb.csio.cdb_len, 30*1000); if (cam_send_ccb(p_env->cam, &ccb) < 0) { cdio_warn ("transport failed: %s", strerror(errno)); return -1; } if ((ccb.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { return 0; } errno = EIO; i_status = ERRCODE(((unsigned char *)&ccb.csio.sense_data)); if (i_status == 0) i_status = -1; else CREAM_ON_ERRNO(((unsigned char *)&ccb.csio.sense_data)); cdio_warn ("transport failed: %d", i_status); return i_status; }
int createsparse() /* **-------------------------------------------------------------- ** Input: none ** Output: returns error code ** Purpose: creates sparse representation of coeff. matrix **-------------------------------------------------------------- */ { int errcode = 0; /* Allocate data structures */ ERRCODE(allocsparse()); if (errcode) return(errcode); /* Build node-link adjacency lists with parallel links removed. */ Degree = (int *) calloc(Nnodes+1, sizeof(int)); ERRCODE(MEMCHECK(Degree)); ERRCODE(buildlists(TRUE)); if (!errcode) { xparalinks(); /* Remove parallel links */ countdegree(); /* Find degree of each junction */ } /* (= # of adjacent links) */ /* Re-order nodes to minimize number of non-zero coeffs. */ /* in factorized solution matrix. At same time, adjacency */ /* list is updated with links representing non-zero coeffs. */ Ncoeffs = Nlinks; ERRCODE(reordernodes()); /* Allocate memory for sparse storage of positions of non-zero */ /* coeffs. and store these positions in vector NZSUB. */ ERRCODE(storesparse(Njuncs)); /* Free memory used for adjacency lists and sort */ /* row indexes in NZSUB to optimize linsolve(). */ if (!errcode) freelists(); ERRCODE(ordersparse(Njuncs)); /* Re-build adjacency lists without removing parallel */ /* links for use in future connectivity checking. */ ERRCODE(buildlists(FALSE)); /* Free allocated memory */ free(Degree); return(errcode); } /* End of createsparse */
int unlinked() /* **-------------------------------------------------------------- ** Input: none ** Output: returns error code if any unlinked junctions found ** Purpose: checks for unlinked junctions in network ** ** NOTE: unlinked tanks have no effect on computations. **-------------------------------------------------------------- */ { char *marked; int i,err, errcode; errcode = 0; err = 0; marked = (char *) calloc(Nnodes+1,sizeof(char)); ERRCODE(MEMCHECK(marked)); if (!errcode) { memset(marked,0,(Nnodes+1)*sizeof(char)); for (i=1; i<=Nlinks; i++) /* Mark end nodes of each link */ { marked[Link[i].N1]++; marked[Link[i].N2]++; } for (i=1; i<=Njuncs; i++) /* Check each junction */ { if (marked[i] == 0) /* If not marked then error */ { err++; sprintf(Msg,ERR233,Node[i].ID); writeline(Msg); } if (err >= MAXERRS) break; } if (err > 0) errcode = 200; } free(marked); return(errcode); } /* End of unlinked */
int openqual() /* **-------------------------------------------------------------- ** Input: none ** Output: returns error code ** Purpose: opens WQ solver system **-------------------------------------------------------------- */ { int errcode = 0; int n; /* Allocate memory pool for WQ segments */ OutOfMemory = FALSE; if (AllocInit() == NULL) errcode = 101; /* Allocate scratch array & reaction rate array*/ X = (float *) calloc(MAX((Nnodes+1),(Nlinks+1)),sizeof(float)); R = (float *) calloc((Nlinks+1), sizeof(float)); ERRCODE(MEMCHECK(X)); ERRCODE(MEMCHECK(R)); /* Allocate memory for WQ solver */ n = Nlinks+Ntanks+1; FirstSeg = (Pseg *) calloc(n, sizeof(Pseg)); LastSeg = (Pseg *) calloc(n, sizeof(Pseg)); FlowDir = (char *) calloc(n, sizeof(char)); n = Nnodes+1; VolIn = (float *) calloc(n, sizeof(float)); MassIn = (float *) calloc(n, sizeof(float)); ERRCODE(MEMCHECK(FirstSeg)); ERRCODE(MEMCHECK(LastSeg)); ERRCODE(MEMCHECK(FlowDir)); ERRCODE(MEMCHECK(VolIn)); ERRCODE(MEMCHECK(MassIn)); return(errcode); }
int disconnected() /* **------------------------------------------------------------------- ** Input: None ** Output: Returns number of disconnected nodes ** Purpose: Tests current hydraulic solution to see if any closed ** links have caused the network to become disconnected. **------------------------------------------------------------------- */ { int i, j; int count, mcount; int errcode = 0; int *nodelist; char *marked; /* Allocate memory for node list & marked list */ nodelist = (int *) calloc(Nnodes+1,sizeof(int)); marked = (char *) calloc(Nnodes+1,sizeof(char)); ERRCODE(MEMCHECK(nodelist)); ERRCODE(MEMCHECK(marked)); if (errcode) return(0); /* Place tanks on node list and marked list */ for (i=1; i<=Ntanks; i++) { j = Njuncs + i; nodelist[i] = j; marked[j] = 1; } /* Place junctions with negative demands on the lists */ mcount = Ntanks; for (i=1; i<=Njuncs; i++) { if (D[i] < 0.0) { mcount++; nodelist[mcount] = i; marked[i] = 1; } } /* Mark all nodes that can be connected to tanks */ /* and count number of nodes remaining unmarked. */ marknodes(mcount,nodelist,marked); j = 0; count = 0; for (i=1; i<=Njuncs; i++) { if (!marked[i] && D[i] != 0.0) /* Skip if no demand */ { count++; if (count <= MAXCOUNT && Messageflag) { sprintf(Msg,WARN03a,Node[i].ID,clocktime(Atime,Htime)); writeline(Msg); } j = i; /* Last unmarked node */ } } /* Report number of unmarked nodes and find closed link */ /* on path from node j back to a tank. */ if (count > 0 && Messageflag) { if (count > MAXCOUNT) { sprintf(Msg, WARN03b, count-MAXCOUNT, clocktime(Atime,Htime)); writeline(Msg); } getclosedlink(j,marked); } /* Free allocated memory */ free(nodelist); free(marked); return(count); } /* End of disconnected() */
int writeresults() /* **-------------------------------------------------------------- ** Input: none ** Output: returns error code ** Purpose: writes simulation results to report file **-------------------------------------------------------------- */ { Pfloat *x; /* Array of pointers to floats */ int j,m,n,np,nnv,nlv; int errcode = 0; /* **----------------------------------------------------------- ** NOTE: The OutFile contains results for 4 node variables ** (demand, head, pressure, & quality) and 8 link ** variables (flow, velocity, headloss, quality, ** status, setting, reaction rate & friction factor) ** at each reporting time. **----------------------------------------------------------- */ /* Return if no output file */ if (OutFile == NULL) return(106); /* Return if no nodes or links selected for reporting */ /* or if no node or link report variables enabled. */ if (!Nodeflag && !Linkflag) return(errcode); nnv = 0; for (j=ELEV; j<=QUALITY; j++) nnv += Field[j].Enabled; nlv = 0; for (j=LENGTH; j<=FRICTION; j++) nlv += Field[j].Enabled; if (nnv == 0 && nlv == 0) return(errcode); /* Allocate memory for output variables. */ /* m = larger of # node variables & # link variables */ /* n = larger of # nodes & # links */ m = MAX( (QUALITY-DEMAND+1), (FRICTION-FLOW+1) ); n = MAX( (Nnodes+1), (Nlinks+1)); x = (Pfloat *) calloc(m, sizeof(Pfloat)); ERRCODE( MEMCHECK(x) ); if (errcode) return(errcode); for (j=0; j<m; j++) { x[j] = (float *) calloc(n, sizeof(float)); ERRCODE( MEMCHECK(x[j]) ); } if (errcode) return(errcode); /* Re-position output file & initialize report time. */ fseek(OutFile,OutOffset2,SEEK_SET); Htime = Rstart; /* For each reporting time: */ for (np=1; np<=Nperiods; np++) { /* Read in node results & write node table. */ /* (Remember to offset x[j] by 1 because array is zero-based). */ for (j=DEMAND; j<=QUALITY; j++) fread((x[j-DEMAND])+1,sizeof(float),Nnodes,OutFile); if (nnv > 0 && Nodeflag > 0) writenodetable(x); /* Read in link results & write link table. */ for (j=FLOW; j<=FRICTION; j++) fread((x[j-FLOW])+1,sizeof(float),Nlinks,OutFile); if (nlv > 0 && Linkflag > 0) writelinktable(x); Htime += Rstep; } /* Free allocated memory */ for (j=0; j<m; j++) free(x[j]); free(x); return(errcode); } /* End of writereport */
int readdata() /* **-------------------------------------------------------------- ** Input: none ** Output: returns error code ** Purpose: reads contents of input data file **-------------------------------------------------------------- */ { char line[MAXLINE+1], /* Line from input data file */ wline[MAXLINE+1]; /* Working copy of input line */ int sect,newsect, /* Data sections */ errcode = 0, /* Error code */ inperr,errsum; /* Error code & total error count */ /* Allocate input buffer */ X = (double *) calloc(MAXTOKS, sizeof(double)); ERRCODE(MEMCHECK(X)); if (!errcode) { /* Initialize number of network components */ Ntitle = 0; Nnodes = 0; Njuncs = 0; Ntanks = 0; Nlinks = 0; Npipes = 0; Npumps = 0; Nvalves = 0; Ncontrols = 0; Nrules = 0; Ncurves = MaxCurves; Npats = MaxPats; PrevPat = NULL; PrevCurve = NULL; sect = -1; errsum = 0; /* Read each line from input file. */ while (fgets(line,MAXLINE,InFile) != NULL) { /* Make copy of line and scan for tokens */ strcpy(wline,line); Ntokens = gettokens(wline); /* Skip blank lines and comments */ if (Ntokens == 0) continue; if (*Tok[0] == ';') continue; /* Check if max. length exceeded */ if (strlen(line) >= MAXLINE) { sprintf(Msg,ERR214); writeline(Msg); writeline(line); errsum++; } /* Check if at start of a new input section */ if (*Tok[0] == '[') { newsect = findmatch(Tok[0],SectTxt); if (newsect >= 0) { sect = newsect; if (sect == _END) break; continue; } else { inperrmsg(201,sect,line); errsum++; break; } } /* Otherwise process next line of input in current section */ else { inperr = newline(sect,line); if (inperr > 0) { inperrmsg(inperr,sect,line); errsum++; } } /* Stop if reach end of file or max. error count */ if (errsum == MAXERRS) break; } /* End of while */ /* Check for errors */ if (errsum > 0) errcode = 200; } /* Check for unlinked nodes */ if (!errcode) errcode = unlinked(); /* Get pattern & curve data from temp. lists */ if (!errcode) errcode = getpatterns(); if (!errcode) errcode = getcurves(); if (!errcode) errcode = getpumpparams(); /* Free input buffer */ free(X); return(errcode); } /* End of readdata */
int linsolve(int n, double *Aii, double *Aij, double *B) /* **-------------------------------------------------------------- ** Input: n = number of equations ** Aii = diagonal entries of solution matrix ** Aij = non-zero off-diagonal entries of matrix ** B = right hand side coeffs. ** Output: B = solution values ** returns 0 if solution found, or index of ** equation causing system to be ill-conditioned ** Purpose: solves sparse symmetric system of linear ** equations using Cholesky factorization ** ** NOTE: This procedure assumes that the solution matrix has ** been symbolically factorized with the positions of ** the lower triangular, off-diagonal, non-zero coeffs. ** stored in the following integer arrays: ** XLNZ (start position of each column in NZSUB) ** NZSUB (row index of each non-zero in each column) ** LNZ (position of each NZSUB entry in Aij array) ** ** This procedure has been adapted from subroutines GSFCT and ** GSSLV in the book "Computer Solution of Large Sparse ** Positive Definite Systems" by A. George and J. W-H Liu ** (Prentice-Hall, 1981). **-------------------------------------------------------------- */ { int *link, *first; int i, istop, istrt, isub, j, k, kfirst, newk; int errcode = 0; double bj, diagj, ljk; double *temp; temp = (double *) calloc(n+1, sizeof(double)); link = (int *) calloc(n+1,sizeof(int)); first = (int *) calloc(n+1,sizeof(int)); ERRCODE(MEMCHECK(temp)); ERRCODE(MEMCHECK(link)); ERRCODE(MEMCHECK(first)); if (errcode) { errcode = -errcode; goto ENDLINSOLVE; } memset(temp,0,(n+1)*sizeof(double)); memset(link,0,(n+1)*sizeof(int)); /* Begin numerical factorization of matrix A into L */ /* Compute column L(*,j) for j = 1,...n */ for (j=1; j<=n; j++) { /* For each column L(*,k) that affects L(*,j): */ diagj = 0.0; newk = link[j]; k = newk; while (k != 0) { /* Outer product modification of L(*,j) by */ /* L(*,k) starting at first[k] of L(*,k). */ newk = link[k]; kfirst = first[k]; ljk = Aij[LNZ[kfirst]]; diagj += ljk*ljk; istrt = kfirst + 1; istop = XLNZ[k+1] - 1; if (istop >= istrt) { /* Before modification, update vectors 'first' */ /* and 'link' for future modification steps. */ first[k] = istrt; isub = NZSUB[istrt]; link[k] = link[isub]; link[isub] = k; /* The actual mod is saved in vector 'temp'. */ for (i=istrt; i<=istop; i++) { isub = NZSUB[i]; temp[isub] += Aij[LNZ[i]]*ljk; } } k = newk; } /* Apply the modifications accumulated */ /* in 'temp' to column L(*,j). */ diagj = Aii[j] - diagj; if (diagj <= 0.0) /* Check for ill-conditioning */ { errcode = j; goto ENDLINSOLVE; } diagj = sqrt(diagj); Aii[j] = diagj; istrt = XLNZ[j]; istop = XLNZ[j+1] - 1; if (istop >= istrt) { first[j] = istrt; isub = NZSUB[istrt]; link[j] = link[isub]; link[isub] = j; for (i=istrt; i<=istop; i++) { isub = NZSUB[i]; bj = (Aij[LNZ[i]] - temp[isub])/diagj; Aij[LNZ[i]] = bj; temp[isub] = 0.0; } } } /* next j */ /* Foward substitution */ for (j=1; j<=n; j++) { bj = B[j]/Aii[j]; B[j] = bj; istrt = XLNZ[j]; istop = XLNZ[j+1] - 1; if (istop >= istrt) { for (i=istrt; i<=istop; i++) { isub = NZSUB[i]; B[isub] -= Aij[LNZ[i]]*bj; } } } /* Backward substitution */ for (j=n; j>=1; j--) { bj = B[j]; istrt = XLNZ[j]; istop = XLNZ[j+1] - 1; if (istop >= istrt) { for (i=istrt; i<=istop; i++) { isub = NZSUB[i]; bj -= Aij[LNZ[i]]*B[isub]; } } B[j] = bj/Aii[j]; } ENDLINSOLVE: free(temp); free(link); free(first); return(errcode); } /* End of linsolve */
/* C_ERRCODE -- Get the error code of the most recent error. */ int c_errcode ( void ) { return (ERRCODE()); }
int savenetdata() /* **--------------------------------------------------------------- ** Input: none ** Output: returns error code ** Purpose: saves input data in original units to binary ** output file using fixed-sized (4-byte) records **--------------------------------------------------------------- */ { int i,nmax; INT4 *ibuf; REAL4 *x; int errcode = 0; /* Allocate buffer arrays */ nmax = MAX(Nnodes,Nlinks) + 1; nmax = MAX(nmax,15); ibuf = (INT4 *) calloc(nmax, sizeof(INT4)); x = (REAL4 *) calloc(nmax, sizeof(REAL4)); ERRCODE(MEMCHECK(ibuf)); ERRCODE(MEMCHECK(x)); if (!errcode) { /* Write integer variables to OutFile */ ibuf[0] = MAGICNUMBER; /*** CODEVERSION replaces VERSION ***/ //(2.00.11 - LR) ibuf[1] = CODEVERSION; //(2.00.11 - LR) ibuf[2] = Nnodes; ibuf[3] = Ntanks; ibuf[4] = Nlinks; ibuf[5] = Npumps; ibuf[6] = Nvalves; ibuf[7] = Qualflag; ibuf[8] = TraceNode; ibuf[9] = Flowflag; ibuf[10] = Pressflag; ibuf[11] = Tstatflag; ibuf[12] = Rstart; ibuf[13] = Rstep; ibuf[14] = Dur; fwrite(ibuf,sizeof(INT4),15,OutFile); /* Write string variables to OutFile */ fwrite(Title[0],sizeof(char),MAXMSG+1,OutFile); fwrite(Title[1],sizeof(char),MAXMSG+1,OutFile); fwrite(Title[2],sizeof(char),MAXMSG+1,OutFile); fwrite(InpFname,sizeof(char),MAXFNAME+1,OutFile); fwrite(Rpt2Fname,sizeof(char),MAXFNAME+1,OutFile); fwrite(ChemName,sizeof(char),MAXID+1,OutFile); fwrite(Field[QUALITY].Units,sizeof(char),MAXID+1,OutFile); /* Write node ID information to OutFile */ for (i=1; i<=Nnodes; i++) fwrite(Node[i].ID, MAXID+1, 1, OutFile); /* Write link information to OutFile */ /* (Note: first transfer values to buffer array,*/ /* then fwrite buffer array at offset of 1 ) */ for (i=1; i<=Nlinks; i++) fwrite(Link[i].ID, MAXID+1, 1, OutFile); for (i=1; i<=Nlinks; i++) ibuf[i] = Link[i].N1; fwrite(ibuf+1,sizeof(INT4),Nlinks,OutFile); for (i=1; i<=Nlinks; i++) ibuf[i] = Link[i].N2; fwrite(ibuf+1,sizeof(INT4),Nlinks,OutFile); for (i=1; i<=Nlinks; i++) ibuf[i] = Link[i].Type; fwrite(ibuf+1,sizeof(INT4),Nlinks,OutFile); /* Write tank information to OutFile.*/ for (i=1; i<=Ntanks; i++) ibuf[i] = Tank[i].Node; fwrite(ibuf+1,sizeof(INT4),Ntanks,OutFile); for (i=1; i<=Ntanks; i++) x[i] = (REAL4)Tank[i].A; FSAVE(Ntanks); /* Save node elevations to OutFile.*/ for (i=1; i<=Nnodes; i++) x[i] = (REAL4)(Node[i].El*Ucf[ELEV]); FSAVE(Nnodes); /* Save link lengths & diameters to OutFile.*/ for (i=1; i<=Nlinks; i++) x[i] = (REAL4)(Link[i].Len*Ucf[ELEV]); FSAVE(Nlinks); for (i=1; i<=Nlinks; i++) { if (Link[i].Type != PUMP) x[i] = (REAL4)(Link[i].Diam*Ucf[DIAM]); else x[i] = 0.0f; } if (FSAVE(Nlinks) < (unsigned)Nlinks) errcode = 308; } /* Free memory used for buffer arrays */ free(ibuf); free(x); return(errcode); }
void CErrorMsg::PrintError(ERRORCODE ecode,PRINTTYPE PrintType) { #define ERRSTART switch(ecode) { #define ERRCODE(code,str) case code: sprintf(tmp,str); break #define ERREND } char tmp[256]; ERRSTART ERRCODE(EC_GENERAL_ERROR, "General Error"); ERRCODE(EC_CONNECT_DISTRIBUTESERVER_FAILED, "TIMEOUT : Connecting To distribute is Failed"); ERRCODE(EC_CONNECT_AGENTSERVER_FAILED,"Connecting To Agent is Failed"); ERRCODE(EC_UNKNOWN_PROTOCOL,"Unknow(Unnecessary) Protocol is Received"); ERRCODE(EC_MSGSEND_FAILED,"Sending Msg is Failed"); ERRCODE(EC_CHARACTERLIST_FAILED,"Receiving CHARACTERLIST is FAILED"); ERRCODE(EC_IDPWAUTH_FAILED,"Checking ID/PW is FAILED"); ERRCODE(EC_CHARACTERSELECT_FAILED,"Selected Character is Invalid"); // ERRCODE(EC_CHARNAME_EXIST, "character name exist"); // ERRCODE(EC_CHARNAME_SUCCESS, "character name doesn't exist"); // ERRCODE(EC_DONOTNAMEDUPLCHECK, "you doesn't check duplicate character name"); ERRCODE(EC_IMAGELOAD_FAILED, "Cann't load Image file from deginated position"); // ERRCODE(EC_CHARACTERMAKE_FAILED, "Character make is failed"); ERRCODE(EC_SKILL_ADDNITEMDELETE_FAILED, "Skill add & item delete failed"); ERRCODE(EC_VERSION_CHECK_FAILED, "Client Version is not updated"); ERRCODE(EC_IPADDRESS_INVALID_FAILED, "Your IP Address is not allowed"); ERRCODE(EC_MAPSERVER_CLOSED, "MapServer Closed. Try Again Later."); ERREND PrintErrorEx(tmp, PrintType); }
int savetimestat(REAL4 *x, char objtype) /* **-------------------------------------------------------------- ** Input: *x = buffer for node values ** objtype = NODEHDR (for nodes) or LINKHDR (for links) ** Output: returns error code ** Purpose: computes time series statistic for nodes or links ** and saves to normal output file. ** ** NOTE: This routine is dependent on how the output reporting ** variables were assigned to FieldType in TYPES.H. **-------------------------------------------------------------- */ { int n, n1, n2; int i, j, p, errcode = 0; long startbyte, skipbytes; float *stat1, *stat2, xx; /* Compute number of bytes in temp output file to skip over (skipbytes) when moving from one time period to the next for a particular variable. */ if (objtype == NODEHDR) { /* For nodes, we start at 0 and skip over node output for all node variables minus 1 plus link output for all link variables. */ startbyte = 0; skipbytes = (Nnodes*(QUALITY-DEMAND) + Nlinks*(FRICTION-FLOW+1))*sizeof(REAL4); n = Nnodes; n1 = DEMAND; n2 = QUALITY; } else { /* For links, we start at the end of all node variables and skip over node output for all node variables plus link output for all link variables minus 1. */ startbyte = Nnodes*(QUALITY-DEMAND+1)*sizeof(REAL4); skipbytes = (Nnodes*(QUALITY-DEMAND+1) + Nlinks*(FRICTION-FLOW))*sizeof(REAL4); n = Nlinks; n1 = FLOW; n2 = FRICTION; } stat1 = (float *) calloc(n+1, sizeof(float)); stat2 = (float *) calloc(n+1, sizeof(float)); ERRCODE(MEMCHECK(stat1)); ERRCODE(MEMCHECK(stat2)); /* Process each output reporting variable */ if (!errcode) { for (j=n1; j<=n2; j++) { /* Initialize stat arrays */ if (Tstatflag == AVG) memset(stat1, 0, (n+1)*sizeof(float)); else for (i=1; i<=n; i++) { stat1[i] = -MISSING; /* +1E10 */ stat2[i] = MISSING; /* -1E10 */ } /* Position temp output file at start of output */ fseek(TmpOutFile, startbyte + (j-n1)*n*sizeof(REAL4), SEEK_SET); /* Process each time period */ for (p=1; p<=Nperiods; p++) { /* Get output results for time period & update stats */ fread(x+1, sizeof(REAL4), n, TmpOutFile); for (i=1; i<=n; i++) { xx = x[i]; if (objtype == LINKHDR) { if (j == FLOW) xx = ABS(xx); if (j == STATUS) { if (xx >= OPEN) xx = 1.0; else xx = 0.0; } } if (Tstatflag == AVG) stat1[i] += xx; else { stat1[i] = MIN(stat1[i], xx); stat2[i] = MAX(stat2[i], xx); } } /* Advance file to next period */ if (p < Nperiods) fseek(TmpOutFile, skipbytes, SEEK_CUR); } /* Compute resultant stat & save to regular output file */ switch (Tstatflag) { case AVG: for (i=1; i<=n; i++) x[i] = stat1[i]/(float)Nperiods; break; case MIN: for (i=1; i<=n; i++) x[i] = stat1[i]; break; case MAX: for (i=1; i<=n; i++) x[i] = stat2[i]; break; case RANGE: for (i=1; i<=n; i++) x[i] = stat2[i] - stat1[i]; break; } if (objtype == LINKHDR && j == STATUS) { for (i=1; i<=n; i++) { if (x[i] < 0.5f) x[i] = CLOSED; else x[i] = OPEN; } } if (fwrite(x+1, sizeof(REAL4), n, OutFile) < (unsigned) n) errcode = 308; /* Update internal output variables where applicable */ if (objtype == NODEHDR) switch (j) { case DEMAND: for (i=1; i<=n; i++) NodeDemand[i] = x[i]/Ucf[DEMAND]; break; case HEAD: for (i=1; i<=n; i++) NodeHead[i] = x[i]/Ucf[HEAD]; break; case QUALITY: for (i=1; i<=n; i++) NodeQual[i] = x[i]/Ucf[QUALITY]; break; } else if (j == FLOW) for (i=1; i<=n; i++) Q[i] = x[i]/Ucf[FLOW]; } } /* Free allocated memory */ free(stat1); free(stat2); return(errcode); }