ningshuxia
2024-04-25 769413fc5ff52240f001fb4bcfcca21728fb275a
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
using IStation.Epanet.Enums;
using IStation.Epanet.Network.Structures;
using System.Diagnostics;
 
namespace IStation.Epanet.Network
{
 
    ///<summary>Units report properties & conversion support class</summary>
    public class FieldsMap
    {
        ///<summary>Report fields properties.</summary>
        private readonly Dictionary<FieldType, Field> _fields = new Dictionary<FieldType, Field>();
        ///<summary>Fields units values.</summary>
        private readonly Dictionary<FieldType, double> _units = new Dictionary<FieldType, double>();
 
        ///<summary>Init fields default configuration</summary>
        public FieldsMap()
        {
            try
            {
 
                foreach (FieldType type in Enum.GetValues(typeof(FieldType)))
                    _fields[type] = new Field(type);
 
                GetField(FieldType.FRICTION).Precision = 3;
 
                for (var i = FieldType.DEMAND; i <= FieldType.QUALITY; i++)
                    GetField(i).Enabled = true;
 
                for (var i = FieldType.FLOW; i <= FieldType.HEADLOSS; i++)
                    GetField(i).Enabled = true;
            }
            catch (EnException e)
            {
                Debug.Print(e.ToString());
            }
        }
 
        ///<summary>Get report field properties from type.</summary>
        /// <param name="fieldType">Field type.</param>
        /// <returns>Report field.</returns>
        /// <remarks>
        /// Throws <see cref="EnException"/> 
        /// If specified type not found.
        /// </remarks>
        public Field GetField(FieldType fieldType)
        {
            if (!_fields.TryGetValue(fieldType, out Field value))
                throw new EnException(ErrorCode.Err201, fieldType.ParseStr());
 
            return value;
        }
 
        ///<summary>Get conversion value from field type.</summary>
        /// <param name="fieldType">Field type.</param>
        /// <returns>Conversion units value (from user units to system units).</returns>
        /// <remarks>
        /// Throws <see cref="EnException"/> If specified type not found.
        /// </remarks>
        public double GetUnits(FieldType fieldType)
        {
            if (!_units.TryGetValue(fieldType, out double value))
                throw new EnException(ErrorCode.Err201, fieldType.ParseStr());
 
            return value;
        }
 
        ///<summary>Update fields and units, after loading the INP.</summary>
        public void Prepare(
            UnitsType targetUnits,
            FlowUnitsType flowFlag,
            PressUnitsType pressFlag,
            QualType qualFlag,
            string chemUnits,
            double spGrav,
            TimeSpan hstep)
        {
 
            double dcf,
                   ccf,
                   qcf,
                   hcf,
                   pcf,
                   wcf;
 
            if (targetUnits == UnitsType.SI)
            {
 
                GetField(FieldType.DEMAND).Units = flowFlag.ToString();
                GetField(FieldType.ELEV).Units = Keywords.u_METERS;
                GetField(FieldType.HEAD).Units = Keywords.u_METERS;
 
                GetField(FieldType.PRESSURE).Units = pressFlag == PressUnitsType.METERS
                    ? Keywords.u_METERS
                    : Keywords.u_KPA;
 
                GetField(FieldType.LENGTH).Units = Keywords.u_METERS;
                GetField(FieldType.DIAM).Units = Keywords.u_MMETERS;
                GetField(FieldType.FLOW).Units = flowFlag.ToString();
                GetField(FieldType.VELOCITY).Units = Keywords.u_MperSEC;
                GetField(FieldType.HEADLOSS).Units = "m" + Keywords.u_per1000M;
                GetField(FieldType.FRICTION).Units = "";
                GetField(FieldType.POWER).Units = Keywords.u_KW;
 
                dcf = 1000.0 * Constants.MperFT;
                qcf = Constants.LPSperCFS;
                switch (flowFlag)
                {
                    case FlowUnitsType.Lpm:
                        qcf = Constants.LPMperCFS;
                        break;
                    case FlowUnitsType.Mld:
                        qcf = Constants.MLDperCFS;
                        break;
                    case FlowUnitsType.Cmh:
                        qcf = Constants.CMHperCFS;
                        break;
                    case FlowUnitsType.Cmd:
                        qcf = Constants.CMDperCFS;
                        break;
                }
 
                hcf = Constants.MperFT;
                if (pressFlag == PressUnitsType.METERS) pcf = Constants.MperFT * spGrav;
                else pcf = Constants.KPAperPSI * Constants.PSIperFT * spGrav;
                wcf = Constants.KWperHP;
            }
            else
            {
                GetField(FieldType.DEMAND).Units = flowFlag.ToString();
                GetField(FieldType.ELEV).Units = Keywords.u_FEET;
                GetField(FieldType.HEAD).Units = Keywords.u_FEET;
 
                GetField(FieldType.PRESSURE).Units = Keywords.u_PSI;
                GetField(FieldType.LENGTH).Units = Keywords.u_FEET;
                GetField(FieldType.DIAM).Units = Keywords.u_INCHES;
                GetField(FieldType.FLOW).Units = flowFlag.ToString();
                GetField(FieldType.VELOCITY).Units = Keywords.u_FTperSEC;
                GetField(FieldType.HEADLOSS).Units = "ft" + Keywords.u_per1000FT;
                GetField(FieldType.FRICTION).Units = "";
                GetField(FieldType.POWER).Units = Keywords.u_HP;
 
 
                dcf = 12.0;
                qcf = 1.0;
 
                switch (flowFlag)
                {
                    case FlowUnitsType.Gpm:
                        qcf = Constants.GPMperCFS;
                        break;
                    case FlowUnitsType.Mgd:
                        qcf = Constants.MGDperCFS;
                        break;
                    case FlowUnitsType.Imgd:
                        qcf = Constants.IMGDperCFS;
                        break;
                    case FlowUnitsType.Afd:
                        qcf = Constants.AFDperCFS;
                        break;
                }
 
                hcf = 1.0;
                pcf = Constants.PSIperFT * spGrav;
                wcf = 1.0;
            }
 
            GetField(FieldType.QUALITY).Units = "";
            ccf = 1.0;
 
            switch (qualFlag)
            {
                case QualType.Chem:
                    ccf = 1.0 / Constants.LperFT3;
                    GetField(FieldType.QUALITY).Units = chemUnits;
                    GetField(FieldType.REACTRATE).Units = chemUnits + Keywords.t_PERDAY;
                    break;
                case QualType.Age:
                    GetField(FieldType.QUALITY).Units = Keywords.u_HOURS;
                    break;
                case QualType.Trace:
                    GetField(FieldType.QUALITY).Units = Keywords.u_PERCENT;
                    break;
            }
 
            SetUnits(FieldType.DEMAND, qcf);
            SetUnits(FieldType.ELEV, hcf);
            SetUnits(FieldType.HEAD, hcf);
            SetUnits(FieldType.PRESSURE, pcf);
            SetUnits(FieldType.QUALITY, ccf);
            SetUnits(FieldType.LENGTH, hcf);
            SetUnits(FieldType.DIAM, dcf);
            SetUnits(FieldType.FLOW, qcf);
            SetUnits(FieldType.VELOCITY, hcf);
            SetUnits(FieldType.HEADLOSS, hcf);
            SetUnits(FieldType.LINKQUAL, ccf);
            SetUnits(FieldType.REACTRATE, ccf);
            SetUnits(FieldType.FRICTION, 1.0);
            SetUnits(FieldType.POWER, wcf);
            SetUnits(FieldType.VOLUME, hcf * hcf * hcf);
 
            if (hstep.TotalSeconds < 1800)
            {
                SetUnits(FieldType.TIME, 1.0 / 60.0);
                GetField(FieldType.TIME).Units = Keywords.u_MINUTES;
            }
            else
            {
                SetUnits(FieldType.TIME, 1.0 / 3600.0);
                GetField(FieldType.TIME).Units = Keywords.u_HOURS;
            }
        }
 
        /// <summary>Revert system units to user units.</summary>
        ///  <param name="fieldType">Field type.</param>
        /// <param name="value">Value to be converted.</param>
        /// <returns>Value in user units.</returns>
        public double RevertUnit(FieldType fieldType, double value)
        {
            return fieldType == (FieldType)(-1)
                ? value
                : value * GetUnits(fieldType);
        }
 
        public double ConvertUnitToSystem(FieldType fieldType, double value)
        {
            return value / GetUnits(fieldType);
        }
 
        ///<summary>Set conversion value from field type.</summary>
        /// <param name="fieldType">Field type.</param>
        /// <param name="value">Field value.</param>
        private void SetUnits(FieldType fieldType, double value)
        {
            _units[fieldType] = value;
        }
 
    }
 
}