-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.c
1222 lines (1090 loc) · 36 KB
/
report.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*********************************************************************
REPORT.C -- Reporting Routines for EPANET Program
VERSION: 2.00
DATE: 5/30/00
6/24/02
8/15/07 (2.00.11)
2/14/08 (2.00.12)
AUTHOR: L. Rossman
US EPA - NRMRL
This module contains various procedures (all beginning with
'write') that are called from other modules to write formatted
output to a report file.
It also contains function disconnected(), called from writehydwarn()
and writehyderr(), that checks if a hydraulic solution causes a
network to become disconnected.
The function writeline(S) is used throughout to write a
formatted string S to the report file.
********************************************************************
*/
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <math.h>
#include <time.h>
#include "hash.h"
#include "text.h"
#include "types.h"
#include "funcs.h"
#define EXTERN extern
#include "vars.h"
#define MAXCOUNT 10 /* Max. # of disconnected nodes listed */
long LineNum; /* Current line number */
long PageNum; /* Current page number */
char DateStamp[26]; /* Current date & time */
char Fprinterr; /* File write error flag */
/* Defined in enumstxt.h in EPANET.C */
extern char *NodeTxt[];
extern char *LinkTxt[];
extern char *StatTxt[];
extern char *TstatTxt[];
extern char *LogoTxt[];
extern char *RptFormTxt[];
typedef REAL4 *Pfloat;
void writenodetable(Pfloat *);
void writelinktable(Pfloat *);
int writereport()
/*
**------------------------------------------------------
** Input: none
** Output: returns error code
** Purpose: writes formatted output report to file
**
** Calls strcomp() from the EPANET.C module.
**------------------------------------------------------
*/
{
char tflag;
FILE *tfile;
int errcode = 0;
/* If no secondary report file specified then */
/* write formatted output to primary report file. */
Fprinterr = FALSE;
if (Rptflag && strlen(Rpt2Fname) == 0 && RptFile != NULL)
{
writecon(FMT17);
writecon(Rpt1Fname);
if (Energyflag) writeenergy();
errcode = writeresults();
}
/* A secondary report file was specified */
else if (strlen(Rpt2Fname) > 0)
{
/* If secondary report file has same name as either input */
/* or primary report file then use primary report file. */
if (strcomp(Rpt2Fname,InpFname) ||
strcomp(Rpt2Fname,Rpt1Fname))
{
writecon(FMT17);
writecon(Rpt1Fname);
if (Energyflag) writeenergy();
errcode = writeresults();
}
/* Otherwise write report to secondary report file. */
else
{
/* Try to open file */
tfile = RptFile;
tflag = Rptflag;
if ((RptFile = fopen(Rpt2Fname,"wt")) == NULL)
{
RptFile = tfile;
Rptflag = tflag;
errcode = 303;
}
/* Write full formatted report to file */
else
{
Rptflag = 1;
writecon(FMT17);
writecon(Rpt2Fname);
writelogo();
if (Summaryflag) writesummary();
if (Energyflag) writeenergy();
errcode = writeresults();
fclose(RptFile);
RptFile = tfile;
Rptflag = tflag;
}
}
}
/* Special error handler for write-to-file error */
if (Fprinterr) errmsg(309);
return(errcode);
} /* End of writereport */
void writelogo()
/*
**--------------------------------------------------------------
** Input: none
** Output: none
** Purpose: writes program logo to report file.
**--------------------------------------------------------------
*/
{
int i;
time_t timer; /* time_t structure & functions time() & */
/* ctime() are defined in time.h */
time(&timer);
strcpy(DateStamp,ctime(&timer));
PageNum = 1;
LineNum = 2;
fprintf(RptFile,FMT18);
fprintf(RptFile,"%s",DateStamp);
for (i=0; LogoTxt[i] != NULL; i++) writeline(LogoTxt[i]);
writeline("");
} /* End of writelogo */
void writesummary()
/*
**--------------------------------------------------------------
** Input: none
** Output: none
** Purpose: writes summary system information to report file
**--------------------------------------------------------------
*/
{
char s[MAXFNAME+1];
int i;
int nres = 0;
for (i=0; i<3; i++)
{
if (strlen(Title[i]) > 0)
{
sprintf(s,"%-.70s",Title[i]);
writeline(s);
}
}
writeline(" ");
sprintf(s,FMT19,InpFname);
writeline(s);
sprintf(s,FMT20,Njuncs);
writeline(s);
for (i=1; i<=Ntanks; i++)
if (Tank[i].A == 0.0) nres++;
sprintf(s,FMT21a,nres);
writeline(s);
sprintf(s,FMT21b,Ntanks-nres);
writeline(s);
sprintf(s,FMT22,Npipes);
writeline(s);
sprintf(s,FMT23,Npumps);
writeline(s);
sprintf(s,FMT24,Nvalves);
writeline(s);
sprintf(s,FMT25,RptFormTxt[Formflag]);
writeline(s);
sprintf(s,FMT26,Hstep*Ucf[TIME],Field[TIME].Units);
writeline(s);
sprintf(s,FMT27,Hacc);
writeline(s);
sprintf(s,FMT27a,CheckFreq); //(2.00.12 - LR)
writeline(s); //(2.00.12 - LR)
sprintf(s,FMT27b,MaxCheck); //(2.00.12 - LR)
writeline(s); //(2.00.12 - LR)
sprintf(s,FMT27c,DampLimit); //(2.00.12 - LR)
writeline(s); //(2.00.12 - LR)
sprintf(s,FMT28,MaxIter);
writeline(s);
if (Qualflag == NONE || Dur == 0.0)
sprintf(s,FMT29);
else if (Qualflag == CHEM)
sprintf(s,FMT30,ChemName);
else if (Qualflag == TRACE)
sprintf(s,FMT31,Node[TraceNode].ID);
else if (Qualflag == AGE)
sprintf(s,FMT32);
writeline(s);
if (Qualflag != NONE && Dur > 0)
{
sprintf(s,FMT33,(float)Qstep/60.0);
writeline(s);
sprintf(s,FMT34,Ctol*Ucf[QUALITY],Field[QUALITY].Units);
writeline(s);
}
sprintf(s,FMT36,SpGrav);
writeline(s);
sprintf(s,FMT37a,Viscos/VISCOS);
writeline(s);
sprintf(s,FMT37b,Diffus/DIFFUS);
writeline(s);
sprintf(s,FMT38,Dmult);
writeline(s);
sprintf(s,FMT39,Dur*Ucf[TIME],Field[TIME].Units);
writeline(s);
if (Rptflag)
{
sprintf(s,FMT40);
writeline(s);
if (Nodeflag == 0) writeline(FMT41);
if (Nodeflag == 1) writeline(FMT42);
if (Nodeflag == 2) writeline(FMT43);
writelimits(DEMAND,QUALITY);
if (Linkflag == 0) writeline(FMT44);
if (Linkflag == 1) writeline(FMT45);
if (Linkflag == 2) writeline(FMT46);
writelimits(DIAM,HEADLOSS);
}
writeline(" ");
} /* End of writesummary */
void writehydstat(int iter, double relerr)
/*
**--------------------------------------------------------------
** Input: iter = # iterations to find hydraulic solution
** relerr = convergence error in hydraulic solution
** Output: none
** Purpose: writes hydraulic status report for solution found
** at current time period to report file
**--------------------------------------------------------------
*/
{
int i,n;
char newstat;
char s1[MAXLINE+1];
/*** Updated 6/24/02 ***/
char atime[13];
/* Display system status */
strcpy(atime,clocktime(Atime,Htime));
if (iter > 0)
{
if (relerr <= Hacc)
sprintf(s1,FMT58,atime,iter);
else
sprintf(s1,FMT59,atime,iter,relerr);
writeline(s1);
}
/*
Display status changes for tanks.
D[n] is net inflow to tank at node n.
Old tank status is stored in OldStat[]
at indexes Nlinks+1 to Nlinks+Ntanks.
*/
for (i=1; i<=Ntanks; i++)
{
n = Tank[i].Node;
if (ABS(D[n]) < 0.001) newstat = CLOSED;
else if (D[n] > 0.0) newstat = FILLING;
else if (D[n] < 0.0) newstat = EMPTYING;
else newstat = OldStat[Nlinks+i];
if (newstat != OldStat[Nlinks+i])
{
if (Tank[i].A > 0.0)
sprintf(s1,FMT50,atime,Node[n].ID,StatTxt[newstat],
(H[n]-Node[n].El)*Ucf[HEAD],Field[HEAD].Units);
else sprintf(s1,FMT51,atime,Node[n].ID,StatTxt[newstat]);
writeline(s1);
OldStat[Nlinks+i] = newstat;
}
}
/* Display status changes for links */
for (i=1; i<=Nlinks; i++)
{
if (S[i] != OldStat[i])
{
if (Htime == 0)
sprintf(s1,FMT52,atime,LinkTxt[Link[i].Type],Link[i].ID,
StatTxt[S[i]]);
else sprintf(s1,FMT53,atime,LinkTxt[Link[i].Type],Link[i].ID,
StatTxt[OldStat[i]],StatTxt[S[i]]);
writeline(s1);
OldStat[i] = S[i];
}
}
writeline(" ");
} /* End of writehydstat */
void writeenergy()
/*
**-------------------------------------------------------------
** Input: none
** Output: none
** Purpose: writes energy usage report to report file
**-------------------------------------------------------------
*/
{
int j;
double csum;
char s[MAXLINE+1];
if (Npumps == 0) return;
writeline(" ");
writeheader(ENERHDR,0);
csum = 0.0;
for (j=1; j<=Npumps; j++)
{
csum += Pump[j].Energy[5];
if (LineNum == (long)PageSize) writeheader(ENERHDR,1);
sprintf(s,"%-8s %6.2f %6.2f %9.2f %9.2f %9.2f %9.2f",
Link[Pump[j].Link].ID,Pump[j].Energy[0],Pump[j].Energy[1],
Pump[j].Energy[2],Pump[j].Energy[3],Pump[j].Energy[4],
Pump[j].Energy[5]);
writeline(s);
}
fillstr(s,'-',63);
writeline(s);
/*** Updated 6/24/02 ***/
sprintf(s,FMT74,"",Emax*Dcost);
writeline(s);
sprintf(s,FMT75,"",csum+Emax*Dcost);
/*** End of update ***/
writeline(s);
writeline(" ");
} /* End of writeenergy */
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] = (REAL4 *) calloc(n, sizeof(REAL4));
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(REAL4),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(REAL4),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 */
void writenodetable(Pfloat *x)
/*
**---------------------------------------------------------------
** Input: x = pointer to node results for current time
** Output: none
** Purpose: writes node results for current time to report file
**---------------------------------------------------------------
*/
{
int i,j;
char s[MAXLINE+1],s1[16];
double y[MAXVAR];
/* Write table header */
writeheader(NODEHDR,0);
/* For each node: */
for (i=1; i<=Nnodes; i++)
{
/* Place results for each node variable in y */
y[ELEV] = Node[i].El*Ucf[ELEV];
for (j=DEMAND; j<=QUALITY; j++) y[j] = *((x[j-DEMAND])+i);
/* Check if node gets reported on */
if ((Nodeflag == 1 || Node[i].Rpt) && checklimits(y,ELEV,QUALITY))
{
/* Check if new page needed */
if (LineNum == (long)PageSize) writeheader(NODEHDR,1);
/* Add node ID and each reported field to string s */
sprintf(s,"%-15s",Node[i].ID);
for (j=ELEV; j<=QUALITY; j++)
{
if (Field[j].Enabled == TRUE)
{
/*** Updated 6/24/02 ***/
if (fabs(y[j]) > 1.e6) sprintf(s1, "%10.2e", y[j]);
else sprintf(s1, "%10.*f", Field[j].Precision, y[j]);
/*** End of update ***/
strcat(s, s1);
}
}
/* Note if node is a reservoir/tank */
if (i > Njuncs)
{
strcat(s, " ");
strcat(s, NodeTxt[getnodetype(i)]);
}
/* Write results for node */
writeline(s);
}
}
writeline(" ");
}
void writelinktable(Pfloat *x)
/*
**---------------------------------------------------------------
** Input: x = pointer to link results for current time
** Output: none
** Purpose: writes link results for current time to report file
**---------------------------------------------------------------
*/
{
int i,j,k;
char s[MAXLINE+1],s1[16];
double y[MAXVAR];
/* Write table header */
writeheader(LINKHDR,0);
/* For each link: */
for (i=1; i<=Nlinks; i++)
{
/* Place results for each link variable in y */
y[LENGTH] = Link[i].Len*Ucf[LENGTH];
y[DIAM] = Link[i].Diam*Ucf[DIAM];
for (j=FLOW; j<=FRICTION; j++) y[j] = *((x[j-FLOW])+i);
/* Check if link gets reported on */
if ((Linkflag == 1 || Link[i].Rpt) && checklimits(y,DIAM,FRICTION))
{
/* Check if new page needed */
if (LineNum == (long)PageSize) writeheader(LINKHDR,1);
/* Add link ID and each reported field to string s */
sprintf(s,"%-15s",Link[i].ID);
for (j=LENGTH; j<=FRICTION; j++)
{
if (Field[j].Enabled == TRUE)
{
if (j == STATUS)
{
if (y[j] <= CLOSED) k = CLOSED;
else if (y[j] == ACTIVE) k = ACTIVE;
else k = OPEN;
sprintf(s1, "%10s", StatTxt[k]);
}
/*** Updated 6/24/02 ***/
else
{
if (fabs(y[j]) > 1.e6) sprintf(s1, "%10.2e", y[j]);
else sprintf(s1, "%10.*f", Field[j].Precision, y[j]);
}
/*** End of update ***/
strcat(s, s1);
}
}
/* Note if link is a pump or valve */
if ( (j = Link[i].Type) > PIPE)
{
strcat(s, " ");
strcat(s, LinkTxt[j]);
}
/* Write results for link */
writeline(s);
}
}
writeline(" ");
}
void writeheader(int type, int contin)
/*
**--------------------------------------------------------------
** Input: type = table type
** contin = table continuation flag
** Output: none
** Purpose: writes column headings for output report tables
**--------------------------------------------------------------
*/
{
char s[MAXLINE+1],s1[MAXLINE+1],s2[MAXLINE+1],s3[MAXLINE+1];
int i,n;
/* Move to next page if < 11 lines remain on current page. */
if (Rptflag && LineNum+11 > (long)PageSize)
{
while (LineNum < (long)PageSize) writeline(" ");
}
writeline(" ");
/* Hydraulic Status Table */
if (type == STATHDR)
{
sprintf(s,FMT49);
if (contin) strcat(s,t_CONTINUED);
writeline(s);
fillstr(s,'-',70);
writeline(s);
}
/* Energy Usage Table */
if (type == ENERHDR)
{
if (Unitsflag == SI) strcpy(s1,t_perM3);
else strcpy(s1,t_perMGAL);
sprintf(s,FMT71);
if (contin) strcat(s,t_CONTINUED);
writeline(s);
fillstr(s,'-',63);
writeline(s);
sprintf(s,FMT72);
writeline(s);
sprintf(s,FMT73,s1);
writeline(s);
fillstr(s,'-',63);
writeline(s);
}
/* Node Results Table */
if (type == NODEHDR)
{
if (Tstatflag == RANGE) sprintf(s,FMT76,t_DIFFER);
else if (Tstatflag != SERIES) sprintf(s,FMT76,TstatTxt[Tstatflag]);
else if (Dur == 0) sprintf(s,FMT77);
else sprintf(s,FMT78,clocktime(Atime,Htime));
if (contin) strcat(s,t_CONTINUED);
writeline(s);
n = 15;
sprintf(s2,"%15s","");
strcpy(s,t_NODEID);
sprintf(s3,"%-15s",s);
for (i=ELEV; i<QUALITY; i++) if (Field[i].Enabled == TRUE)
{
n += 10;
sprintf(s,"%10s",Field[i].Name);
strcat(s2,s);
sprintf(s,"%10s",Field[i].Units);
strcat(s3,s);
}
if (Field[QUALITY].Enabled == TRUE)
{
n += 10;
sprintf(s,"%10s",ChemName);
strcat(s2,s);
sprintf(s,"%10s",ChemUnits);
strcat(s3,s);
}
fillstr(s1,'-',n);
writeline(s1);
writeline(s2);
writeline(s3);
writeline(s1);
}
/* Link Results Table */
if (type == LINKHDR)
{
if (Tstatflag == RANGE) sprintf(s,FMT79,t_DIFFER);
else if (Tstatflag != SERIES) sprintf(s,FMT79,TstatTxt[Tstatflag]);
else if (Dur == 0) sprintf(s,FMT80);
else sprintf(s,FMT81,clocktime(Atime,Htime));
if (contin) strcat(s,t_CONTINUED);
writeline(s);
n = 15;
sprintf(s2,"%15s","");
strcpy(s,t_LINKID);
sprintf(s3,"%-15s",s);
for (i=LENGTH; i<=FRICTION; i++) if (Field[i].Enabled == TRUE)
{
n += 10;
sprintf(s,"%10s",Field[i].Name);
strcat(s2,s);
sprintf(s,"%10s",Field[i].Units);
strcat(s3,s);
}
fillstr(s1,'-',n);
writeline(s1);
writeline(s2);
writeline(s3);
writeline(s1);
}
} /* End of writeheader */
void writeline(char *s)
/*
**--------------------------------------------------------------
** Input: *s = text string
** Output: none
** Purpose: writes a line of output to report file
**--------------------------------------------------------------
*/
{
if (RptFile == NULL) return;
if (Rptflag)
{
if (LineNum == (long)PageSize)
{
PageNum++;
if (fprintf(RptFile,FMT82,(int)PageNum,Title[0]) == EOF)
Fprinterr = TRUE;
LineNum = 3;
}
}
if (fprintf(RptFile,"\n %s",s) == EOF) Fprinterr = TRUE;
LineNum++;
} /* End of writeline */
void writerelerr(int iter, double relerr)
/*
**-----------------------------------------------------------------
** Input: iter = current iteration of hydraulic solution
** relerr = current convergence error
** Output: none
** Purpose: writes out convergence status of hydraulic solution
**-----------------------------------------------------------------
*/
{
if (iter == 0)
{
sprintf(Msg, FMT64, clocktime(Atime,Htime));
writeline(Msg);
}
else
{
sprintf(Msg,FMT65,iter,relerr);
writeline(Msg);
}
} /* End of writerelerr */
void writestatchange(int k, char s1, char s2)
/*
**--------------------------------------------------------------
** Input: k = link index
** s1 = old link status
** s2 = new link status
** Output: none
** Purpose: writes change in link status to output report
**--------------------------------------------------------------
*/
{
int j1,j2;
double setting;
/* We have a pump/valve setting change instead of a status change */
if (s1 == s2)
{
/*** Updated 10/25/00 ***/
setting = K[k]; //Link[k].Kc;
switch (Link[k].Type)
{
case PRV:
case PSV:
case PBV: setting *= Ucf[PRESSURE]; break;
case FCV: setting *= Ucf[FLOW];
}
sprintf(Msg,FMT56,LinkTxt[Link[k].Type],Link[k].ID,setting);
writeline(Msg);
return;
}
/* We have a status change. Write the old & new status types. */
if (s1 == ACTIVE) j1 = ACTIVE;
else if (s1 <= CLOSED) j1 = CLOSED;
else j1 = OPEN;
if (s2 == ACTIVE) j2 = ACTIVE;
else if (s2 <= CLOSED) j2 = CLOSED;
else j2 = OPEN;
if (j1 != j2)
{
sprintf(Msg,FMT57,LinkTxt[Link[k].Type],Link[k].ID,
StatTxt[j1],StatTxt[j2]);
writeline(Msg);
}
} /* End of writestatchange */
void writecontrolaction(int k, int i)
/*
----------------------------------------------------------------
** Input: k = link index
** i = control index
** Output: none
** Purpose: writes control action taken to status report
**--------------------------------------------------------------
*/
{
int n;
switch (Control[i].Type)
{
case LOWLEVEL:
case HILEVEL:
n = Control[i].Node;
sprintf(Msg,FMT54,clocktime(Atime,Htime),LinkTxt[Link[k].Type],
Link[k].ID,NodeTxt[getnodetype(n)],Node[n].ID);
break;
case TIMER:
case TIMEOFDAY:
sprintf(Msg,FMT55,clocktime(Atime,Htime),LinkTxt[Link[k].Type],
Link[k].ID);
break;
default: return;
}
writeline(Msg);
}
void writeruleaction(int k, char *ruleID)
/*
**--------------------------------------------------------------
** Input: k = link index
** *ruleID = rule ID
** Output: none
** Purpose: writes rule action taken to status report
**--------------------------------------------------------------
*/
{
sprintf(Msg,FMT63,clocktime(Atime,Htime),LinkTxt[Link[k].Type],
Link[k].ID,ruleID);
writeline(Msg);
}
int writehydwarn(int iter, double relerr)
/*
**--------------------------------------------------------------
** Input: iter = # iterations to find hydraulic solution
** Output: warning flag code
** Purpose: writes hydraulic warning message to report file
**
** Note: Warning conditions checked in following order:
** 1. System balanced but unstable
** 2. Negative pressures
** 3. FCV cannot supply flow or PRV/PSV cannot maintain pressure
** 4. Pump out of range
** 5. Network disconnected
** 6. System unbalanced
**--------------------------------------------------------------
*/
{
int i,j;
char flag = 0;
char s; //(2.00.11 - LR)
/* Check if system unstable */
if (iter > MaxIter && relerr <= Hacc)
{
sprintf(Msg,WARN02,clocktime(Atime,Htime));
if (Messageflag) writeline(Msg);
flag = 2;
}
/* Check for negative pressures */
for (i=1; i<=Njuncs; i++)
{
if (H[i] < Node[i].El && D[i] > 0.0)
{
sprintf(Msg,WARN06,clocktime(Atime,Htime));
if (Messageflag) writeline(Msg);
flag = 6;
break;
}
}
/* Check for abnormal valve condition */
for (i=1; i<=Nvalves; i++)
{
j = Valve[i].Link;
if (S[j] >= XFCV)
{
sprintf(Msg,WARN05,LinkTxt[Link[j].Type],Link[j].ID,
StatTxt[S[j]],clocktime(Atime,Htime));
if (Messageflag) writeline(Msg);
flag = 5;
}
}
/* Check for abnormal pump condition */
for (i=1; i<=Npumps; i++)
{
j = Pump[i].Link;
s = S[j]; //(2.00.11 - LR)
if (S[j] >= OPEN) //(2.00.11 - LR)
{ //(2.00.11 - LR)
if (Q[j] > K[j]*Pump[i].Qmax) s = XFLOW; //(2.00.11 - LR)
if (Q[j] < 0.0) s = XHEAD; //(2.00.11 - LR)
} //(2.00.11 - LR)
if (s == XHEAD || s == XFLOW) //(2.00.11 - LR)
{
sprintf(Msg,WARN04,Link[j].ID,StatTxt[s], //(2.00.11 - LR)
clocktime(Atime,Htime));
if (Messageflag) writeline(Msg);
flag = 4;
}
}
/* Check if system is unbalanced */
if (iter > MaxIter && relerr > Hacc)
{
sprintf(Msg,WARN01,clocktime(Atime,Htime));
if (ExtraIter == -1) strcat(Msg,t_HALTED);
if (Messageflag) writeline(Msg);
flag = 1;
}
/* Check for disconnected network */
/* & update global warning flag */
if (flag > 0)
{
disconnected();
Warnflag = flag;
}
return(flag);
} /* End of writehydwarn */
void writehyderr(int errnode)
/*
**-----------------------------------------------------------
** Input: none
** Output: none
** Purpose: outputs status & checks connectivity when
** network hydraulic equations cannot be solved.
**-----------------------------------------------------------
*/
{
sprintf(Msg,FMT62,clocktime(Atime,Htime),Node[errnode].ID);
if (Messageflag) writeline(Msg);
writehydstat(0,0);
disconnected();
} /* End of writehyderr */
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++;