public MainWindow() { InitializeComponent(); DataContext = new ViewModel(); mvm = ((ViewModel)DataContext).TheModel; }
public ViewModel() { TheModel = new MainModel(); TheModel.ReadConfiguration(@"F:\Oplandsmodel\NitrateModel\config_clgw122_Rerun11.xml"); SubModels = new SmartCollection<BaseModel>(); SubModels.AddRange(TheModel.SourceModels.Cast<BaseModel>()); SubModels.AddRange(TheModel.InternalReductionModels.Cast<BaseModel>()); SubModels.AddRange(TheModel.MainStreamRecutionModels.Cast<BaseModel>()); TheModel.LoadCatchments(); }
public void InitializeTest() { MainModel target2 = new MainModel(); target2.LoadCatchments(@"D:\DK_information\TestData\FileStructure\id15_NSTmodel.shp"); PointSource target = new PointSource(); DateTime Start = new DateTime(); DateTime End = new DateTime(); target.ShapeFile = new SafeFile() { FileName = @"D:\DK_information\Overfladevand\Punktkilder\spredt_pkt.shp" }; target.DBFFile = new SafeFile() { FileName = @"D:\DK_information\Overfladevand\Punktkilder\spredt_data_final.dbf" }; target.Initialize(Start, End, target2.AllCatchments.Values); }
public void CurrentCatchmentTest() { MainModel target = new MainModel(); target.LoadCatchments(@"D:\DK_information\TestData\FileStructure\id15_NSTmodel.shp"); target.CurrentCatchment = target.AllCatchments.Values.First(); Stopwatch sw = new Stopwatch(); sw.Start(); sw.Stop(); sw.Reset(); }
public void InitializeTest() { MainModel catchme = new MainModel(); catchme.LoadCatchments(@"D:\DK_information\TestData\FileStructure\id15_NSTmodel.shp"); AtmosphericDeposition target = new AtmosphericDeposition(); DateTime Start = new DateTime(); DateTime End = new DateTime(); target.Shapefile = new SafeFile() { FileName = @"D:\DK_information\TestData\FileStructure\Ndeposition\EMEP_centroid_DK.shp" }; target.ExcelFile = new SafeFile() { FileName = @"D:\DK_information\TestData\FileStructure\Ndeposition\EMEP_Ndep_1990_2013.xlsx" }; target.Initialize(Start, End, catchme.AllCatchments.Values); //TODO: Add precipitation for this to work Assert.AreEqual(0.0144132844, target.GetValue(catchme.AllCatchments.Values.First(), new DateTime(1990, 5, 1)),1e-6); }
[STAThread]//Due to OpenFileDialog static void Main(string[] args) { var par = args.FirstOrDefault(a => a.StartsWith("-")); string FileName=""; if (args.Count(a => !a.StartsWith("-")) == 0) { //Creates an open FileDialog OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Known file types (*.xml)|*.xml"; //Only open .xml-files ofd.Multiselect = false; //Now show the dialog and continue if the user presses ok if (ofd.ShowDialog() == DialogResult.OK) { FileName = ofd.FileName; } } else FileName = args.First(a => !a.StartsWith("-")); if (!string.IsNullOrEmpty(FileName)) { if (par != null) { ExtraPrinter ep = new ExtraPrinter(); // ep.FromConfigFile(FileName); } else { MainModel m = new MainModel(); m.ReadConfiguration(FileName); m.Initialize(); m.Run(); m.Print(); m.DebugPrint(); } } }
public void Calibrate(MainModel MW, DateTime CStart, DateTime CEnd) { dt.Columns.Add("ID15", typeof(int)); dt.Columns.Add("No_iterations", typeof(int)); dt.Columns.Add("LastError", typeof(double)); dt.Columns.Add("GWRatio", typeof(double)); dt.Columns.Add("IntRatio", typeof(double)); dt.Columns.Add("MainRatio", typeof(double)); dt.Columns.Add("RedFactor", typeof(double)); dt.PrimaryKey = new DataColumn[]{ dt.Columns[0]}; DateTime CurrentTime = CStart; string gwsourcename =MW.SourceModels.Single(s=>s.GetType()==typeof(GroundWaterSource)).Name; foreach (var item in MW.AllCatchments.Values) { var row = dt.NewRow(); row[0] = item.ID; CurrentTime = CStart; double gwleach = 0; double gwsourec = 0; double gwConcDeg = 0; double intsource = 0; double intred = 0; double upstream = 0; double mainred = 0; while (CurrentTime < CEnd) { double IntMass = 0; var CurrentState = MW.StateVariables.Rows.Find(new object[] { item.ID, CurrentTime }); gwleach += (double) CurrentState["Leaching"]; gwsourec += (double)CurrentState[gwsourcename]; IntMass = (double)CurrentState[gwsourcename]; foreach (var conc in MW.InternalReductionModels.Where(s => s.GetType() == typeof(ConceptualSourceReducer) && ((ConceptualSourceReducer)s).SourceModelName == gwsourcename)) { gwConcDeg += (double)CurrentState[conc.Name]; IntMass -= (double)CurrentState[conc.Name]; } foreach (var intsou in MW.SourceModels.Where(s => s.Name != gwsourcename)) { intsource += (double)CurrentState[intsou.Name]; IntMass += (double)CurrentState[intsou.Name]; } foreach (var conc in MW.InternalReductionModels.Where(s => s.GetType() != typeof(ConceptualSourceReducer))) { intred += (double)CurrentState[conc.Name]; IntMass -= (double)CurrentState[conc.Name]; } foreach (var mainr in MW.MainStreamRecutionModels) { if (!CurrentState.IsNull(mainr.Name)) { mainred += (double)CurrentState[mainr.Name]; IntMass -= (double)CurrentState[mainr.Name]; } } if (!CurrentState.IsNull("DownStreamOutput")) { IntMass = (double)CurrentState["DownStreamOutput"] - IntMass; upstream += IntMass; } CurrentTime = CurrentTime.AddMonths(1); } if (gwleach == 0) row["GWRatio"]=1; else row["GWRatio"] = (gwleach - gwsourec + gwConcDeg) / gwleach; row["IntRatio"] = intred / (gwsourec - gwConcDeg + intsource); row["MainRatio"] = mainred / (gwsourec - gwConcDeg + intsource - intred + upstream); dt.Rows.Add(row); } CurrentTime = CStart; this.MW = MW; List<Catchment> SortedCatchments = new List<Catchment>(); ConceptualSourceReducer GWCor = new ConceptualSourceReducer(); GWCor.Name = "Calibrator"; GWCor.SourceModelName = "GroundWater"; var LastConceptual = MW.InternalReductionModels.LastOrDefault(c => c.GetType() == typeof(ConceptualSourceReducer)); if (LastConceptual == null) MW.InternalReductionModels.Insert(0, GWCor); else MW.InternalReductionModels.Insert(MW.InternalReductionModels.IndexOf(LastConceptual) + 1, GWCor); if (!MW.StateVariables.Columns.Contains(GWCor.Name)) MW.StateVariables.Columns.Add(GWCor.Name, typeof(double)); ConceptualSourceReducer IntCor = new ConceptualSourceReducer(); IntCor.Name = "Calib_Int"; MW.InternalReductionModels.Add(IntCor); if (!MW.StateVariables.Columns.Contains(IntCor.Name)) MW.StateVariables.Columns.Add(IntCor.Name, typeof(double)); ConceptualSourceReducer MainCor = new ConceptualSourceReducer(); MainCor.Name = "Calib_Main"; MW.MainStreamRecutionModels.Add(MainCor); if (!MW.StateVariables.Columns.Contains(MainCor.Name)) MW.StateVariables.Columns.Add(MainCor.Name, typeof(double)); foreach (var item in MW.EndCatchments) { GetCatchmentsWithObs(item, SortedCatchments); } foreach (var item in MW.AllCatchments.Values) { GWCor.Reduction.Add(item.ID, 0); IntCor.Reduction.Add(item.ID, 0); MainCor.Reduction.Add(item.ID, 0); } int totaliter = 0; foreach (var v in SortedCatchments) { List<double> Errors = new List<double>(); double localdamp = DampingFactor; double currentreducer = 0; double Error = double.MaxValue; int itercount = 0; var row = dt.Rows.Find(v.ID); NewMessage("Calibrating " + v.ID); while (Math.Abs(Error) > AbsoluteConvergence & itercount < MaxNoOfIterations) { v.ObsNitrate = null; v.SimNitrate = null; double accgws = 0; double accs = 0; double accsink = 0; double accmainsink = 0; double obssum = 0; CurrentTime = CStart; while (CurrentTime < CEnd) { v.MoveInTime(CurrentTime); double obsn =v.Measurements.Nitrate.GetValue(CurrentTime, InterpolationMethods.DeleteValue); if (obsn != v.Measurements.Nitrate.DeleteValue) { obssum += obsn; accgws += AccumulateUpstream(GWCor.SourceModelName, v, CurrentTime); foreach (var s in MW.InternalReductionModels) accsink += AccumulateUpstream(s.Name, v, CurrentTime); foreach (var s in MW.SourceModels.Where(ss => ss.Name != GWCor.SourceModelName)) accs += AccumulateUpstream(s.Name, v, CurrentTime); foreach (var s in MW.MainStreamRecutionModels) accmainsink += AccumulateUpstream(s.Name, v, CurrentTime); } CurrentTime = CurrentTime.AddMonths(1); } double[] sim; double[] obs; v.ObsNitrate.AlignRemoveDeletevalues(v.SimNitrate, out obs, out sim); double simerror = obs.Sum() - sim.Sum(); Error = (accs + accgws - accsink - accmainsink) - obssum; if (itercount == 0 & double.IsNaN(Error)) { NewMessage("Initial error is NAN. Could not calibrate " + v.ID); break; } currentreducer = Error / accgws * localdamp; Errors.Add(Error); NewMessage(Error.ToString()); if (double.IsNaN(Error) || (itercount > 2 && Math.Abs(Error) > Errors.Skip(itercount - 3).Take(3).Select(e => Math.Abs(e)).Max())) { SendReducUpstream(v, GWCor.Reduction, currentreducer, "GWRatio", true); SendReducUpstream(v, IntCor.Reduction, InternalRatio * currentreducer, "IntRatio", true); SendReducUpstream(v, MainCor.Reduction, MainRatio * currentreducer, "MainRatio", true); NewMessage("Reduce damping and resetting reducer to first value"); localdamp *= 0.5; currentreducer = Errors.First() / accgws * localdamp; Error = 2 * AbsoluteConvergence; //To make sure we do not NAN for testing in the next iteration. } SendReducUpstream(v, GWCor.Reduction, currentreducer, "GWRatio",false); SendReducUpstream(v, IntCor.Reduction, InternalRatio * currentreducer, "IntRatio", false); SendReducUpstream(v, MainCor.Reduction, MainRatio * currentreducer, "MainRatio", false); itercount++; } totaliter += itercount; row[0] = v.ID; row[1] = itercount; row[2] = Error; row["RedFactor"] = GWCor.Reduction[v.ID]; NewMessage(v.ID + " calibrated in " + itercount + " iterations. Final error: " + Error + ". ReductionFactor: " + GWCor.Reduction[v.ID]); } NewMessage("Total number of model calls: " + totaliter); var outdir = Path.GetDirectoryName(MW.AlldataFile.FileName); GWCor.DebugPrint(outdir, MW.AllCatchments); IntCor.DebugPrint(outdir, MW.AllCatchments); MainCor.DebugPrint(outdir, MW.AllCatchments); using (ShapeWriter sw = new ShapeWriter(Path.Combine(outdir, "CalibrationResult")) { Projection = MainModel.projection }) { for (int i = 0; i < dt.Rows.Count; i++) { GeoRefData gd = new GeoRefData() { Geometry = MW.AllCatchments[(int)dt.Rows[i][0]].Geometry }; gd.Data = dt.Rows[i]; sw.Write(gd); } } }
public void TestPolygonHole() { MainModel target = new MainModel(); target.LoadCatchments(@"D:\NitrateModel\Overfladevand\oplande\id15_NSTmodel_maj2014.shp"); Assert.IsFalse( target.AllCatchments[44601235].Geometry.Contains(563937, 6211641)); }
public void MainViewModelConstructorTest() { MainModel target = new MainModel(); target.ReadConfiguration(@"D:\Work\HydroNumerics\MikeSheTools\HydroNumerics.Nitrate.Model\config.xml"); target.Initialize(); target.Run(); target.Print(); }
public void LoadMikeSheDataTest() { MainModel target = new MainModel(); // TODO: Initialize to an appropriate value target.LoadCatchments(@"D:\DK_information\TestData\FileStructure\id15_NSTmodel.shp"); target.LoadMikeSheData(@"E:\dhi\data\dkm\dk2\result\DK2_v3_gvf_PT_100p_24hr.she"); using (System.IO.StreamWriter sw = new System.IO.StreamWriter(@"d:\temp\precip.csv")) { foreach (var c in target.AllCatchments.Values) { if (c.Precipitation == null) sw.WriteLine(c.ID + ",,"); else { foreach (var year in c.Precipitation.GetTs(TimeStepUnit.Year).Items) sw.WriteLine(c.ID + "," + year); } } } }
public void CreateLeachFile() { var gwsource = new GroundWaterSource(); gwsource.DaisyFiles.Add(new SafeFile(){FileName =@"D:\DK_information\TestData\FileStructure\DaisyLeaching\SoilFarms_dmi10kmgrid_daily1990.txt"}); gwsource.DaisyFiles.Add(new SafeFile() { FileName = @"D:\DK_information\TestData\FileStructure\DaisyLeaching\SoilFarms_dmi10kmgrid_daily1991.txt" }); gwsource.DaisyFiles.Add(new SafeFile() { FileName = @"D:\DK_information\TestData\FileStructure\DaisyLeaching\SoilFarms_dmi10kmgrid_daily1992.txt" }); gwsource.DaisyFiles.Add(new SafeFile() { FileName = @"D:\DK_information\TestData\FileStructure\DaisyLeaching\SoilFarms_dmi10kmgrid_daily1993.txt" }); gwsource.DaisyFiles.Add(new SafeFile() { FileName = @"D:\DK_information\TestData\FileStructure\DaisyLeaching\SoilFarms_dmi10kmgrid_daily1994.txt" }); gwsource.DaisyFiles.Add(new SafeFile() { FileName = @"D:\DK_information\TestData\FileStructure\DaisyLeaching\SoilFarms_dmi10kmgrid_daily1995.txt" }); gwsource.DaisyFiles.Add(new SafeFile() { FileName = @"D:\DK_information\TestData\FileStructure\DaisyLeaching\SoilFarms_dmi10kmgrid_daily1996.txt" }); gwsource.DaisyFiles.Add(new SafeFile() { FileName = @"D:\DK_information\TestData\FileStructure\DaisyLeaching\SoilFarms_dmi10kmgrid_daily1997.txt" }); gwsource.DaisyFiles.Add(new SafeFile() { FileName = @"D:\DK_information\TestData\FileStructure\DaisyLeaching\SoilFarms_dmi10kmgrid_daily1998.txt" }); gwsource.DaisyFiles.Add(new SafeFile() { FileName = @"D:\DK_information\TestData\FileStructure\DaisyLeaching\SoilFarms_dmi10kmgrid_daily1999.txt" }); gwsource.DaisyFiles.Add(new SafeFile() { FileName = @"D:\DK_information\TestData\FileStructure\DaisyLeaching\SoilFarms_dmi10kmgrid_daily2000.txt" }); gwsource.DaisyFiles.Add(new SafeFile() { FileName = @"D:\DK_information\TestData\FileStructure\DaisyLeaching\SoilFarms_dmi10kmgrid_daily2001.txt" }); gwsource.DaisyFiles.Add(new SafeFile() { FileName = @"D:\DK_information\TestData\FileStructure\DaisyLeaching\SoilFarms_dmi10kmgrid_daily2002.txt" }); gwsource.DaisyFiles.Add(new SafeFile() { FileName = @"D:\DK_information\TestData\FileStructure\DaisyLeaching\SoilFarms_dmi10kmgrid_daily2003.txt" }); gwsource.DaisyFiles.Add(new SafeFile() { FileName = @"D:\DK_information\TestData\FileStructure\DaisyLeaching\SoilFarms_dmi10kmgrid_daily2004.txt" }); gwsource.DaisyFiles.Add(new SafeFile() { FileName = @"D:\DK_information\TestData\FileStructure\DaisyLeaching\SoilFarms_dmi10kmgrid_daily2005.txt" }); gwsource.DaisyFiles.Add(new SafeFile() { FileName = @"D:\DK_information\TestData\FileStructure\DaisyLeaching\SoilFarms_dmi10kmgrid_daily2006.txt" }); gwsource.DaisyFiles.Add(new SafeFile() { FileName = @"D:\DK_information\TestData\FileStructure\DaisyLeaching\SoilFarms_dmi10kmgrid_daily2007.txt" }); gwsource.DaisyFiles.Add(new SafeFile() { FileName = @"D:\DK_information\TestData\FileStructure\DaisyLeaching\SoilFarms_dmi10kmgrid_daily2008.txt" }); gwsource.DaisyFiles.Add(new SafeFile() { FileName = @"D:\DK_information\TestData\FileStructure\DaisyLeaching\SoilFarms_dmi10kmgrid_daily2009.txt" }); gwsource.DaisyFiles.Add(new SafeFile() { FileName = @"D:\DK_information\TestData\FileStructure\DaisyLeaching\SoilFarms_dmi10kmgrid_daily2010.txt" }); gwsource.DaisyFiles.Add(new SafeFile() { FileName = @"D:\DK_information\TestData\FileStructure\DaisyLeaching\SoilFarms_dmi10kmgrid_daily2011.txt" }); gwsource.SoilCodes = new SafeFile() { FileName = @"D:\DK_information\TestData\FileStructure\DaisyLeaching\DKDomainNodes_LU_Soil_codes.shp" }; gwsource.ParticleFiles.Add(new SafeFile() { FileName = @"D:\DK_information\TestData\FileStructure\Particles\PTReg_Extraction_1_20131007_dk2.shp" }); gwsource.ParticleFiles.Last().Parameters.Add(100); MainModel mv = new MainModel(); mv.LoadCatchments(@"D:\DK_information\TestData\FileStructure\id15_NSTmodel.shp"); Stopwatch sw = new Stopwatch(); sw.Start(); gwsource.Initialize(new DateTime(1991, 1, 1), new DateTime(2010, 1, 1), mv.AllCatchments.Values); sw.Stop(); var ts = sw.Elapsed; //using (HydroNumerics.Geometry.Shapes.ShapeWriter sw = new Geometry.Shapes.ShapeWriter(@"D:\DK_information\TestData\leach1990MontlyPar")) //{ // System.Data.DataTable dt = new System.Data.DataTable(); // dt.Columns.Add("ID15", typeof(int)); // dt.Columns.Add("Januar", typeof(double)); // dt.Columns.Add("Februar", typeof(double)); // dt.Columns.Add("Marts", typeof(double)); // dt.Columns.Add("April", typeof(double)); // dt.Columns.Add("Maj", typeof(double)); // dt.Columns.Add("Juni", typeof(double)); // dt.Columns.Add("Juli", typeof(double)); // dt.Columns.Add("August", typeof(double)); // dt.Columns.Add("September", typeof(double)); // dt.Columns.Add("Oktober", typeof(double)); // dt.Columns.Add("November", typeof(double)); // dt.Columns.Add("December", typeof(double)); // foreach (var c in mv.AllCatchments.Values) // { // var dr = dt.NewRow(); // dr[0] = c.ID; // var data = CatchLeach[c.ID]; // for (int i = 0; i < 12; i++) // dr[i + 1] = data[i]; // sw.Write(new HydroNumerics.Geometry.GeoRefData() { Geometry = c.Geometry, Data = dr }); // } //} //using (HydroNumerics.Geometry.Shapes.ShapeWriter sw = new Geometry.Shapes.ShapeWriter(@"D:\DK_information\TestData\leachYearlyscaledPar")) //{ // System.Data.DataTable dt = new System.Data.DataTable(); // dt.Columns.Add("ID15", typeof(int)); // for (int i= Start.Year; i<= End.Year;i++) // { // dt.Columns.Add(i.ToString(), typeof(double)); // } // foreach (var c in mv.AllCatchments.Values) // { // var dr = dt.NewRow(); // dr[0] = c.ID; // var data = CatchLeach[c.ID]; // for (int i = 0; i < End.Year - Start.Year; i++) // dr[i + 1] = data.Skip(i * 12).Take(12).Sum()/((HydroNumerics.Geometry.IXYPolygon)c.Geometry).GetArea(); // sw.Write(new HydroNumerics.Geometry.GeoRefData() { Geometry = c.Geometry, Data = dr }); // } //} //double sum = CatchLeach.Values.Sum(c=>c.Sum(v=>v)); }