void Controller_ControllerEvent(object sender, ControllerEventArgs e) { // When model is prepared, we can connect to the HD Module if (e.State == ControllerState.Prepared) { // We are done with the ControllerEvent, so unregister controller.ControllerEvent -= Controller_ControllerEvent; // Register for event triggered every time step, where the weir coefficient will be updated controller.EngineNet.PostTimeStepEvent += UpdateWeirCoefficients; // Find upstream H gridpoint, where to extract the water level. ProxyUtil proxy = new ProxyUtil(controller.EngineNet); for (int i = 0; i < _weirCoefficients.Count; i++) { HonmaWeir weir = _weirCoefficients[i].Weir; // Find location of weir, and upstream reach and grid point - for a water level getter we need the H grid point // This is chainage-upstream and not flow-upstream. EngineReach engineReach = controller.EngineNet.FindReach(weir.Location); if (engineReach == null) { continue; // throw error? } GridPoint gp = engineReach.GetClosestUpstreamGridPoint(weir.Location.Chainage, gpm => gpm is HGridPoint, false); if (gp == null) { continue; // throw error? } // Extract a getter which will return the current water level _weirCoefficients[i].Getter = proxy.Getter(engineReach, gp.PointIndex, Quantity.Create(PredefinedQuantity.WaterLevel)); } } }
/// <summary> /// Creates an <see cref="IEngineDataItem{T}"/> that stores one value on the gridpoint /// where the structure is located. /// Adds this <see cref="IEngineDataItem{T}"/> to the <see cref="DataModule"/> for supporting outputting. /// Returns an <see cref="IEngineDataReach{T}"/> holding the value to update every timestep. /// </summary> private EngineDataReach <double> CreateEngineDataReach(Quantity quantity, EngineReach engineReach, GridPoint gridPoint) { // Grid point index where PowerPlant data belongs - required to get output right int[] indexList = new int[] { gridPoint.PointIndex }; // EngineDataItem that stores data for a single PowerPlant and transfers data to result file var engineData = new EngineDataItemAll <double>(_controller.EngineNet, quantity) { ReachesData = new IEngineDataReach <double> [_controller.EngineNet.Reaches.Count] }; // Add data items to data module _controller.EngineNet.DataModule.AddDataItem(engineData); // Data for this reach, containing one value at the structure grid point var engineDataReach = new EngineDataReach <double>() { Values = new double[indexList.Length], IndexList = indexList, }; // Register reach data with dataitem engineData.ReachesData[engineReach.ReachListIndex] = engineDataReach; return(engineDataReach); }
private void Initialize() { _powerPlants = new List <PowerPlantData>(); // Proxy, used for getting access to engine variables ProxyUtil proxy = new ProxyUtil(_controller.EngineNet); // Quantities Quantity powerQuantity = new Quantity("EnergyPower", "Energy Power", eumItem.eumIPower, eumUnit.eumUkwatt); Quantity energyQuantity = new Quantity("EnergyProduced", "Energy produced", eumItem.eumIenergy, eumUnit.eumUKiloWattHour); // Add quantities to default HD results ResultSpecification hdResults = _controller.Mike1DData.ResultSpecifications.Find(rs => rs.ID == "DefaultHDResults"); hdResults.What.Add(powerQuantity); hdResults.What.Add(energyQuantity); // Setup all powerplants foreach (string powerPlantStructureId in _powerPlantStructureIds) { // Find structure with Id - returns null if not found IStructure structure = _controller.Mike1DData.Network.StructureCollection.Structures.FirstOrDefault( s => StringComparer.OrdinalIgnoreCase.Equals(s.ID, powerPlantStructureId)); // Only applicable for Discharge structures DischargeStructure dischargeStructure = structure as DischargeStructure; // null if structure was not an IDischargeStructure, or powerPlantStructureId was not found if (dischargeStructure == null) { continue; } // Find reach and grid point of structure ILocation location = dischargeStructure.Location; EngineReach engineReach = _controller.EngineNet.FindReach(location); GridPoint gridPoint = engineReach.GetClosestGridPoint <GridPoint>(dischargeStructure.Location); // Getters for waterlevels upstream and downstream of structure. DDoubleGetter upWl = proxy.GetterUnboxed(engineReach, gridPoint.PointIndex - 1, Quantity.Create(PredefinedQuantity.WaterLevel)); DDoubleGetter doWl = proxy.GetterUnboxed(engineReach, gridPoint.PointIndex + 1, Quantity.Create(PredefinedQuantity.WaterLevel)); // Data for this reach. Id of structure is added to description of quantity var powerDataReach = CreateEngineDataReach(CreateIdQuantity(powerQuantity, structure.ID), engineReach, gridPoint); var energyDataReach = CreateEngineDataReach(CreateIdQuantity(energyQuantity, structure.ID), engineReach, gridPoint); PowerPlantData powerPlantData = new PowerPlantData(); powerPlantData.Structure = dischargeStructure; powerPlantData.UpWl = upWl; powerPlantData.DoWl = doWl; powerPlantData.PowerData = powerDataReach; powerPlantData.EnergyData = energyDataReach; _powerPlants.Add(powerPlantData); } _controller.EngineNet.PostTimeStepEvent += PostTimestep; }
private void AddLocation(EngineReach engineReach, LocationSpan locationsSpan) { var hdReach = _engineNet.HDModule.GetReach(engineReach); if (hdReach == null) { return; } var useAllReach = locationsSpan.StartChainage == Constants.DOUBLE_DELETE_VALUE || locationsSpan.EndChainage == Constants.DOUBLE_DELETE_VALUE; var location = new BedResistanceLocation() { Reach = hdReach }; // Bed resistance toolbox modifies the resistance only on the H grid points. foreach (var hdGridPoint in hdReach.GridPoints) { var hdhGridPoint = hdGridPoint as IHDHGridPoint; if (hdhGridPoint == null) { continue; } if (!locationsSpan.ContainsChainage(hdhGridPoint.GridPoint.Location.Chainage) && !useAllReach) { continue; } location.GridPoints.Add(hdhGridPoint); } if (location.GridPoints.Count > 0) { _locations.Add(location); } }