private static void ConnectItems(ITimeSpaceComponent sourceComponentInstance, string outputItemId, ITimeSpaceComponent targetComponentInstance, string inputItemId) { ITimeSpaceOutput output = FindOutputItem(sourceComponentInstance, outputItemId); ITimeSpaceInput input = FindInputItem(targetComponentInstance, inputItemId); output.AddConsumer(input); }
/// <summary> /// Connect the output defined by the <paramref name="outputId"/> with the input /// defined by the <paramref name="inputId"/>, and put the adaptors /// listed by the id's <paramref name="adaptorIds"/> in between. /// <para> /// If any of the adaptors needs custom parameters, this method can not be used. /// </para> /// </summary> /// <param name="outputComponent">Component that has the output</param> /// <param name="outputId">Id of output</param> /// <param name="inputComponent">Component that has the input</param> /// <param name="inputId">Id of input</param> /// <param name="adaptorIds">Id of adaptors. They must be available from the output component</param> /// <param name="scale">If scale is different from 0, a linear operation adaptor is also added.</param> public static void Connect(ITimeSpaceComponent outputComponent, string outputId, ITimeSpaceComponent inputComponent, string inputId, IList <string> adaptorIds, double scale) { ITimeSpaceInput input = inputComponent.FindInputItem(inputId); ITimeSpaceOutput output = outputComponent.FindOutputItem(outputId); // End point of connection ITimeSpaceOutput leaf = output; // Add all adaptors foreach (string adaptorID in adaptorIds) { ITimeSpaceAdaptedOutput adaptor = outputComponent.FindAdaptor(adaptorID, leaf, input); if (adaptor == null) { throw new Exception("Coult not find output adaptor with id: " + adaptorID); } adaptor.Initialize(); leaf = adaptor; } // Add a linear conversion adaptor if (scale != 0) { ITimeSpaceAdaptedOutput adaptor = outputComponent.FindAdaptor("LinearOperation", leaf, input); if (adaptor == null) { throw new Exception("Coult not find output linear conversion adaptor"); } adaptor.Arguments[0].Value = scale; adaptor.Initialize(); leaf = adaptor; } // Connect leaf adaptor/output to input leaf.AddConsumer(input); }
public void GetValuesOnCfOutputItem() { // create the component // connect query and output item // take care that component becomes valid and produces initial output for connected items ITimeSpaceComponent cf = RainRrCfComponents.CreateChannelFlowInstance("CF-2"); ITimeSpaceOutput selectedOutput = RainRrCfCompositions.FindOutputItem(cf, "node-4.discharge"); selectedOutput.AddConsumer(_dischargeQueryItem); cf.Validate(); // check initial values Assert.AreEqual(7000.0, (double)selectedOutput.Values.Values2D[0][0], "Value[0] as property"); // set query time for getting values _dischargeQueryItem.TimeSet.SetSingleTimeStamp(new DateTime(2009, 3, 28, 12, 0, 0)); ITimeSpaceValueSet values = selectedOutput.GetValues(_dischargeQueryItem); Assert.IsNotNull(values, "values != null"); Assert.AreEqual(7001.5, (double)values.Values2D[0][0], "value[0] from GetValues 1"); _dischargeQueryItem.TimeSet.SetSingleTimeStamp(new DateTime(2009, 3, 29, 0, 0, 0)); values = selectedOutput.GetValues(_dischargeQueryItem); Assert.IsNotNull(values, "values != null"); Assert.AreEqual(7002.0, (double)values.Values2D[0][0], "value[0] from GetValues 1"); try { _dischargeQueryItem.TimeSet.SetSingleTimeStamp(new DateTime(2009, 3, 30, 0, 0, 0)); selectedOutput.GetValues(_dischargeQueryItem); } catch (Exception e) { Assert.IsTrue(e.Message.StartsWith("Could not update engine \"")); } }
public void GetValuesFromGwModel() { Quantity dischargeQuantity = new Quantity(new Unit(PredefinedUnits.CubicMeterPerSecond), null, "Discharge"); Quantity waterlevelQuantity = new Quantity(new Unit(PredefinedUnits.Meter), null, "Water Level"); ElementSet idBasedElementSetA = new ElementSet(null, "ElmSet-A", ElementType.IdBased); idBasedElementSetA.AddElement(new Element("elm-1")); Input queryItem1 = new Input("discharge, to be retrieved from some output item", dischargeQuantity, idBasedElementSetA); queryItem1.TimeSet = new TimeSet(); Input queryItem2 = new Input("water level, to be retrieved from some output item", waterlevelQuantity, idBasedElementSetA); queryItem2.TimeSet = new TimeSet(); // Connect query item(s) to output item(s) // Take care that component becomes valid (and has produced initial output for connected items) ITimeSpaceComponent gwModel = new GWModelLC(); gwModel.Initialize(); ITimeSpaceOutput storageOnGrid = UTHelper.FindOutputItem(gwModel, "Grid.Storage"); storageOnGrid.AddConsumer(queryItem1); gwModel.Validate(); Assert.IsTrue(gwModel.Status == LinkableComponentStatus.Valid); gwModel.Prepare(); Assert.IsTrue(gwModel.Status == LinkableComponentStatus.Updated); // check initial values Assert.AreEqual(4, ValueSet.GetElementCount(storageOnGrid.Values), "#values for " + storageOnGrid.Id); Assert.AreEqual(0.0, (double)storageOnGrid.Values.GetValue(0, 0), "Value[0] as property"); // get values for specified query times queryItem1.TimeSet.SetSingleTimeStamp(new DateTime(2005, 1, 3, 0, 0, 0)); ITimeSpaceValueSet values = storageOnGrid.GetValues(queryItem1); Assert.IsNotNull(values, "values != null"); Assert.AreEqual(0.0, (double)values.GetValue(0, 0), "value[0] from GetValues 1"); // set next query time queryItem1.TimeSet.SetSingleTimeStamp(new DateTime(2005, 2, 4, 0, 0, 0)); values = storageOnGrid.GetValues(queryItem1); Assert.IsNotNull(values, "values != null"); Assert.AreEqual(0.0, (double)values.GetValue(0, 0), "value[0] from GetValues 1"); // ask for same time again values = storageOnGrid.GetValues(queryItem1); Assert.IsNotNull(values, "values != null"); Assert.AreEqual(0.0, (double)values.GetValue(0, 0), "value[0] from GetValues 1"); try { // set query time back in time queryItem1.TimeSet.SetSingleTimeStamp(new DateTime(2005, 2, 3, 0, 0, 0)); storageOnGrid.GetValues(queryItem1); } catch (Exception e) { Assert.IsTrue(e.Message.StartsWith("Could not update engine \"")); } try { // set query time beyond time horizon queryItem1.TimeSet.SetSingleTimeStamp(new DateTime(2005, 2, 28, 0, 0, 0)); storageOnGrid.GetValues(queryItem1); } catch (Exception e) { Assert.IsTrue(e.Message.StartsWith("Could not update engine \"")); } }
public void InputOutputTests() { GWModelLC gwlc = new GWModelLC(); IBaseLinkableComponent lc = gwlc; List<IArgument> arguments = CreateTestArguments(); lc.Initialize(arguments); IElementSet elementSet = ((ITimeSpaceExchangeItem)lc.Inputs[2]).ElementSet(); Quantity dischargeQuantity = new Quantity(new Unit(PredefinedUnits.CubicMeterPerSecond), null, "Discharge"); Quantity waterlevelQuantity = new Quantity(new Unit(PredefinedUnits.Meter), null, "Water Level"); ElementSet idBasedElementSetA = new ElementSet(null, "ElmSet-A", ElementType.IdBased); idBasedElementSetA.AddElement(new Element("elm-1")); Input waterLevelTriggerInput = new Input("Water level, to be retrieved from some output item", waterlevelQuantity, elementSet); waterLevelTriggerInput.TimeSet = new TimeSet(); SimpleOutput dischargeOutput = new SimpleOutput("Discharge, to be sent to GW model", dischargeQuantity, elementSet); dischargeOutput.ConstOutput = 6; dischargeOutput.TimeSet = new TimeSet(); ITimeSpaceInput gwInflow = UTHelper.FindInputItem(lc, "Grid.Inflow"); ITimeSpaceOutput gwLevel = UTHelper.FindOutputItem(lc, "Grid.gwLevel"); // Connect discharge gwInflow.Provider = dischargeOutput; dischargeOutput.AddConsumer(gwInflow); // Connect triggering input gwLevel.AddConsumer(waterLevelTriggerInput); lc.Validate(); lc.Prepare(); // specify query times double firstTriggerGetValuesTime = gwLevel.TimeSet.Times[0].StampAsModifiedJulianDay; double secondTriggerGetValuesTime = firstTriggerGetValuesTime + 3; double thirdTriggerGetValuesTime = firstTriggerGetValuesTime + 5; // check initial values Assert.AreEqual(8, gwLevel.Values.Values2D[0].Count); Assert.AreEqual(-10, (double)gwLevel.Values.GetValue(0, 0)); ITimeSpaceValueSet values; // get values for initial time, therefor intial values waterLevelTriggerInput.TimeSet.SetSingleTime(new Time(firstTriggerGetValuesTime)); values = gwLevel.GetValues(waterLevelTriggerInput); Assert.AreEqual(-10, values.GetValue(0, 0), "value for first query time"); // get values for second query time: -10 : base gw level. // 10 : storageHeight is multiplied by a factor of 10 // 3 days, 6 constant inflow, 3600*24 seconds in a day, 1000 L/M3, (100 x 200) grid cell size waterLevelTriggerInput.TimeSet.SetSingleTime(new Time(secondTriggerGetValuesTime)); values = gwLevel.GetValues(waterLevelTriggerInput); Assert.AreEqual(-10.0 + 10 * 3 * 6.0 * 60 * 60 * 24 / 1000 / (100 * 200), values.GetValue(0, 0), "value for first query time"); // get values for second query time: // 10 : storageHeight is multiplied by a factor of 10 // 5 days, 6 constant inflow, 3600*24 seconds in a day, 1000 L/M3, (100 x 200) grid cell size waterLevelTriggerInput.TimeSet.SetSingleTime(new Time(thirdTriggerGetValuesTime)); values = gwLevel.GetValues(waterLevelTriggerInput); Assert.AreEqual(-10.0 + 10 * 5 * 6.0 * 60 * 60 * 24 / 1000 / (100 * 200), values.GetValue(0, 0), "value for first query time"); lc.Finish(); }
public void AddConsumer(IBaseInput consumer) { _adapted.AddConsumer(consumer); }
public void GetValues() { // Connect query item(s) to output item(s) // Take care that component becomes valid (and has produced initial output for connected items) ITimeSpaceComponent riverModel = CreateRiverModel(); List <IArgument> arguments = CreateRiverModelArguments(riverModel); arguments.Add(new ArgumentBool("flowItemsAsSpan", _flowItemsAsSpan)); riverModel.Arguments.ApplyArguments(arguments); riverModel.Initialize(); ITimeSpaceOutput flowOnBranch = UTHelper.FindOutputItem(riverModel, "Branch:2:Flow"); flowOnBranch.AddConsumer(_queryItem1); riverModel.Validate(); Assert.IsTrue(riverModel.Status == LinkableComponentStatus.Valid); riverModel.Prepare(); Assert.IsTrue(riverModel.Status == LinkableComponentStatus.Updated); // check initial values Assert.AreEqual(1, ValueSet.GetElementCount(flowOnBranch.Values), "#values for " + flowOnBranch.Id); Assert.AreEqual(7.0, (double)flowOnBranch.Values.GetValue(0, 0), "Value[0] as property"); // get values for specified query times _queryItem1.TimeSet.SetSingleTimeStamp(new DateTime(2005, 1, 3, 0, 0, 0)); ITimeSpaceValueSet values = flowOnBranch.GetValues(_queryItem1); Assert.IsNotNull(values, "values != null"); double flow3 = 35.0 / 4.0; // = 10 * (1.0 / 2.0 + 1.0 / 4.0 + 1.0 / 8.0) = 8.75 Assert.AreEqual(flow3, (double)values.GetValue(0, 0), "value[0] from GetValues 1"); // set next query time _queryItem1.TimeSet.SetSingleTimeStamp(new DateTime(2005, 2, 4, 0, 0, 0)); values = flowOnBranch.GetValues(_queryItem1); Assert.IsNotNull(values, "values != null"); flow3 = 10 * (1.0 / 2.0 + 1.0 / 4.0 + 1.0 / 8.0); // = 8.75 Assert.AreEqual(flow3, (double)values.GetValue(0, 0), "value[0] from GetValues 1"); // ask for same time again values = flowOnBranch.GetValues(_queryItem1); Assert.IsNotNull(values, "values != null"); Assert.AreEqual(flow3, (double)values.GetValue(0, 0), "value[0] from GetValues 1"); try { // set query time back in time _queryItem1.TimeSet.SetSingleTimeStamp(new DateTime(2005, 2, 3, 0, 0, 0)); flowOnBranch.GetValues(_queryItem1); } catch (Exception e) { Assert.IsTrue(e.Message.StartsWith("Could not update engine \"")); } try { // set query time beyond time horizon _queryItem1.TimeSet.SetSingleTimeStamp(new DateTime(2005, 2, 28, 0, 0, 0)); flowOnBranch.GetValues(_queryItem1); } catch (Exception e) { Assert.IsTrue(e.Message.StartsWith("Could not update engine \"")); } }