From a9eb63b17384f317929136e96d21ac4ff65f1ede Mon Sep 17 00:00:00 2001 From: Jonathan Clough Date: Fri, 29 Mar 2024 15:00:54 -0400 Subject: [PATCH] AQ_Sim_2D: Improved weighted averaging when passing data by concentrations so that incoming concentrations are weighted by daily flow of multiple inputs. NewSimForm.cs: Fixed some relative file-path references Continued to refine csproj and publish options to get correct files copied to correct directories. --- Data.Simulate.AQUATOX/AQ_Sim_2D.cs | 66 +- GUI/GUI.AQUATOX/GUI.AQUATOX.csproj | 5 +- GUI/GUI.AQUATOX/MultiSegForm.Designer.cs | 2 +- GUI/GUI.AQUATOX/NewSimForm.cs | 6 +- GUI/GUI.AQUATOX/Param_Form.cs | 2 +- .../PublishProfiles/ClickOnceProfile.pubxml | 1208 +++++++++++++++++ Web.Services/Models/WSAquatoxWorkflow.cs | 3 +- 7 files changed, 1259 insertions(+), 33 deletions(-) diff --git a/Data.Simulate.AQUATOX/AQ_Sim_2D.cs b/Data.Simulate.AQUATOX/AQ_Sim_2D.cs index 1357b9ef..d95ca636 100644 --- a/Data.Simulate.AQUATOX/AQ_Sim_2D.cs +++ b/Data.Simulate.AQUATOX/AQ_Sim_2D.cs @@ -1467,12 +1467,14 @@ public string PopulateStreamNetwork(int iSeg, string setupjson, out string jsond /// Number of upstream inputs /// Data structure of archived inputs from which to gather model results /// if true passes mass, if false, passes the water concentration which can be done if water models may differ between segments e.g. reservoir to stream + /// a list containing the sum of the previous flows linked to this segment for each time step. Needed for weighted averaging. /// a list of any additional divergence flows from source segment (flows not to this segment), for the complete set of time-steps of the simulation in m3/s /// errors, warnings - public string Pass_Data(AQTSim Sim, int SrcID, int ninputs, bool passMass, archived_results AR = null, List >> divergence_flows = null) + public string Pass_Data(AQTSim Sim, int SrcID, int ninputs, bool passMass, ref SortedList previous_flows, archived_results AR = null, List >> divergence_flows = null) { + //archived_results AR; - if (AR == null) // (AR.Equals(null)) crashed + if (AR == null) { archive.TryGetValue(SrcID, out AR); if (AR == null) @@ -1482,7 +1484,7 @@ public string Pass_Data(AQTSim Sim, int SrcID, int ninputs, bool passMass, archi SN.waterbodies.comid_wb.TryGetValue(SrcID, out SrcID); // translate SrcID to the relevant WBCOMID archive.TryGetValue(SrcID, out AR); } - if (AR == null) return "WARNING: Segment "+Sim.AQTSeg.StudyName+" is missing expected linkage data from "+SrcID; + if (AR == null) return "ERROR: Segment "+Sim.AQTSeg.StudyName+" is missing expected linkage data from "+SrcID; } } @@ -1497,28 +1499,29 @@ public string Pass_Data(AQTSim Sim, int SrcID, int ninputs, bool passMass, archi ((TSV.IsPlant()) && ( ((TPlant)TSV).IsPhytoplankton() || (((TPlant)TSV).IsMacrophyte() && (((TPlant)TSV).MacroType == TMacroType.Freefloat))) ) || ((TSV.IsAnimal()) && ((TAnimal)TSV).IsPlanktonInvert())) { - TVolume tvol = Sim.AQTSeg.GetStatePointer(AllVariables.Volume, T_SVType.StV, T_SVLayer.WaterCol) as TVolume; + TVolume tvol = Sim.AQTSeg.GetStatePointer(AllVariables.Volume, T_SVType.StV, T_SVLayer.WaterCol) as TVolume; int ndates = AR.dates.Count(); - TLoadings flowLoad; - if (tvol.Calc_Method == VolumeMethType.Manning) flowLoad = tvol.LoadsRec.Alt_Loadings[1]; //manning's input is "discharge load" or [1] - else flowLoad = tvol.LoadsRec.Alt_Loadings[0]; //[0] is inflow + TLoadings flowLoad; + if (tvol.Calc_Method == VolumeMethType.Manning) flowLoad = tvol.LoadsRec.Alt_Loadings[1]; //manning's input is "discharge load" or [1] + else flowLoad = tvol.LoadsRec.Alt_Loadings[0]; //[0] is inflow - SortedList newlist = new SortedList(); + SortedList newlist = new SortedList(); + if (ninputs == 1) previous_flows = new SortedList(); // first or only loading, start saving flows for weighted averaging with potential future loadings for (int i = 0; i < ndates; i++) - { + { double OutVol = AR.washout[i]; // out volume to this segment from upstream segment - double frac_this_segment = 1.0; - double totOutVol = OutVol; - if (divergence_flows != null) - foreach (ITimeSeriesOutput> its in divergence_flows) - { - totOutVol = totOutVol + Convert.ToDouble(its.Data.Values.ElementAt(i)[0]) * 86400 ; //potential issue if master setup time-step changes or simulation time-period is increased since NWM data gathering - // m3/d m3/d m3/s s/d - frac_this_segment = OutVol / totOutVol; - } + double frac_this_segment = 1.0; + double totOutVol = OutVol; + if (divergence_flows != null) + foreach (ITimeSeriesOutput> its in divergence_flows) + { + totOutVol = totOutVol + Convert.ToDouble(its.Data.Values.ElementAt(i)[0]) * 86400; //fixme potential issue if master setup time-step changes or simulation time-period is increased since NWM data gathering + // m3/d m3/d m3/s s/d + frac_this_segment = OutVol / totOutVol; + } double InVol = flowLoad.ReturnLoad(AR.dates[i]); // inflow volume to current segment, If velocity is not used, must be estimated as current seg. outflow @@ -1527,13 +1530,23 @@ public string Pass_Data(AQTSim Sim, int SrcID, int ninputs, bool passMass, archi if (InVol < Consts.Tiny) newlist.Add(AR.dates[i], 0); else if (ninputs == 1) newlist.Add(AR.dates[i], AR.concs[iTSV][i] * (OutVol / InVol) * frac_this_segment); // first or only input else newlist.Add(AR.dates[i], TSV.LoadsRec.Loadings.list.Values[i] + AR.concs[iTSV][i] * (OutVol / InVol) * frac_this_segment); //adding second or third inputs + } + else // pass concentration + { + if (ninputs == 1) + { + newlist.Add(AR.dates[i], AR.concs[iTSV][i]); // first or only input + previous_flows.Add(AR.dates[i], OutVol * frac_this_segment); //water flowing into this segment at this time step corrected for divergences } - else // pass concentration + else { - if (ninputs == 1) newlist.Add(AR.dates[i], AR.concs[iTSV][i]); // first or only input - else newlist.Add(AR.dates[i], (TSV.LoadsRec.Loadings.list.Values[i] *(ninputs-1) + AR.concs[iTSV][i])/ninputs); //weighted-averaging second or third inputs. FIXME not weighted by flow + double otherSegFlows = previous_flows.Values[i]; //sum of other-segment flows into this segment in this time step + double thisSegFlows = OutVol * frac_this_segment; //flows into this segment from passage segment this time step + double totFlows = otherSegFlows + thisSegFlows; //total flows into this segment this time step + newlist.Add(AR.dates[i], (TSV.LoadsRec.Loadings.list.Values[i] * otherSegFlows + AR.concs[iTSV][i] * thisSegFlows) / totFlows); //weighted-averaging second or third inputs by volume of water. } } + } TSV.LoadsRec.Loadings.list = newlist; TSV.LoadsRec.Loadings.UseConstant = false; @@ -1593,7 +1606,8 @@ public bool executeModel(long segID, string setupjson, ref List outstr, SVList.Add(SV.PName+" ("+SV.StateUnit+")"); //save list of SVs for output } } - + + SortedList previous_flows = null; int nSources = 0; if (SN != null) { @@ -1604,7 +1618,7 @@ public bool executeModel(long segID, string setupjson, ref List outstr, if ((SrcID != segID) && !outofnetwork.Contains(SrcID)) // don't pass data from out of network segments { nSources++; - string errstr = Pass_Data(Sim, SrcID, nSources, false, null, divergence_flows); + string errstr = Pass_Data(Sim, SrcID, nSources, false, ref previous_flows, null, divergence_flows); if (errstr != "") outstr.Add(errstr); else outstr.Add("INFO: Passed data from Source " + SrcID.ToString() + " into COMID " + segID.ToString()); } @@ -1623,11 +1637,11 @@ public bool executeModel(long segID, string setupjson, ref List outstr, if (AR != null) { nSources++; - string errstr = Pass_Data(Sim, SrcID, nSources, false, null, divergence_flows); + string errstr = Pass_Data(Sim, SrcID, nSources, false, ref previous_flows, null, divergence_flows); if (errstr != "") outstr.Add(errstr); else outstr.Add("INFO: Passed data from Source " + entry.Key.ToString() + " into WBCOMID " + segID.ToString()); - } - } + } + } } } diff --git a/GUI/GUI.AQUATOX/GUI.AQUATOX.csproj b/GUI/GUI.AQUATOX/GUI.AQUATOX.csproj index 45459574..e82bde62 100644 --- a/GUI/GUI.AQUATOX/GUI.AQUATOX.csproj +++ b/GUI/GUI.AQUATOX/GUI.AQUATOX.csproj @@ -146,7 +146,10 @@ - + + PreserveNewest + + SettingsSingleFileGenerator Settings.Designer.cs diff --git a/GUI/GUI.AQUATOX/MultiSegForm.Designer.cs b/GUI/GUI.AQUATOX/MultiSegForm.Designer.cs index 751bb7b7..6a524de1 100644 --- a/GUI/GUI.AQUATOX/MultiSegForm.Designer.cs +++ b/GUI/GUI.AQUATOX/MultiSegForm.Designer.cs @@ -167,7 +167,7 @@ private void InitializeComponent() BaseJSONBox.Size = new System.Drawing.Size(200, 23); BaseJSONBox.TabIndex = 16; BaseJSONBox.Tag = ""; - BaseJSONBox.Text = "..\\..\\..\\2D_Inputs\\LBR Glenwood 4.JSON"; + BaseJSONBox.Text = "..\\2D_Inputs\\LBR Glenwood 4.JSON"; BaseJSONBox.Enter += BaseJSONBox_Enter; BaseJSONBox.Leave += BaseJSONBox_Leave; // diff --git a/GUI/GUI.AQUATOX/NewSimForm.cs b/GUI/GUI.AQUATOX/NewSimForm.cs index e1890ad2..7465d017 100644 --- a/GUI/GUI.AQUATOX/NewSimForm.cs +++ b/GUI/GUI.AQUATOX/NewSimForm.cs @@ -279,7 +279,7 @@ private static bool ReadLakeSurrogates() if (LS != null) return true; // don't need to re-read already in memory double LSVersionNum = 0; - string fileN = Path.GetFullPath("..\\..\\..\\2D_Inputs\\" + "Multi_Seg_Surrogates.json"); + string fileN = Path.GetFullPath("..\\2D_Inputs\\" + "Multi_Seg_Surrogates.json"); if (File.Exists(fileN)) { @@ -489,9 +489,9 @@ private void MapType_CheckChanged(object sender, EventArgs e) private void MS_Surrogate_Button_Click(object sender, EventArgs e) { // private functionality to create and write Multi-segment Surrogates object - Lake_Surrogates LS = new Lake_Surrogates(Path.GetFullPath("..\\..\\..\\2D_Inputs\\" + "MS_Surrogates_Table.json"), "..\\..\\..\\Studies\\"); + Lake_Surrogates LS = new Lake_Surrogates(Path.GetFullPath("..\\2D_Inputs\\" + "MS_Surrogates_Table.json"), "..\\Studies\\"); string json = JsonConvert.SerializeObject(LS, LSJSONSettings()); - File.WriteAllText("..\\..\\..\\2D_Inputs\\" + "Multi_Seg_Surrogates.json", json); + File.WriteAllText("..\\2D_Inputs\\" + "Multi_Seg_Surrogates.json", json); return; } diff --git a/GUI/GUI.AQUATOX/Param_Form.cs b/GUI/GUI.AQUATOX/Param_Form.cs index cdfa49d2..369f8548 100644 --- a/GUI/GUI.AQUATOX/Param_Form.cs +++ b/GUI/GUI.AQUATOX/Param_Form.cs @@ -495,7 +495,7 @@ public void removeControls(int i) private string ReadDBPath(string deflt) { - string fileN = Path.GetFullPath("..\\..\\..\\DB\\" + deflt); + string fileN = Path.GetFullPath("..\\DB\\" + deflt); if (MessageBox.Show("Open the default database: '" + fileN + "'?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.No) diff --git a/GUI/GUI.AQUATOX/Properties/PublishProfiles/ClickOnceProfile.pubxml b/GUI/GUI.AQUATOX/Properties/PublishProfiles/ClickOnceProfile.pubxml index 0098227f..782ed0f7 100644 --- a/GUI/GUI.AQUATOX/Properties/PublishProfiles/ClickOnceProfile.pubxml +++ b/GUI/GUI.AQUATOX/Properties/PublishProfiles/ClickOnceProfile.pubxml @@ -35,6 +35,1214 @@ https://go.microsoft.com/fwlink/?LinkID=208121. Publish.html false + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + + + + + + Include + true + File + + true diff --git a/Web.Services/Models/WSAquatoxWorkflow.cs b/Web.Services/Models/WSAquatoxWorkflow.cs index 84a2c9b7..b15bb92b 100644 --- a/Web.Services/Models/WSAquatoxWorkflow.cs +++ b/Web.Services/Models/WSAquatoxWorkflow.cs @@ -210,10 +210,11 @@ private void Pass_Data(AQTSim sim, List comids) { // Pass the archived data to the current simulation int nSources = 0; + SortedList previous_flows = null; foreach (int SrcId in comids) { nSources++; - Pass_Data(sim, SrcId, nSources, true, this.archive[SrcId]); + Pass_Data(sim, SrcId, nSources, true, ref previous_flows, this.archive[SrcId]); }; }