Esempio n. 1
0
		public void IsBefore()
		{
			TimeStamp t_4 = new TimeStamp(4);
			TimeStamp t_7 = new TimeStamp(7);
            Assert.AreEqual(true, global::HydroNumerics.OpenMI.Sdk.Buffer.Support.IsBefore(t_4, t_7));
            Assert.AreEqual(false, global::HydroNumerics.OpenMI.Sdk.Buffer.Support.IsBefore(t_7, t_4));
            Assert.AreEqual(false, global::HydroNumerics.OpenMI.Sdk.Buffer.Support.IsBefore(t_4, t_4));

            HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan t_3_5 = new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(3), new TimeStamp(5));
            HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan t_4_6 = new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(4), new TimeStamp(6));
            HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan t_5_7 = new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(5), new TimeStamp(7));
            HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan t_6_8 = new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(6), new TimeStamp(8));

            Assert.AreEqual(true, global::HydroNumerics.OpenMI.Sdk.Buffer.Support.IsBefore(t_3_5, t_6_8));
            Assert.AreEqual(false, global::HydroNumerics.OpenMI.Sdk.Buffer.Support.IsBefore(t_6_8, t_3_5));
            Assert.AreEqual(false, global::HydroNumerics.OpenMI.Sdk.Buffer.Support.IsBefore(t_3_5, t_4_6));
            Assert.AreEqual(false, global::HydroNumerics.OpenMI.Sdk.Buffer.Support.IsBefore(t_3_5, t_5_7));
            Assert.AreEqual(false, global::HydroNumerics.OpenMI.Sdk.Buffer.Support.IsBefore(t_3_5, t_3_5));

            Assert.AreEqual(true, global::HydroNumerics.OpenMI.Sdk.Buffer.Support.IsBefore(t_4, t_5_7));
            Assert.AreEqual(false, global::HydroNumerics.OpenMI.Sdk.Buffer.Support.IsBefore(t_4, t_3_5));
            Assert.AreEqual(false, global::HydroNumerics.OpenMI.Sdk.Buffer.Support.IsBefore(t_7, t_3_5));
            Assert.AreEqual(false, global::HydroNumerics.OpenMI.Sdk.Buffer.Support.IsBefore(t_4, t_4_6));
            Assert.AreEqual(false, global::HydroNumerics.OpenMI.Sdk.Buffer.Support.IsBefore(t_7, t_5_7));

            Assert.AreEqual(true, global::HydroNumerics.OpenMI.Sdk.Buffer.Support.IsBefore(t_3_5, t_7));
            Assert.AreEqual(false, global::HydroNumerics.OpenMI.Sdk.Buffer.Support.IsBefore(t_3_5, t_4));
            Assert.AreEqual(false, global::HydroNumerics.OpenMI.Sdk.Buffer.Support.IsBefore(t_6_8, t_4));
            Assert.AreEqual(false, global::HydroNumerics.OpenMI.Sdk.Buffer.Support.IsBefore(t_5_7, t_7));

		}
Esempio n. 2
0
		public ITimeSpan GetTimeHorizon()
		{
			TimeStamp startTime = new TimeStamp(HydroNumerics.OpenMI.Sdk.DevelopmentSupport.CalendarConverter.Gregorian2ModifiedJulian(_simulationStart));
			TimeStamp endTime   = new TimeStamp(HydroNumerics.OpenMI.Sdk.DevelopmentSupport.CalendarConverter.Gregorian2ModifiedJulian(_simulationEnd));
			HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan timeHorizon = new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(startTime,endTime);
			return timeHorizon;
		}
Esempio n. 3
0
		public void Constructor()
		{
			TimeStamp timeStamp2 = new TimeStamp(timeStamp);
			Assert.AreEqual(timeStamp,timeStamp2);

            TimeStamp timeStamp3 = new TimeStamp(new DateTime(1858, 11, 17)); // the zero Julian time
            Assert.AreEqual(0.0, timeStamp3.ModifiedJulianDay);
		}
Esempio n. 4
0
 /// <summary>
 /// Invokes <see cref="ILinkableComponent.GetValues">ILinkableComponent.GetValues</see>
 /// method on model linked to this trigger, if any.
 /// </summary>
 /// <param name="runToTime">Time for <see cref="ILinkableComponent.GetValues">ILinkableComponent.GetValues</see> call.</param>
 public void Run(HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp runToTime)
 {
     if (_link != null)
     {
         _earliestInputTime = runToTime;
         _link.SourceComponent.GetValues(runToTime, _link.ID);
     }
 }
Esempio n. 5
0
		public void CompareTo()
		{
			TimeStamp timeStamp1 = new TimeStamp(12345.3);
			Assert.AreEqual(0.0,timeStamp.CompareTo(timeStamp1));
			timeStamp1.ModifiedJulianDay = 10000;
			Assert.IsTrue(timeStamp.CompareTo(timeStamp1)>0.0);
			Assert.IsTrue(timeStamp1.CompareTo(timeStamp)<0.0);
		}
        public void TimeHorizon()
        {
            LinkableTimeSeriesGroup linkableTimeSeriesGroup = new LinkableTimeSeriesGroup();

            linkableTimeSeriesGroup.Initialize(arguments);
            System.DateTime start = new HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp(linkableTimeSeriesGroup.TimeHorizon.Start).ToDateTime();
            System.DateTime end   = new HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp(linkableTimeSeriesGroup.TimeHorizon.End).ToDateTime();
            Assert.AreEqual(new System.DateTime(2010, 1, 1), start);
            Assert.AreEqual(new System.DateTime(2010, 1, 11), end);
        }
Esempio n. 7
0
		public void Equals()
		{
			TimeStamp timeStamp1 = new TimeStamp(12345.3);
			Assert.IsTrue(timeStamp.Equals(timeStamp1));
			timeStamp1.ModifiedJulianDay = 34.0;
			Assert.IsFalse(timeStamp.Equals(timeStamp1));

			Assert.IsFalse(timeStamp.Equals(null));
			Assert.IsFalse(timeStamp.Equals("string"));
		}
Esempio n. 8
0
 public global::OpenMI.Standard.ITimeSpan GetTimeHorizon()
 {
     //HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp startTime = new HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp(model.CurrentTime);
     //HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp endTime = new HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp(model.EndTime);
     //OpenMI times cannot handle the DateTime.MinValue and the DateTime.MaxValue so the timehorizon is simply made large (see below).
     HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp startTime   = new HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp(new DateTime(1900, 1, 1));
     HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp endTime     = new HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp(new DateTime(2100, 1, 1));
     HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan  timeHorizon = new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(startTime, endTime);
     return(timeHorizon);
 }
 private string ITimeToString(ITime time)
 {
     if (time is ITimeSpan)
     {
         DateTime startTime = new HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp(((ITimeSpan)time).Start).ToDateTime();
         DateTime endTime   = new HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp(((ITimeSpan)time).End).ToDateTime();
         return("(" + startTime.ToLongDateString() + ":" + startTime.ToLongTimeString() + ", " + endTime.ToLongDateString() + ":" + endTime.ToLongTimeString());
     }
     else
     {
         DateTime dateTime = new HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp((ITimeStamp)time).ToDateTime();
         return("(" + dateTime.ToLongDateString() + ":" + dateTime.ToLongTimeString() + ")");
     }
 }
Esempio n. 10
0
        /// <summary>
        /// Convert a ITime object to a ITimeStamp.
        /// </summary>
        /// <param name="time">The ITime object to convert</param>
        /// <returns>The converted time</returns>
        public static HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp TimeToTimeStamp(ITime time)
        {
            HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp t;

            if (time is ITimeStamp)
            {
                t = new HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp(((ITimeStamp)time).ModifiedJulianDay);
            }
            else
            {
                t = new HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp(((ITimeSpan)time).End.ModifiedJulianDay);
            }

            return(t);
        }
		public void GetValues()
		{
			HydroNumerics.OpenMI.Sdk.Wrapper.UnitTest.Trigger trigger = new HydroNumerics.OpenMI.Sdk.Wrapper.UnitTest.Trigger();
			TimeSeriesComponent ts = new TimeSeriesComponent();

			ts.Initialize(new Argument[0]);
			Link triggerLink = new Link();

			triggerLink.ID				 = "TriggerLink";
			triggerLink.SourceComponent  = ts;
			triggerLink.SourceElementSet = ts.GetOutputExchangeItem(0).ElementSet;
			triggerLink.SourceQuantity   = ts.GetOutputExchangeItem(0).Quantity;
			triggerLink.TargetComponent  = trigger;
			triggerLink.TargetElementSet = trigger.GetInputExchangeItem(0).ElementSet;
			triggerLink.TargetQuantity   = trigger.GetInputExchangeItem(0).Quantity;

			trigger.AddLink(triggerLink);
			ts.AddLink(triggerLink);

			double tt = ts.TimeHorizon.Start.ModifiedJulianDay;
			TimeStamp[] triggerTimes = new TimeStamp[4];
			triggerTimes[0] = new TimeStamp(tt + 0.5);
			triggerTimes[1] = new TimeStamp(tt + 1.5);
			triggerTimes[2] = new TimeStamp(tt + 1.9);
			triggerTimes[3] = new TimeStamp(tt + 2.1);

			trigger.Run(triggerTimes);
			Assert.AreEqual(0,((IScalarSet)trigger.ResultsBuffer.GetValuesAt(0)).GetScalar(0));
			Assert.AreEqual(1,((IScalarSet)trigger.ResultsBuffer.GetValuesAt(1)).GetScalar(0));
			Assert.AreEqual(1,((IScalarSet)trigger.ResultsBuffer.GetValuesAt(2)).GetScalar(0));
			Assert.AreEqual(2,((IScalarSet)trigger.ResultsBuffer.GetValuesAt(3)).GetScalar(0));

			//Teting with timespans
			Assert.AreEqual(2.0,((ScalarSet) ts.GetValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(tt+2),new TimeStamp(tt + 2.0 + 1.0/24.0)),"TriggerLink")).GetScalar(0));
			Assert.AreEqual(2.0,((ScalarSet) ts.GetValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(tt + 2.0 + 1.0/24.0),new TimeStamp(tt + 2.0 + 2.0/24.0)),"TriggerLink")).GetScalar(0));
			Assert.AreEqual(2.0,((ScalarSet) ts.GetValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(tt + 2.0 + 2.0/24.0),new TimeStamp(tt + 2.0 + 3.0/24.0)),"TriggerLink")).GetScalar(0));
			Assert.AreEqual(2.0,((ScalarSet) ts.GetValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(tt + 2.0 + 5.0/24.0),new TimeStamp(tt + 2.0 + 6.0/24.0)),"TriggerLink")).GetScalar(0));
			Assert.AreEqual(2.0,((ScalarSet) ts.GetValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(tt + 2.0 + 15.0/24.0),new TimeStamp(tt + 2.0 + 16.0/24.0)),"TriggerLink")).GetScalar(0));
			Assert.AreEqual(2.0,((ScalarSet) ts.GetValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(tt + 2.0 + 23.0/24.0),new TimeStamp(tt + 2.0 + 24.0/24.0)),"TriggerLink")).GetScalar(0));
			Assert.AreEqual(3.0,((ScalarSet) ts.GetValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(tt + 3.0 ),new TimeStamp(tt + 3.0 + 1.0/24.0)),"TriggerLink")).GetScalar(0));
		
		}
//		[Ignore ("This test fails when dt = 1 hour but works for dt = 0.5 day")]
		public void GetValues2C()
		{
			// This test is a variation of GetValues 2B. What makes this test different is that the the two
			// connected river models are not using the same timesteps.
			// This test will test:
			// - is the buffer working correcly with respect to interpolations and buffering
			// - is the buffer working correctly with respect to clearing the buffer.
			
			try
			{
				ILinkableComponent timeSeries = new TimeSeriesComponent();
				ILinkableComponent upperRiver = new RiverModelLC();
				ILinkableComponent lowerRiver = new RiverModelLC();
				HydroNumerics.OpenMI.Sdk.Wrapper.UnitTest.Trigger trigger     = new HydroNumerics.OpenMI.Sdk.Wrapper.UnitTest.Trigger();

				timeSeries.Initialize(new Argument[0]);

				// The ModelID is passes in ordet to make it easier to debug, otherwise you cannot se the difference between the two istances of RiverModelLC
				Argument[] upperRiverArguments = new Argument[2];
				upperRiverArguments[0] = new Argument("ModelID","upperRiverModel",true,"argument");
				upperRiverArguments[1] = new Argument("TimeStepLength","21600",true,"A time step length of 1 day");
				upperRiver.Initialize(upperRiverArguments);

				// The ModelID is passes in ordet to make it easier to debug, otherwise you cannot se the difference between the two istances of RiverModelLC
				Argument[] lowerRiverArguments = new Argument[2];
				lowerRiverArguments[0] = new Argument("ModelID","lowerRiverModel",true,"argument");
				lowerRiverArguments[1] = new Argument("TimeStepLength","86400",true,"xx");
				lowerRiver.Initialize(lowerRiverArguments);
				trigger.Initialize(new Argument[0]);

				Assert.AreEqual("upperRiverModel",upperRiver.ModelID);
				Assert.AreEqual("lowerRiverModel",lowerRiver.ModelID);

				Link timeSeriesToUpperRiverLink = new Link();
				timeSeriesToUpperRiverLink.ID = "timeSeriesToUpperRiverLink";
				timeSeriesToUpperRiverLink.SourceComponent  = timeSeries;
				timeSeriesToUpperRiverLink.SourceElementSet = timeSeries.GetOutputExchangeItem(0).ElementSet; //last branch in the river
				timeSeriesToUpperRiverLink.SourceQuantity   = timeSeries.GetOutputExchangeItem(0).Quantity;
				timeSeriesToUpperRiverLink.TargetComponent  = upperRiver;
				timeSeriesToUpperRiverLink.TargetElementSet = upperRiver.GetInputExchangeItem(0).ElementSet;  //first node in the river
				timeSeriesToUpperRiverLink.TargetQuantity   = upperRiver.GetInputExchangeItem(0).Quantity;
				
				Link link = new Link();
				link.ID = "RiverToRiverLink";
				link.SourceComponent  = upperRiver;
				link.SourceElementSet = upperRiver.GetOutputExchangeItem(2).ElementSet; //last branch in the river
				link.SourceQuantity   = upperRiver.GetOutputExchangeItem(2).Quantity;
				link.TargetComponent  = lowerRiver;
				link.TargetElementSet = lowerRiver.GetInputExchangeItem(0).ElementSet;  //first node in the river
				link.TargetQuantity   = lowerRiver.GetInputExchangeItem(0).Quantity;

				Link triggerLink = new Link();

				triggerLink.ID				 = "TriggerLink";
				triggerLink.SourceComponent  = lowerRiver;
				triggerLink.SourceElementSet = lowerRiver.GetOutputExchangeItem(2).ElementSet;
				triggerLink.SourceQuantity   = lowerRiver.GetOutputExchangeItem(2).Quantity;
				triggerLink.TargetComponent  = trigger;
				triggerLink.TargetElementSet = trigger.GetInputExchangeItem(0).ElementSet;
				triggerLink.TargetQuantity   = trigger.GetInputExchangeItem(0).Quantity;
		
				timeSeries.AddLink(timeSeriesToUpperRiverLink);
				upperRiver.AddLink(timeSeriesToUpperRiverLink);

				upperRiver.AddLink(link);
				lowerRiver.AddLink(link);

				lowerRiver.AddLink(triggerLink);
				trigger.AddLink(triggerLink);
				
				timeSeries.Prepare();
				upperRiver.Prepare();
				lowerRiver.Prepare();
				trigger.Prepare();

				double firstTriggerGetValuesTime = lowerRiver.TimeHorizon.Start.ModifiedJulianDay;
				TimeStamp[] triggerTimes = new TimeStamp[2];
				triggerTimes[0] = new TimeStamp(firstTriggerGetValuesTime + 12.5);
				triggerTimes[1] = new TimeStamp(firstTriggerGetValuesTime + 16.2);

				trigger.Run(triggerTimes);

				double x1 = ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(0)).GetScalar(0);
				Assert.AreEqual(315.0/32.0 + 13.0/64.0,x1);

				double x2 = ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(1)).GetScalar(0);
				Assert.AreEqual(315.0/32.0 + 17.0/64.0,x2);

				Assert.AreEqual(10,((RiverModelLC) upperRiver)._maxBufferSize); //Test that the buffer is cleared

				upperRiver.Finish();
				lowerRiver.Finish();

				upperRiver.Dispose();
				lowerRiver.Dispose();

			}
			catch (System.Exception e)
			{
				ExceptionHandler.WriteException(e);
				throw (e);
			}
		}
Esempio n. 13
0
 /// <summary>
 /// return a string with date, time and julian time for both start and end time
 /// </summary>
 /// <returns></returns>
 public string ToLongString()
 {
     string str = "(ITimeSpan) From: ";
     str += new TimeStamp(_start).ToLongString();
     str += " To: ";
     str += new TimeStamp(_end).ToLongString();
     return str;
 }
Esempio n. 14
0
		/// <summary>
		/// Copy constructor
		/// </summary>
		/// <param name="source">The time span to copy</param>
		public TimeSpan(ITimeSpan source)
		{
			Start = new TimeStamp(source.Start);
			End = new TimeStamp(source.End);
		}
Esempio n. 15
0
		public void AddValues_01()
		{
			try
			{
				SmartBuffer smartBuffer = new SmartBuffer();
				smartBuffer.DoExtendedDataVerification = true;
				
				ScalarSet scalarSet = new ScalarSet(new double[3] { 0, 1, 2 });
				TimeStamp timeStamp = new TimeStamp(1);

				smartBuffer.AddValues(timeStamp, scalarSet);

				timeStamp.ModifiedJulianDay = 2;
				scalarSet.data[0] = 10;
				scalarSet.data[1] = 11;
				scalarSet.data[2] = 12;

				smartBuffer.AddValues(timeStamp, scalarSet);

				smartBuffer.AddValues(new TimeStamp(3), new ScalarSet(new double[3] { 110, 111, 112 }));
				smartBuffer.AddValues(new TimeStamp(4), new ScalarSet(new double[3] { 1110, 1111, 1112 }));

				Assert.AreEqual(0,((IScalarSet) smartBuffer.GetValuesAt(0)).GetScalar(0));
				Assert.AreEqual(1,((IScalarSet) smartBuffer.GetValuesAt(0)).GetScalar(1));
				Assert.AreEqual(2,((IScalarSet) smartBuffer.GetValuesAt(0)).GetScalar(2));

				Assert.AreEqual(10,((IScalarSet) smartBuffer.GetValuesAt(1)).GetScalar(0));
				Assert.AreEqual(11,((IScalarSet) smartBuffer.GetValuesAt(1)).GetScalar(1));
				Assert.AreEqual(12,((IScalarSet) smartBuffer.GetValuesAt(1)).GetScalar(2));

				Assert.AreEqual(110,((IScalarSet) smartBuffer.GetValuesAt(2)).GetScalar(0));
				Assert.AreEqual(111,((IScalarSet) smartBuffer.GetValuesAt(2)).GetScalar(1));
				Assert.AreEqual(112,((IScalarSet) smartBuffer.GetValuesAt(2)).GetScalar(2));

				Assert.AreEqual(1110,((IScalarSet) smartBuffer.GetValuesAt(3)).GetScalar(0));
				Assert.AreEqual(1111,((IScalarSet) smartBuffer.GetValuesAt(3)).GetScalar(1));
				Assert.AreEqual(1112,((IScalarSet) smartBuffer.GetValuesAt(3)).GetScalar(2));

				Assert.AreEqual(4,smartBuffer.TimesCount);
				Assert.AreEqual(3,smartBuffer.ValuesCount);
			}
			catch (System.Exception e)
			{
                //WriteException(e);
				throw (e);
			}
		}
Esempio n. 16
0
		public void ClearAfter()
		{
			SmartBuffer smartBuffer = new SmartBuffer();
			smartBuffer.DoExtendedDataVerification = true;

			// --Populate the SmartBuffer --
			smartBuffer.AddValues(new TimeStamp(10), new ScalarSet(new double[2] {11, 21}));
			smartBuffer.AddValues(new TimeStamp(13), new ScalarSet(new double[2] {12 ,22}));
			smartBuffer.AddValues(new TimeStamp(16), new ScalarSet(new double[2] {13 ,23}));
			smartBuffer.AddValues(new TimeStamp(20), new ScalarSet(new double[2] {14 ,24}));
			smartBuffer.AddValues(new TimeStamp(27), new ScalarSet(new double[2] {15 ,25}));
			smartBuffer.AddValues(new TimeStamp(30), new ScalarSet(new double[2] {16, 26}));
			smartBuffer.AddValues(new TimeStamp(48), new ScalarSet(new double[2] {17, 27}));

            HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp time = new HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp();

			time.ModifiedJulianDay = 50; // this time is after the last time in buffer
			smartBuffer.ClearAfter(time);
			Assert.AreEqual(7,smartBuffer.TimesCount);  //nothing removed
			Assert.AreEqual(11,((IScalarSet) smartBuffer.GetValuesAt(0)).GetScalar(0));
			Assert.AreEqual(27,((IScalarSet) smartBuffer.GetValuesAt(6)).GetScalar(1));

			time.ModifiedJulianDay = 30;
			smartBuffer.ClearAfter(time);
			Assert.AreEqual(5,smartBuffer.TimesCount);
			Assert.AreEqual(11,((IScalarSet) smartBuffer.GetValuesAt(0)).GetScalar(0));
			Assert.AreEqual(25,((IScalarSet) smartBuffer.GetValuesAt(4)).GetScalar(1));

			time.ModifiedJulianDay = 16.5;
			smartBuffer.ClearAfter(time);
			Assert.AreEqual(3,smartBuffer.TimesCount);
			Assert.AreEqual(11,((IScalarSet) smartBuffer.GetValuesAt(0)).GetScalar(0));
			Assert.AreEqual(23,((IScalarSet) smartBuffer.GetValuesAt(2)).GetScalar(1));

			time.ModifiedJulianDay = 9;
			smartBuffer.ClearAfter(time);
			Assert.AreEqual(0,smartBuffer.TimesCount);

			time.ModifiedJulianDay = 9;
			smartBuffer.ClearAfter(time);
			Assert.AreEqual(0,smartBuffer.TimesCount);

			smartBuffer.AddValues(new TimeStamp(10), new ScalarSet(new double[2] {11, 21}));
			smartBuffer.AddValues(new TimeStamp(13), new ScalarSet(new double[2] {12 ,22}));
			smartBuffer.AddValues(new TimeStamp(16), new ScalarSet(new double[2] {13 ,23}));
			smartBuffer.AddValues(new TimeStamp(20), new ScalarSet(new double[2] {14 ,24}));
			smartBuffer.AddValues(new TimeStamp(27), new ScalarSet(new double[2] {15 ,25}));
			smartBuffer.AddValues(new TimeStamp(30), new ScalarSet(new double[2] {16, 26}));
			smartBuffer.AddValues(new TimeStamp(48), new ScalarSet(new double[2] {17, 27}));

			HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp start   = new HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp(50);
			HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp end     = new HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp(55);
			HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan timeSpan = new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(start,end);

			start.ModifiedJulianDay = 50; // this time is after the last time in buffer
			smartBuffer.ClearAfter(timeSpan);
			Assert.AreEqual(7,smartBuffer.TimesCount);  //nothing removed
			Assert.AreEqual(11,((IScalarSet) smartBuffer.GetValuesAt(0)).GetScalar(0));
			Assert.AreEqual(27,((IScalarSet) smartBuffer.GetValuesAt(6)).GetScalar(1));

			start.ModifiedJulianDay = 30;
			smartBuffer.ClearAfter(timeSpan);
			Assert.AreEqual(5,smartBuffer.TimesCount);
			Assert.AreEqual(11,((IScalarSet) smartBuffer.GetValuesAt(0)).GetScalar(0));
			Assert.AreEqual(25,((IScalarSet) smartBuffer.GetValuesAt(4)).GetScalar(1));

			start.ModifiedJulianDay = 16.5;
			smartBuffer.ClearAfter(timeSpan);
			Assert.AreEqual(3,smartBuffer.TimesCount);
			Assert.AreEqual(11,((IScalarSet) smartBuffer.GetValuesAt(0)).GetScalar(0));
			Assert.AreEqual(23,((IScalarSet) smartBuffer.GetValuesAt(2)).GetScalar(1));

			start.ModifiedJulianDay = 9;
			smartBuffer.ClearAfter(timeSpan);
			Assert.AreEqual(0,smartBuffer.TimesCount);

			start.ModifiedJulianDay = 9;
			smartBuffer.ClearAfter(timeSpan);
			Assert.AreEqual(0,smartBuffer.TimesCount);

			smartBuffer.AddValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(10),new TimeStamp(13)), new ScalarSet(new double[2] {11, 21}));
			smartBuffer.AddValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(13),new TimeStamp(16)), new ScalarSet(new double[2] {12, 22}));
			smartBuffer.AddValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(16),new TimeStamp(20)), new ScalarSet(new double[2] {13, 23}));
			smartBuffer.AddValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(20),new TimeStamp(27)), new ScalarSet(new double[2] {14, 24}));
			smartBuffer.AddValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(27),new TimeStamp(30)), new ScalarSet(new double[2] {15, 25}));
			smartBuffer.AddValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(30),new TimeStamp(48)), new ScalarSet(new double[2] {16, 26}));
			smartBuffer.AddValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(48),new TimeStamp(55)), new ScalarSet(new double[2] {17, 27}));

			
			time.ModifiedJulianDay = 50; // this time is after the last time in buffer
			smartBuffer.ClearAfter(time);
			Assert.AreEqual(7,smartBuffer.TimesCount);  //nothing removed
			Assert.AreEqual(11,((IScalarSet) smartBuffer.GetValuesAt(0)).GetScalar(0));
			Assert.AreEqual(27,((IScalarSet) smartBuffer.GetValuesAt(6)).GetScalar(1));

			time.ModifiedJulianDay = 30;
			smartBuffer.ClearAfter(time);
			Assert.AreEqual(5,smartBuffer.TimesCount);
			Assert.AreEqual(11,((IScalarSet) smartBuffer.GetValuesAt(0)).GetScalar(0));
			Assert.AreEqual(25,((IScalarSet) smartBuffer.GetValuesAt(4)).GetScalar(1));

			time.ModifiedJulianDay = 16.5;
			smartBuffer.ClearAfter(time);
			Assert.AreEqual(3,smartBuffer.TimesCount);
			Assert.AreEqual(11,((IScalarSet) smartBuffer.GetValuesAt(0)).GetScalar(0));
			Assert.AreEqual(23,((IScalarSet) smartBuffer.GetValuesAt(2)).GetScalar(1));

			time.ModifiedJulianDay = 9;
			smartBuffer.ClearAfter(time);
			Assert.AreEqual(0,smartBuffer.TimesCount);

			time.ModifiedJulianDay = 9;
			smartBuffer.ClearAfter(time);
			Assert.AreEqual(0,smartBuffer.TimesCount);

			smartBuffer.AddValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(10),new TimeStamp(13)), new ScalarSet(new double[2] {11, 21}));
			smartBuffer.AddValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(13),new TimeStamp(16)), new ScalarSet(new double[2] {12, 22}));
			smartBuffer.AddValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(16),new TimeStamp(20)), new ScalarSet(new double[2] {13, 23}));
			smartBuffer.AddValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(20),new TimeStamp(27)), new ScalarSet(new double[2] {14, 24}));
			smartBuffer.AddValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(27),new TimeStamp(30)), new ScalarSet(new double[2] {15, 25}));
			smartBuffer.AddValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(30),new TimeStamp(48)), new ScalarSet(new double[2] {16, 26}));
			smartBuffer.AddValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(48),new TimeStamp(55)), new ScalarSet(new double[2] {17, 27}));

			start.ModifiedJulianDay = 50; // this time is after the last time in buffer
			smartBuffer.ClearAfter(timeSpan);
			Assert.AreEqual(7,smartBuffer.TimesCount);  //nothing removed
			Assert.AreEqual(11,((IScalarSet) smartBuffer.GetValuesAt(0)).GetScalar(0));
			Assert.AreEqual(27,((IScalarSet) smartBuffer.GetValuesAt(6)).GetScalar(1));

			start.ModifiedJulianDay = 30;
			smartBuffer.ClearAfter(timeSpan);
			Assert.AreEqual(5,smartBuffer.TimesCount);
			Assert.AreEqual(11,((IScalarSet) smartBuffer.GetValuesAt(0)).GetScalar(0));
			Assert.AreEqual(25,((IScalarSet) smartBuffer.GetValuesAt(4)).GetScalar(1));

			start.ModifiedJulianDay = 16.5;
			smartBuffer.ClearAfter(timeSpan);
			Assert.AreEqual(3,smartBuffer.TimesCount);
			Assert.AreEqual(11,((IScalarSet) smartBuffer.GetValuesAt(0)).GetScalar(0));
			Assert.AreEqual(23,((IScalarSet) smartBuffer.GetValuesAt(2)).GetScalar(1));

			start.ModifiedJulianDay = 9;
			smartBuffer.ClearAfter(timeSpan);
			Assert.AreEqual(0,smartBuffer.TimesCount);

			start.ModifiedJulianDay = 9;
			smartBuffer.ClearAfter(timeSpan);
			Assert.AreEqual(0,smartBuffer.TimesCount);
		}
		public void GetValues1A()
		{
			// Running with one instances of riverModelLC linked ID-based to trigger

			try
			{
				ILinkableComponent riverModelLC = new RiverModelLC();
				HydroNumerics.OpenMI.Sdk.Wrapper.UnitTest.Trigger trigger = new HydroNumerics.OpenMI.Sdk.Wrapper.UnitTest.Trigger();
	
				Argument[] riverArguments = new Argument[2];
				riverArguments[0] = new Argument("ModelID","RiverModel",true,"argument");
				riverArguments[1] = new Argument("TimeStepLength","3600",true,"A time step length of 1 hour");

				riverModelLC.Initialize(riverArguments);
				trigger.Initialize(new Argument[0]);

				Link link = new Link(riverModelLC, riverModelLC.GetOutputExchangeItem(2).ElementSet,riverModelLC.GetOutputExchangeItem(2).Quantity,trigger,trigger.GetInputExchangeItem(0).ElementSet,trigger.GetInputExchangeItem(0).Quantity,"LinkID");
				riverModelLC.AddLink(link);
				trigger.AddLink(link);

				riverModelLC.Prepare();
				
				double firstTriggerGetValuesTime = riverModelLC.TimeHorizon.Start.ModifiedJulianDay;
				TimeStamp[] triggerTimes = new TimeStamp[2];
				triggerTimes[0] = new TimeStamp(firstTriggerGetValuesTime + 2);
				triggerTimes[1] = new TimeStamp(firstTriggerGetValuesTime + 4.3);
	
				trigger.Run(triggerTimes);
				double x1 = ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(0)).GetScalar(0);
				Assert.AreEqual(35.0/4.0,x1);

				double x2 = ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(1)).GetScalar(0);
				Assert.AreEqual(35.0/4.0,x2);

				riverModelLC.Finish();
				riverModelLC.Dispose();
				
			}
			catch (System.Exception e)
			{
				ExceptionHandler.WriteException(e);
				throw (e);
			}
		}
		public void LinearConvertionDataOperation()
		{
			// Running with one instances of riverModelLC linked ID-based to trigger
			// using the linearConversionDataOperation.

			try
			{
				ILinkableComponent riverModelLC = new RiverModelLC();
				HydroNumerics.OpenMI.Sdk.Wrapper.UnitTest.Trigger trigger = new HydroNumerics.OpenMI.Sdk.Wrapper.UnitTest.Trigger();
	
				Argument[] riverArguments = new Argument[2];
				riverArguments[0] = new Argument("ModelID","RiverModel",true,"argument");
				riverArguments[1] = new Argument("TimeStepLength","3600",true,"A time step length of 1 hour");

				riverModelLC.Initialize(riverArguments);
				trigger.Initialize(new Argument[0]);

				Link link = new Link(riverModelLC, riverModelLC.GetOutputExchangeItem(2).ElementSet,riverModelLC.GetOutputExchangeItem(2).Quantity,trigger,trigger.GetInputExchangeItem(0).ElementSet,trigger.GetInputExchangeItem(0).Quantity,"LinkID");

				//add linear conversion data operation
				bool dataOperationWasFound = false;
				int dataOperationIndex = -9;
				for (int i = 0; i < riverModelLC.GetOutputExchangeItem(2).DataOperationCount; i++)
				{
					if (riverModelLC.GetOutputExchangeItem(2).GetDataOperation(i).ID == "Linear Conversion")
					{
						dataOperationWasFound = true;
						dataOperationIndex = i;
					}
				}
				Assert.AreEqual(true,dataOperationWasFound);
				IDataOperation linearConvertionDataOperation = riverModelLC.GetOutputExchangeItem(2).GetDataOperation(dataOperationIndex);
				bool key_A_WasFound = false;
				bool key_B_WasFound = false;
				bool key_Type_WasFound = false;

				for (int i = 0; i < linearConvertionDataOperation.ArgumentCount; i++)
				{
					if ( linearConvertionDataOperation.GetArgument(i).Key == "A")
					{
						linearConvertionDataOperation.GetArgument(i).Value = "2.5";
						key_A_WasFound = true;
						Assert.AreEqual(false,linearConvertionDataOperation.GetArgument(i).ReadOnly);
					}
					if ( linearConvertionDataOperation.GetArgument(i).Key == "B")
					{
						linearConvertionDataOperation.GetArgument(i).Value = "3.5";
						key_B_WasFound = true;
						Assert.AreEqual(false,linearConvertionDataOperation.GetArgument(i).ReadOnly);
					}

					if ( linearConvertionDataOperation.GetArgument(i).Key == "Type")
					{
						key_Type_WasFound = true;
						Assert.AreEqual(true,linearConvertionDataOperation.GetArgument(i).ReadOnly);
					}
				}

				Assert.AreEqual(true,key_A_WasFound);
				Assert.AreEqual(true,key_B_WasFound);
				Assert.AreEqual(true,key_Type_WasFound);
				Assert.AreEqual("Linear Conversion",linearConvertionDataOperation.ID);

				link.AddDataOperation(linearConvertionDataOperation);

				riverModelLC.AddLink(link);
				trigger.AddLink(link);

				riverModelLC.Prepare();
				
				double firstTriggerGetValuesTime = riverModelLC.TimeHorizon.Start.ModifiedJulianDay;
				TimeStamp[] triggerTimes = new TimeStamp[2];
				triggerTimes[0] = new TimeStamp(firstTriggerGetValuesTime + 2);
				triggerTimes[1] = new TimeStamp(firstTriggerGetValuesTime + 4.3);
	
				trigger.Run(triggerTimes);
				double x1 = ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(0)).GetScalar(0);
				Assert.AreEqual(2.5*(35.0/4.0) + 3.5, x1);

				double x2 = ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(1)).GetScalar(0);
				Assert.AreEqual(2.5 * (35.0/4.0) + 3.5, x2);

				riverModelLC.Finish();
				riverModelLC.Dispose();
				
			}
			catch (System.Exception e)
			{
				ExceptionHandler.WriteException(e);
				throw (e);
			}
		}
        public override IValueSet GetValues(ITime time, string LinkID)
        {
            if (!initializeWasInvoked)
            {
                throw new Exception("Method \"GetValues\" in LinkableTimeSeriesGroup class was invoked before the Initialize method was invoked");
            }

            ILink link = this.GetLink(LinkID);
            SendSourceAfterGetValuesCallEvent(time, link);

            // -- handle incoming links (where this component is the target)
            if (!isBusy) //avoiding deadlocks 
            {
                isBusy = true;
                foreach (ILink acceptingLink in _acceptingLinks)
                {
                    if (((TsQuantity)acceptingLink.TargetQuantity).BaseTimeSeries is TimestampSeries)
                    {
                        TimestampSeries timestampSeries = (TimestampSeries)((TsQuantity)acceptingLink.TargetQuantity).BaseTimeSeries;
                        foreach (TimestampValue timestampValue in timestampSeries.Items)
                        {
                            TimeStamp getValuesTime = new TimeStamp(timestampValue.Time);
                            SendTargetBeforeGetValuesCall(getValuesTime, acceptingLink);
                            IValueSet valueSet = acceptingLink.SourceComponent.GetValues(getValuesTime, acceptingLink.ID);
                            timestampValue.Value = ((IScalarSet)valueSet).GetScalar(0);
                            SendTargetAfterGetValuesReturn(timestampValue.Value, acceptingLink);
                        }
                    }
                    else if (((TsQuantity)acceptingLink.TargetQuantity).BaseTimeSeries is TimespanSeries)
                    {
                        TimespanSeries timespanSeries = (TimespanSeries)((TsQuantity)acceptingLink.TargetQuantity).BaseTimeSeries;
                        foreach (TimespanValue timespanValue in timespanSeries.Items)
                        {
                            HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan timeSpan = new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp(timespanValue.StartTime), new HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp(timespanValue.EndTime));
                            SendTargetBeforeGetValuesCall(timeSpan, acceptingLink);
                            IValueSet valueSet = acceptingLink.SourceComponent.GetValues(timeSpan, acceptingLink.ID);
                            timespanValue.Value = ((IScalarSet)valueSet).GetScalar(0);
                            SendTargetAfterGetValuesReturn(timespanValue.Value, acceptingLink);
                        }
                    }
                    else
                    {
                        throw new Exception("Unexpected exception : Undefined timeseries type (occured in HydroNumerics.Time.OpenMI.LinkableTimeSeriesGroup class)");
                    }
                }
                isBusy = false;
            }

            // -- handle outgoing links (where this component is the source)
            HydroNumerics.Core.Unit toUnit = new HydroNumerics.Core.Unit();
            toUnit.ConversionFactorToSI = link.TargetQuantity.Unit.ConversionFactorToSI;
            toUnit.OffSetToSI = link.TargetQuantity.Unit.OffSetToSI;

            if (time is ITimeStamp)
            {
                HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp t = new TimeStamp((ITimeStamp)time);

                double x = ((TsQuantity)link.SourceQuantity).BaseTimeSeries.GetValue(t.ToDateTime(), toUnit);
                ScalarSet scalarSet = new ScalarSet(new double[] { x });
                SendSourceBeforeGetValuesReturn(x, link);
                return scalarSet;
            }
            else
            {
                HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp start = new TimeStamp(((ITimeSpan)time).Start);
                HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp end = new TimeStamp(((ITimeSpan)time).End);

                double x = ((TsQuantity)link.SourceQuantity).BaseTimeSeries.GetValue(start.ToDateTime(), end.ToDateTime(), toUnit);
                ScalarSet scalarSet = new ScalarSet(new double[] { x });
                SendSourceBeforeGetValuesReturn(x, link);
                return scalarSet;
            }
        }
		public void GetValues2A()
		{
			// == Running with two instances of riverModelLC ==
			// 
			// - The Two river are running with the same timestepping.
			//
			// - The link is ID Based link with flow from last branch of the source river to the top
			//   node of the target river.
			//
			// - The time argument in the GetValues from rive to river is of type ITimeSpan
			//
			//TODO: 1: The RiverModelEngine should change the inflow over time. As it is now the inflow is the same
			//         in all time steps. Another idea would be to have a output exchange item that hold the accumulated
			//         inflow, this could be useful when testing the manage state interface.
			//
			//       2: Make this test run with the two river using different timesteps and with the source river
			//          starting ealier that the target river.
			//
			//       3: In this test also events could be tested. Simply test if all the required events are
			//          thrown during the simulations.
			try
			{
				ILinkableComponent upperRiver = new RiverModelLC();
				ILinkableComponent lowerRiver = new RiverModelLC();
                HydroNumerics.OpenMI.Sdk.Wrapper.UnitTest.Trigger trigger     = new HydroNumerics.OpenMI.Sdk.Wrapper.UnitTest.Trigger();

				// The ModelID is passes in ordet to make it easier to debug, otherwise you cannot se the difference between the two istances of RiverModelLC
				Argument[] upperRiverArguments = new Argument[1];
				upperRiverArguments[0] = new Argument("ModelID","upperRiverModel",true,"argument");
				upperRiver.Initialize(upperRiverArguments);

				// The ModelID is passes in ordet to make it easier to debug, otherwise you cannot se the difference between the two istances of RiverModelLC
				Argument[] lowerRiverArguments = new Argument[1];
				lowerRiverArguments[0] = new Argument("ModelID","lowerRiverModel",true,"argument");
				lowerRiver.Initialize(lowerRiverArguments);
				trigger.Initialize(new Argument[0]);

				Assert.AreEqual("upperRiverModel",upperRiver.ModelID);
				Assert.AreEqual("lowerRiverModel",lowerRiver.ModelID);

				Link link = new Link();
				link.ID = "RiverToRiverLink";
				link.SourceComponent  = upperRiver;
				link.SourceElementSet = upperRiver.GetOutputExchangeItem(2).ElementSet; //last branch in the river
				link.SourceQuantity   = upperRiver.GetOutputExchangeItem(2).Quantity;
				link.TargetComponent  = lowerRiver;
				link.TargetElementSet = lowerRiver.GetInputExchangeItem(0).ElementSet;  //first node in the river
				link.TargetQuantity   = lowerRiver.GetInputExchangeItem(0).Quantity;

				Link triggerLink = new Link();

				triggerLink.ID				 = "TriggerLink";
				triggerLink.SourceComponent  = lowerRiver;
				triggerLink.SourceElementSet = lowerRiver.GetOutputExchangeItem(2).ElementSet;
				triggerLink.SourceQuantity   = lowerRiver.GetOutputExchangeItem(2).Quantity;
				triggerLink.TargetComponent  = trigger;
				triggerLink.TargetElementSet = trigger.GetInputExchangeItem(0).ElementSet;
				triggerLink.TargetQuantity   = trigger.GetInputExchangeItem(0).Quantity;
		
				upperRiver.AddLink(link);
				lowerRiver.AddLink(link);

				lowerRiver.AddLink(triggerLink);
				trigger.AddLink(triggerLink);

				upperRiver.Prepare();
				lowerRiver.Prepare();
				trigger.Prepare();

				double firstTriggerGetValuesTime = lowerRiver.TimeHorizon.Start.ModifiedJulianDay;
				TimeStamp[] triggerTimes = new TimeStamp[2];
				triggerTimes[0] = new TimeStamp(firstTriggerGetValuesTime + 3);
				triggerTimes[1] = new TimeStamp(firstTriggerGetValuesTime + 4.3);

				trigger.Run(triggerTimes);

				double x1 = ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(0)).GetScalar(0);
				Assert.AreEqual(315.0/32.0,x1);

				double x2 = ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(1)).GetScalar(0);
				Assert.AreEqual(315.0/32.0,x2);

				upperRiver.Finish();
				lowerRiver.Finish();

				upperRiver.Dispose();
				lowerRiver.Dispose();

			}
			catch (System.Exception e)
			{
				ExceptionHandler.WriteException(e);
				throw (e);
			}
		}
		public void TimeToTimeStamp()
		{
			HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan timeSpan = new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(10.0),new TimeStamp(20.0));
			HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp timeStamp = new TimeStamp(15.0);

			Assert.AreEqual(20.0, LinkableRunEngine.TimeToTimeStamp(timeSpan).ModifiedJulianDay);
			Assert.AreEqual(15.0, LinkableRunEngine.TimeToTimeStamp(timeStamp).ModifiedJulianDay);
		}
Esempio n. 22
0
		public Trigger()
		{
			_resultsBuffer = new SmartBuffer();
			_earliestInputTime = new TimeStamp(0);
		}
		public void GetValues4A()
		{
			//This test is: RiverModel --> GWModel --> Trigger
			//Testing: Georeferenced links
			RiverModelLC riverModel = new RiverModelLC();
			GWModelLC    gWModel    = new GWModelLC();
			Trigger      trigger    = new Trigger();

			riverModel.Initialize(new Argument[0]);
			gWModel.Initialize(new Argument[0]);

			Link riverGWLink = new Link();
			riverGWLink.ID = "RiverGWLink";
			riverGWLink.SourceComponent = riverModel;
			riverGWLink.SourceElementSet = riverModel.GetOutputExchangeItem(6).ElementSet;
			riverGWLink.SourceQuantity   = riverModel.GetOutputExchangeItem(0).Quantity;
			riverGWLink.TargetComponent  = gWModel;
			riverGWLink.TargetElementSet = gWModel.GetInputExchangeItem(0).ElementSet;
			riverGWLink.TargetQuantity   = gWModel.GetInputExchangeItem(0).Quantity;

			int dataOperationIndex = -9;
			for (int i = 0; i < riverModel.GetOutputExchangeItem(6).DataOperationCount; i++)
			{
				if (riverModel.GetOutputExchangeItem(6).GetDataOperation(i).ID == "ElementMapper501")
				{
					dataOperationIndex = i;
				}
			}

			if (dataOperationIndex < 0)
			{
				throw new Exception("failed to find dataOperation");
			}
			
			riverGWLink.AddDataOperation(riverModel.GetOutputExchangeItem(6).GetDataOperation(dataOperationIndex));

			Link triggerLink = new Link();
			triggerLink.ID = "RiverGWLink";
			triggerLink.SourceComponent = gWModel;
			triggerLink.SourceElementSet = gWModel.GetOutputExchangeItem(0).ElementSet;
			triggerLink.SourceQuantity   = gWModel.GetOutputExchangeItem(0).Quantity;
			triggerLink.TargetComponent  = trigger;
			triggerLink.TargetElementSet = trigger.GetInputExchangeItem(0).ElementSet;
			triggerLink.TargetQuantity   = trigger.GetInputExchangeItem(0).Quantity;

			riverModel.AddLink(riverGWLink);
			gWModel.AddLink(riverGWLink);
			gWModel.AddLink(triggerLink);
			trigger.AddLink(triggerLink);

			riverModel.Prepare();
			gWModel.Prepare();

			double firstTriggerGetValuesTime = riverModel.TimeHorizon.Start.ModifiedJulianDay;
			TimeStamp[] triggerTimes = new TimeStamp[2];
			triggerTimes[0] = new TimeStamp(firstTriggerGetValuesTime + 12.1);
			triggerTimes[1] = new TimeStamp(firstTriggerGetValuesTime + 16.7);
	
			trigger.Run(triggerTimes);

			double x0 = ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(0)).GetScalar(0);
			Assert.AreEqual(0.0,x0);

			double x1 = ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(0)).GetScalar(1);
			Assert.AreEqual(105.0/16.0,x1);

			double x2 = ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(0)).GetScalar(2);
			Assert.AreEqual(7.5,x2);

			double x3 = ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(0)).GetScalar(3);
			Assert.AreEqual(5.0+35.0/16.0,x3);

			riverModel.Finish();
			gWModel.Finish();

			riverModel.Dispose();
			gWModel.Dispose();

		}
		public void ITimeToString()
		{
			System.DateTime t1 = new DateTime(2004,7,12,10,25,34);
			System.DateTime t2 = new DateTime(2004,8,15,13,15,14);
			TimeStamp timeStamp1 = new TimeStamp(HydroNumerics.OpenMI.Sdk.DevelopmentSupport.CalendarConverter.Gregorian2ModifiedJulian(t1));
			TimeStamp timeStamp2 = new TimeStamp(HydroNumerics.OpenMI.Sdk.DevelopmentSupport.CalendarConverter.Gregorian2ModifiedJulian(t2));
			HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan timeSpan = new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(timeStamp1,timeStamp2);

			Assert.AreEqual(t1.ToString(),LinkableRunEngine.ITimeToString(timeStamp1));
			string str = "[" + t1.ToString()+", "+t2.ToString()+"]";
			Assert.AreEqual(str, LinkableRunEngine.ITimeToString(timeSpan));

		}
Esempio n. 25
0
		public void Init()
		{
			timeStamp = new TimeStamp(12345.3);
		}
		public void GetValues2B()
		{
			// This is a variation of GetValues2A. In this test the timeSeries is linked ID based to the 
			// top node of the upperRiver. The last upperRiver branch of the upperRiver is ID based connected
			// to the top node of the lowerRiver. The last branch in the lowerRiver is linked to the trigger.
			// The timeSeries provides data that changes over time. This is what makes this test different from
			// GetValues2A, where everytning is the same for every time step.
			
			try
			{
				ILinkableComponent timeSeries = new TimeSeriesComponent();
				ILinkableComponent upperRiver = new RiverModelLC();
				ILinkableComponent lowerRiver = new RiverModelLC();
				HydroNumerics.OpenMI.Sdk.Wrapper.UnitTest.Trigger trigger     = new HydroNumerics.OpenMI.Sdk.Wrapper.UnitTest.Trigger();

				timeSeries.Initialize(new Argument[0]);

				// The ModelID is passes in ordet to make it easier to debug, otherwise you cannot se the difference between the two istances of RiverModelLC
				Argument[] upperRiverArguments = new Argument[1];
				upperRiverArguments[0] = new Argument("ModelID","upperRiverModel",true,"argument");
				upperRiver.Initialize(upperRiverArguments);

				// The ModelID is passes in ordet to make it easier to debug, otherwise you cannot se the difference between the two istances of RiverModelLC
				Argument[] lowerRiverArguments = new Argument[1];
				lowerRiverArguments[0] = new Argument("ModelID","lowerRiverModel",true,"argument");
				lowerRiver.Initialize(lowerRiverArguments);
				trigger.Initialize(new Argument[0]);

				Assert.AreEqual("upperRiverModel",upperRiver.ModelID);
				Assert.AreEqual("lowerRiverModel",lowerRiver.ModelID);

				Link timeSeriesToUpperRiverLink = new Link();
				timeSeriesToUpperRiverLink.ID = "timeSeriesToUpperRiverLink";
				timeSeriesToUpperRiverLink.SourceComponent  = timeSeries;
				timeSeriesToUpperRiverLink.SourceElementSet = timeSeries.GetOutputExchangeItem(0).ElementSet; //last branch in the river
				timeSeriesToUpperRiverLink.SourceQuantity   = timeSeries.GetOutputExchangeItem(0).Quantity;
				timeSeriesToUpperRiverLink.TargetComponent  = upperRiver;
				timeSeriesToUpperRiverLink.TargetElementSet = upperRiver.GetInputExchangeItem(0).ElementSet;  //first node in the river
				timeSeriesToUpperRiverLink.TargetQuantity   = upperRiver.GetInputExchangeItem(0).Quantity;
				
				Link link = new Link();
				link.ID = "RiverToRiverLink";
				link.SourceComponent  = upperRiver;
				link.SourceElementSet = upperRiver.GetOutputExchangeItem(2).ElementSet; //last branch in the river
				link.SourceQuantity   = upperRiver.GetOutputExchangeItem(2).Quantity;
				link.TargetComponent  = lowerRiver;
				link.TargetElementSet = lowerRiver.GetInputExchangeItem(0).ElementSet;  //first node in the river
				link.TargetQuantity   = lowerRiver.GetInputExchangeItem(0).Quantity;

				Link triggerLink = new Link();

				triggerLink.ID				 = "TriggerLink";
				triggerLink.SourceComponent  = lowerRiver;
				triggerLink.SourceElementSet = lowerRiver.GetOutputExchangeItem(2).ElementSet;
				triggerLink.SourceQuantity   = lowerRiver.GetOutputExchangeItem(2).Quantity;
				triggerLink.TargetComponent  = trigger;
				triggerLink.TargetElementSet = trigger.GetInputExchangeItem(0).ElementSet;
				triggerLink.TargetQuantity   = trigger.GetInputExchangeItem(0).Quantity;
		
				timeSeries.AddLink(timeSeriesToUpperRiverLink);
				upperRiver.AddLink(timeSeriesToUpperRiverLink);

				upperRiver.AddLink(link);
				lowerRiver.AddLink(link);

				lowerRiver.AddLink(triggerLink);
				trigger.AddLink(triggerLink);

				timeSeries.Prepare();
				upperRiver.Prepare();
				lowerRiver.Prepare();
				trigger.Prepare();

				double firstTriggerGetValuesTime = lowerRiver.TimeHorizon.Start.ModifiedJulianDay;
				TimeStamp[] triggerTimes = new TimeStamp[2];
				triggerTimes[0] = new TimeStamp(firstTriggerGetValuesTime + 12.5);
				triggerTimes[1] = new TimeStamp(firstTriggerGetValuesTime + 16.2);

				trigger.Run(triggerTimes);

				double x1 = ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(0)).GetScalar(0);
				Assert.AreEqual(315.0/32.0 + 13.0/64.0,x1);

				double x2 = ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(1)).GetScalar(0);
				Assert.AreEqual(315.0/32.0 + 17.0/64.0,x2);

				upperRiver.Finish();
				lowerRiver.Finish();

				upperRiver.Dispose();
				lowerRiver.Dispose();

			}
			catch (System.Exception e)
			{
				ExceptionHandler.WriteException(e);
				throw (e);
			}
		}
Esempio n. 27
0
		public ITime GetInputTime(string QuantityID, string ElementSetID)
		{
			HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp inputStart = new TimeStamp(((ITimeSpan)GetCurrentTime()).End.ModifiedJulianDay);
			HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp inputEnd = new TimeStamp(((ITimeSpan) GetCurrentTime()).End.ModifiedJulianDay + _timeStepLength/(24.0 * 3600.0));
			return new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(inputStart, inputEnd);
		}
        /// <summary>
        /// Runs simulation.
        /// </summary>
        /// <param name="runListener">Simulation listener.</param>
        /// <param name="runInSameThread">If <c>true</c>, simulation is run in same thread like caller,
        /// ie. method blocks until simulation don't finish. If <c>false</c>, simulation is
        /// run in separate thread and method returns immediately.</param>
        /// <remarks>
        /// Simulation is run the way that trigger invokes <see cref="ILinkableComponent.GetValues">ILinkableComponent.GetValues</see>
        /// method of the model it's connected to
        /// at the time specified by <see cref="TriggerInvokeTime">TriggerInvokeTime</see> property.
        /// If you need to use more than one listener you can use <see cref="ProxyListener">ProxyListener</see>
        /// class or <see cref="ProxyMultiThreadListener">ProxyMultiThreadListener</see> if <c>runInSameThread</c> is <c>false</c>.
        /// </remarks>
        public void Run(IListener runListener, bool runInSameThread)
        {
            if (!HasTrigger())
                throw (new Exception("Composition has no trigger."));
            if (_running)
                throw (new Exception("Simulation is already running."));

            _running = true;
            _runListener = runListener;

            try
            {
                TimeStamp runToTime = new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(_triggerInvokeTime));

                // Create informative message
                if (_runListener != null)
                {
                    StringBuilder description = new StringBuilder();
                    description.Append("Starting simulation at ");
                    description.Append(DateTime.Now.ToString());
                    description.Append(",");

                    description.Append(" composition consists from following models:\n");
                    foreach (UIModel model in _models)
                    {
                        description.Append(model.ModelID);
                        description.Append(", ");
                    }

                    // todo: add more info?

                    Event theEvent = new Event(EventType.Informative);
                    theEvent.Description = description.ToString();
                    _runListener.OnEvent(theEvent);
                }

                _runPrepareForComputationStarted = true;

                // prepare for computation
                if (_runListener != null)
                {
                    Event theEvent = new Event(EventType.Informative);
                    theEvent.Description = "Preparing for computation....";
                    _runListener.OnEvent(theEvent);
                }
                foreach (UIModel uimodel in _models)
                {
                    if (_runListener != null)
                    {
                        Event theEvent = new Event(EventType.Informative);
                        theEvent.Description = "Calling Prepare() method of model " + uimodel.ModelID;
                        _runListener.OnEvent(theEvent);
                    }
                    uimodel.LinkableComponent.Prepare();
                }

                // subscribing event listener to all models
                if (_runListener != null)
                {
                    Event theEvent = new Event(EventType.Informative);
                    theEvent.Description = "Subscribing proxy event listener....";
                    _runListener.OnEvent(theEvent);

                    for (int i = 0; i < _runListener.GetAcceptedEventTypeCount(); i++)
                        foreach (UIModel uimodel in _models)
                        {
                            theEvent = new Event(EventType.Informative);
                            theEvent.Description = "Calling Subscribe() method with EventType." + ((EventType)i).ToString() + " of model " + uimodel.ModelID;
                            _runListener.OnEvent(theEvent);

                            for (int j = 0; j < uimodel.LinkableComponent.GetPublishedEventTypeCount(); j++)
                                if (uimodel.LinkableComponent.GetPublishedEventType(j) == _runListener.GetAcceptedEventType(i))
                                {
                                    uimodel.LinkableComponent.Subscribe(_runListener, _runListener.GetAcceptedEventType(i));
                                    break;
                                }
                        }
                }



                if (!runInSameThread)
                {
                    // creating run thread
                    if (_runListener != null)
                    {
                        Event theEvent = new Event(EventType.Informative);
                        theEvent.Description = "Creating run thread....";
                        _runListener.OnEvent(theEvent);
                    }

                    _runThread = new Thread(new ThreadStart(RunThreadFunction));

                    // starting thread...
                    if (_runListener != null)
                    {
                        Event theEvent = new Event(EventType.GlobalProgress);
                        theEvent.Description = "Starting run thread....";
                        _runListener.OnEvent(theEvent);
                    }

                    _runThread.Start();
                }
                else
                {
                    // run simulation in same thread (for example when running from console)
                    if (_runListener != null)
                    {
                        Event theEvent = new Event(EventType.Informative);
                        theEvent.Description = "Running simulation in same thread....";
                        _runListener.OnEvent(theEvent);
                    }
                    RunThreadFunction();
                }
            }
            catch (System.Exception e)
            {
                if (_runListener != null)
                {
                    Event theEvent = new Event(EventType.Informative);
                    theEvent.Description = "Exception occured while initiating simulation run: " + e.ToString();
                    _runListener.OnEvent(theEvent);
                    _runListener.OnEvent(SimulationFailedEvent); // todo: add info about time to this event
                }
            }

        }
		/// <summary>
		/// Convert a ITime object to a ITimeStamp.
		/// </summary>
		/// <param name="time">The ITime object to convert</param>
		/// <returns>The converted time</returns>
        public static HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp TimeToTimeStamp(ITime time)
		{
			HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp t;

			if (time is ITimeStamp)
			{
				t = new HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp(((ITimeStamp) time).ModifiedJulianDay);
			}
			else
			{
				t = new HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp(((ITimeSpan) time).End.ModifiedJulianDay);
			}

			return t;
		}
Esempio n. 30
0
		/// <summary>
		/// Invokes <see cref="ILinkableComponent.GetValues">ILinkableComponent.GetValues</see>
		/// method on model linked to this trigger, if any.
		/// </summary>
		/// <param name="runToTime">Time for <see cref="ILinkableComponent.GetValues">ILinkableComponent.GetValues</see> call.</param>
		public void Run(HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp runToTime)
		{
			if( _link!=null )
			{
				_earliestInputTime = runToTime;
				_link.SourceComponent.GetValues(runToTime,_link.ID);
			}
		}
Esempio n. 31
0
		/// <summary>
		/// Creates a new instance of <see cref="Trigger">Trigger</see> class.
		/// </summary>
		public Trigger()
		{
			_link = null;
			_inputExchangeItem = new TriggerExchangeItem();

			HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp
				start = new TimeStamp( CalendarConverter.Gregorian2ModifiedJulian(new DateTime(1800,1,1)) ),
				end = new TimeStamp( CalendarConverter.Gregorian2ModifiedJulian(new DateTime(2200,1,1)) );
			_timeHorizon = new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(start, end);

			_earliestInputTime = end;
		}
        private string ITimeToString(ITime time)
        {
            if (time is ITimeSpan)
            {
                DateTime startTime = new HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp(((ITimeSpan)time).Start).ToDateTime();
                DateTime endTime = new HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp(((ITimeSpan)time).End).ToDateTime();
                return("(" + startTime.ToLongDateString()+":"+ startTime.ToLongTimeString() + ", " + endTime.ToLongDateString() +":" + endTime.ToLongTimeString());
            }
            else
            {
                DateTime dateTime = new HydroNumerics.OpenMI.Sdk.Backbone.TimeStamp((ITimeStamp)time).ToDateTime();
                return("(" + dateTime.ToLongDateString() +":" +dateTime.ToLongTimeString() + ")");
            }

        }
		public void GetValues1B()
		{
			// Running with one instances of riverModelLC linked ID-based to trigger and to 
			// an instance of the TimeSeriesComponent.
			try
			{
				ILinkableComponent timeSeries = new TimeSeriesComponent();
				ILinkableComponent riverModelLC = new RiverModelLC();
				HydroNumerics.OpenMI.Sdk.Wrapper.UnitTest.Trigger trigger = new HydroNumerics.OpenMI.Sdk.Wrapper.UnitTest.Trigger();
	
				timeSeries.Initialize(new Argument[0]);

				Argument[] riverArguments = new Argument[2];
				riverArguments[0] = new Argument("ModelID","upperRiverModel",true,"argument");
				riverArguments[1] = new Argument("TimeStepLength","3600",true,"A time step length of 1 day");

				riverModelLC.Initialize(riverArguments);
				trigger.Initialize(new Argument[0]);

				Link timeSeriesToRiverLink = new Link();
				timeSeriesToRiverLink.ID = "timeSeriesToUpperRiverLink";
				timeSeriesToRiverLink.SourceComponent  = timeSeries;
				timeSeriesToRiverLink.SourceElementSet = timeSeries.GetOutputExchangeItem(0).ElementSet; //last branch in the river
				timeSeriesToRiverLink.SourceQuantity   = timeSeries.GetOutputExchangeItem(0).Quantity;
				timeSeriesToRiverLink.TargetComponent  = riverModelLC;
				timeSeriesToRiverLink.TargetElementSet = riverModelLC.GetInputExchangeItem(0).ElementSet;  //first node in the river
				timeSeriesToRiverLink.TargetQuantity   = riverModelLC.GetInputExchangeItem(0).Quantity;
				

				Link link = new Link(riverModelLC, riverModelLC.GetOutputExchangeItem(2).ElementSet,riverModelLC.GetOutputExchangeItem(2).Quantity,trigger,trigger.GetInputExchangeItem(0).ElementSet,trigger.GetInputExchangeItem(0).Quantity,"LinkID");
				
                timeSeries.AddLink(timeSeriesToRiverLink);
				riverModelLC.AddLink(timeSeriesToRiverLink);

				riverModelLC.AddLink(link);
				trigger.AddLink(link);

				riverModelLC.Prepare();
				
				double firstTriggerGetValuesTime = riverModelLC.TimeHorizon.Start.ModifiedJulianDay;
				TimeStamp[] triggerTimes = new TimeStamp[2];
				triggerTimes[0] = new TimeStamp(firstTriggerGetValuesTime + 12.1);
				triggerTimes[1] = new TimeStamp(firstTriggerGetValuesTime + 16.7);
	
				trigger.Run(triggerTimes);
				double x1 = ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(0)).GetScalar(0);
				Assert.AreEqual(35.0/4.0 + 13.0/8.0,x1);

				double x2 = ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(1)).GetScalar(0);
				Assert.AreEqual(35.0/4.0 + 17.0/8.0,x2);

				riverModelLC.Finish();
				riverModelLC.Dispose();
				
			}
			catch (System.Exception e)
			{
				ExceptionHandler.WriteException(e);
				throw (e);
			}
		}
Esempio n. 34
0
			public EventCopy( IEvent theEvent )
			{
				_description = theEvent.Description;
				_type = theEvent.Type;
				_sender = theEvent.Sender;
				if( theEvent.SimulationTime == null )
					_simulationTime = null;
				else
					_simulationTime = new TimeStamp( theEvent.SimulationTime );
			}
Esempio n. 35
0
 public void ToDateTime()
 {
     DateTime testDate = new DateTime(2008, 12, 31);
     TimeStamp timeStamp = new TimeStamp(testDate);
     Assert.AreEqual(testDate, timeStamp.ToDateTime());
 }