/// <summary>
	  /// Retrieves data from the providing LinkableComponent as defined 
	  /// in the Link and sets this data in the engine
	  /// </summary>
	  public virtual void UpdateInput()
	  { 
		  ITime inputTime = this._engine.GetInputTime(link.TargetQuantity.ID, link.TargetElementSet.ID);

		  if (inputTime != null) 
		  {
              SendEvent(EventType.TargetBeforeGetValuesCall, this.link.TargetComponent);
              IScalarSet sourceValueSet = (IScalarSet)link.SourceComponent.GetValues(inputTime, link.ID);

              //The input values set is copied in ordet to avoid the risk that it is changed be the provider.

              double targetMissValDef = this._engine.GetMissingValueDefinition();
              ScalarSet targetValueSet = new ScalarSet(sourceValueSet);

              for (int i = 0; i < sourceValueSet.Count; i++)
              {
                  if (!sourceValueSet.IsValid(i))
                  {
                      targetValueSet.data[i] = targetMissValDef;
                  }
              }

              targetValueSet.MissingValueDefinition = targetMissValDef;
              targetValueSet.CompareDoublesEpsilon = targetMissValDef / 1.0e+10;
              SendEvent(EventType.TargetAfterGetValuesReturn, this.link.TargetComponent);
              this.Engine.SetValues(link.TargetQuantity.ID, link.TargetElementSet.ID, targetValueSet);
          }
	  }
示例#2
0
        public void CopyConstructorSpeed()
        {
            var random = new Random();

            var values = new double[5000000];
            for (var i = 0; i < 5000000; i++)
            {
                values[i] = random.Next();
            }

            var scalarSet = new ScalarSet(values);

            // copying values
            var stopwatch = new Stopwatch();
            stopwatch.Start();
            values.Clone();
            stopwatch.Stop();

            var copyArrayTime = stopwatch.ElapsedMilliseconds;
            Trace.WriteLine("Copying array with 1M values took: " + copyArrayTime + " ms");

            stopwatch.Reset();
            stopwatch.Start();
            new ScalarSet(scalarSet);
            stopwatch.Stop();

            Trace.WriteLine("Copying scalar set with 1M values took: " + stopwatch.ElapsedMilliseconds + " ms");

            var fraction = stopwatch.ElapsedMilliseconds/copyArrayTime;

            Assert.IsTrue(fraction < 1.1);
        }
示例#3
0
		public void Equals()
		{
			double[] values={1.0,2.0,3.0};
			ScalarSet scalarSet2 = new ScalarSet(values);
			Assert.IsTrue(scalarSet.Equals(scalarSet2));
			scalarSet2.data[1] = 2.5;
			Assert.IsFalse(scalarSet.Equals(scalarSet2));
		}
示例#4
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);
            }
        }
示例#5
0
        ///<summary>
        /// Check if the current instance equals another instance of this class.
        ///</summary>
        ///<param name="obj">The instance to compare the current instance with.</param>
        ///<returns><code>true</code> if the instances are the same instance or have the same content.</returns>
        public override bool Equals(Object obj)
        {
            if (obj is ScalarSet)
            {
                ScalarSet sourceSet = (ScalarSet)obj;
                if (sourceSet.Count != Count)
                {
                    return(false);
                }
                for (int i = 0; i < Count; i++)
                {
                    if (sourceSet.GetScalar(i) != GetScalar(i))
                    {
                        return(false);
                    }
                }
                return(true);
            }

            return(base.Equals(obj));
        }
示例#6
0
        public global::OpenMI.Standard.IValueSet GetValues(string QuantityID, string ElementSetID)
        {
            if (QuantityID != ElementSetID)
            {
                throw new ApplicationException("Element sets should be named after quantities");
            }
            if (!_elementSets.Contains(ElementSetID))
            {
                throw new ApplicationException("Unknown element set '" + ElementSetID + "'");
            }
            ElementSet elementSet = (ElementSet)_elementSets[ElementSetID];
            int        count      = elementSet.ElementCount;

            double[] returnValues = new double[count];

            // This is O (e * s * q) instead of O (1).
            for (int e = 0; e < count; e++)
            {
                Element element  = elementSet.GetElement(e);
                string  ColumnID = element.ID;
                if (!_elementScopes.Contains(element))
                {
                    throw new ApplicationException("Unknown element for '" + ColumnID + "'/'" + QuantityID + "'");
                }
                Scope scope = (Scope)_elementScopes[element];

                if (scope.HasNumber(QuantityID))
                {
                    returnValues[e] = scope.Number(QuantityID);
                }
                else
                {
                    returnValues[e] = GetMissingValueDefinition();
                }
            }
            Oatc.OpenMI.Sdk.Backbone.ScalarSet values = new Oatc.OpenMI.Sdk.Backbone.ScalarSet(returnValues);
            return(values);
        }
示例#7
0
		/// <summary>
		///	Add corresponding values for time and values to the SmartBuffer.
		/// </summary>
		/// <param name="time"> Description of the time parameter</param>
		/// <param name="valueSet">Description of the values parameter</param>
		/// <remarks>
		/// The AddValues method will internally make a copy of the added times and values. The reason for
		/// doing this is that the times and values arguments are references, and the correspondign values 
		/// could be changed by the owner of the classes
		/// </remarks>
		public void AddValues(ITime time, IValueSet valueSet)
		{
			if (time is ITimeStamp)
			{
				_times.Add(new TimeStamp( ((ITimeStamp) time).ModifiedJulianDay ));
			}
			else if(time is ITimeSpan)
			{
				TimeStamp newStartTime = new TimeStamp(((ITimeSpan) time).Start.ModifiedJulianDay);
				TimeStamp newEndTime = new TimeStamp(((ITimeSpan) time).End.ModifiedJulianDay);

				Backbone.TimeSpan newTimeSpan = new Backbone.TimeSpan(newStartTime, newEndTime);
				_times.Add(newTimeSpan);
			}
			else
			{
				throw new Exception("Invalid datatype used for time argument in method AddValues");
			}

			if (valueSet is IScalarSet)
			{
				double[] x = new double[(valueSet).Count];
				for (int i = 0; i < x.Length; i++)
				{
					x[i] = ((IScalarSet) valueSet).GetScalar(i);
				}

				ScalarSet newScalarSet = new ScalarSet(x);

                if (valueSet is ScalarSet)
                {
                    newScalarSet.MissingValueDefinition =
                        ((ScalarSet)valueSet).MissingValueDefinition;
                    newScalarSet.CompareDoublesEpsilon =
                        ((ScalarSet)valueSet).CompareDoublesEpsilon;
                }

                _values.Add(newScalarSet);
			}
			else if (valueSet is IVectorSet)
			{			
				Vector[] vectors = new Vector[valueSet.Count];
				for (int i = 0; i < vectors.Length; i++)
				{
					vectors[i] = new Vector(((IVectorSet) valueSet).GetVector(i).XComponent, ((IVectorSet) valueSet).GetVector(i).YComponent, ((IVectorSet) valueSet).GetVector(i).ZComponent);
				}
			  VectorSet newVectorSet = new VectorSet(vectors);
				_values.Add(newVectorSet);
			}
			else
			{
				throw new Exception("Invalid datatype used for values argument in method AddValues");
			}
	
			if (_doExtendedDataVerification)
			{
				CheckBuffer();
			}
		}
示例#8
0
        public void Run(ITimeStamp time)
        {
            //IScalarSet scalarSet = new ScalarSet();

            ScalarSet scalarSet = new ScalarSet((IScalarSet) _link.SourceComponent.GetValues(time,_link.ID));
            _earliestInputTime.ModifiedJulianDay = time.ModifiedJulianDay;
            _resultsBuffer.AddValues(time,scalarSet);
        }
示例#9
0
        /// <summary>
        /// Implementation of the same method in
        /// OpenMI.Standard.ILInkableComponent
        /// </summary>
        /// <param name="time">Time (ITimeSpan or ITimeStamp) for which values are requested</param>
        /// <param name="LinkID">LinkID associated to the requested values</param>
        /// <returns>The values</returns>
        public override IValueSet GetValues(ITime time, string LinkID)
        {
            try
            {
                CheckTimeArgumentInGetvaluesMethod(time);
                SendSourceAfterGetValuesCallEvent(time, LinkID);
                IValueSet engineResult = new ScalarSet();

                int outputLinkIndex = -999;
                for (int i = 0; i < _smartOutputLinks.Count; i++)
                {
                    if (((SmartOutputLink)_smartOutputLinks[i]).link.ID == LinkID)
                    {
                        outputLinkIndex = i;
                        break;
                    }
                }
                RunToTime(time, outputLinkIndex);

                engineResult = ((SmartOutputLink)_smartOutputLinks[outputLinkIndex]).GetValue(time);

                SendEvent(EventType.SourceBeforeGetValuesReturn);
                return engineResult;

            }
            catch (System.Exception e)
            {
                string message = "Exception in LinkableComponent. ComponentID: ";
                message += this.ComponentID;
                throw new System.Exception(message, e);
            }
        }
示例#10
0
		/// <summary>
		/// A ValueSet corresponding to a TimeSpan is calculated using interpolation or
		/// extrapolation in corresponding lists of ValueSets and TimeStamps.
		/// </summary>
		/// <param name="requestedTime">Time for which the ValueSet is requested</param>
		/// <returns>ValueSet that corresponds to requestedTime</returns>
		private IValueSet MapFromTimeStampsToTimeSpan(ITimeSpan requestedTime)
		{
			try
      {
        int	       m  = ((IValueSet)_values[0]).Count;
			//int        N  = _times.Count;								   	      // Number of time steps in buffer
			double[][] xr = new double[m][];                                      // Values to return
			double trb    = requestedTime.Start.ModifiedJulianDay;   // Begin time in requester time interval
			double tre    = requestedTime.End.ModifiedJulianDay;    // End time in requester time interval

			int nk; // number of components (scalars has only 1 and vectors has 3 (3 axis))

			if (_values[0] is IVectorSet)
			{
				nk = 3;
			}
			else
			{
				nk = 1;
			}
				
			for (int i = 0; i < m; i++)
			{
				xr[i] = new double[nk];
			}

			for (int i = 0; i < m; i++)
			{
				for (int k = 0; k < nk; k++)
				{
					xr[i][k] = 0;
				}
			}


			for (int n = 0; n < _times.Count-1; n++)
			{
				double tbn   = ((ITimeStamp) _times[n]).ModifiedJulianDay;
				double tbnp1 = ((ITimeStamp) _times[n+1]).ModifiedJulianDay;
				

				//---------------------------------------------------------------------------
				//    B:           <-------------------------->
				//    R:        <------------------------------------->
				// --------------------------------------------------------------------------
				if (trb <= tbn && tre >= tbnp1 )
				{
					for (int k = 1; k <= nk; k++)
					{
						for (int i = 0; i < m; i++) // for all values coorsponding to the same time interval
						{
							double sbin   = Support.GetVal((IValueSet)_values[n], i, k);
							double sbinp1 = Support.GetVal((IValueSet)_values[n+1], i, k);
							xr[i][k-1] += 0.5 * (sbin + sbinp1) * (tbnp1 - tbn)/(tre - trb);
						}
					}
				}

				//---------------------------------------------------------------------------
				//           Times[i] Interval:        t1|-----------------------|t2
				//           Requested Interval:          rt1|--------------|rt2
				// --------------------------------------------------------------------------
				else if (tbn <= trb && tre <= tbnp1) //cover all
				{
					for (int k = 1; k <= nk; k++)
					{
						for (int i = 0; i < m; i++) // for all values coorsponding to the same time interval
						{
							double sbin   = Support.GetVal((IValueSet)_values[n], i, k);
							double sbinp1 = Support.GetVal((IValueSet)_values[n+1], i, k);
							xr[i][k-1] += sbin + ((sbinp1 - sbin)/(tbnp1 - tbn))*((tre + trb)/2 - tbn);
						}
					}
				}

				//---------------------------------------------------------------------------
				//           Times[i] Interval:       t1|-----------------|t2
				//           Requested Interval:                 rt1|--------------|rt2
				// --------------------------------------------------------------------------
				else if (tbn < trb && trb < tbnp1 && tre > tbnp1)
				{
					for (int k = 1; k <= nk; k++)
					{
						for (int i = 0; i < m; i++) // for all values coorsponding to the same time interval
						{
							double sbin   = Support.GetVal((IValueSet)_values[n], i, k);
							double sbinp1 = Support.GetVal((IValueSet)_values[n+1], i, k);
							xr[i][k-1] +=  (sbinp1 - (sbinp1 - sbin)/(tbnp1 - tbn)*((tbnp1 - trb)/2))* (tbnp1 - trb)/(tre - trb);
						}
					}
				}

				//---------------------------------------------------------------------------
				//           Times[i] Interval:             t1|-----------------|t2
				//           Requested Interval:      rt1|--------------|rt2
				// --------------------------------------------------------------------------
				else if (trb < tbn && tre > tbn && tre < tbnp1)
				{
					for (int k = 1; k <= nk; k++)
					{
						for (int i = 0; i < m; i++) // for all values coorsponding to the same time interval
						{
							double sbin   = Support.GetVal((IValueSet)_values[n], i, k);
							double sbinp1 = Support.GetVal((IValueSet)_values[n+1], i, k);
							xr[i][k-1] += (sbin + (sbinp1 - sbin)/(tbnp1 - tbn)*((tre - tbn)/2)) * (tre - tbn)/(tre - trb);
						}
					}
				}
			}
			//--------------------------------------------------------------------------
			//              |--------|---------|--------| B
			//        |----------------|                  R
			//---------------------------------------------------------------------------
			double tb0   = ((ITimeStamp) _times[0]).ModifiedJulianDay;
			//double tb1   = ((ITimeStamp) _times[0]).ModifiedJulianDay;
			double tb1   = ((ITimeStamp) _times[1]).ModifiedJulianDay; // line above was corrected to this Gregersen Sep 15 2004
			double tbN_1 = ((ITimeStamp) _times[_times.Count-1]).ModifiedJulianDay;
			double tbN_2 = ((ITimeStamp) _times[_times.Count-2]).ModifiedJulianDay;
			
			if (trb < tb0 && tre > tb0)
			{
				for (int k = 1; k <= nk; k++)
				{
					for (int i = 0; i < m; i++)
					{
						double sbi0 = Support.GetVal((IValueSet)_values[0], i, k);
						double sbi1 = Support.GetVal((IValueSet)_values[1], i, k);
						xr[i][k-1] += ((tb0 - trb)/(tre - trb)) * (sbi0 - (1 - _relaxationFactor) * 0.5 * ((tb0 - trb)*(sbi1 - sbi0)/(tb1 - tb0)));
					}
				}
			}
			//-------------------------------------------------------------------------------------
			//              |--------|---------|--------| B
			//                                    |----------------|                  R
			//-------------------------------------------------------------------------------------
			if (tre > tbN_1 && trb < tbN_1)
			{
				for (int k = 1; k <= nk; k++)
				{
					for (int i = 0; i < m; i++)
					{
						double sbiN_1 = Support.GetVal((IValueSet)_values[_times.Count-1], i, k);
						double sbiN_2 = Support.GetVal((IValueSet)_values[_times.Count-2], i, k);
						xr[i][k-1] += ((tre - tbN_1)/(tre - trb)) * (sbiN_1 + (1 - _relaxationFactor) * 0.5 * ((tre - tbN_1)*(sbiN_1 - sbiN_2)/(tbN_1 - tbN_2)));
					}
				}
			}
			//-------------------------------------------------------------------------------------
			//              |--------|---------|--------| B
			//                                              |----------------|   R
			//-------------------------------------------------------------------------------------
			if (trb >= tbN_1)
			{
				
				for (int k = 1; k <= nk; k++)
				{
					for (int i = 0; i < m; i++)
					{
						double sbiN_1 = Support.GetVal((IValueSet)_values[_times.Count-1], i, k);
						double sbiN_2 = Support.GetVal((IValueSet)_values[_times.Count-2], i, k);
					
						xr[i][k-1] = sbiN_1 + (1 - _relaxationFactor) * ((sbiN_1 - sbiN_2)/(tbN_1 - tbN_2)) * ( 0.5 * (trb + tre) - tbN_1);
					}
				}
			}
			//-------------------------------------------------------------------------------------
			//                           |--------|---------|--------| B
			//        |----------------|   R
			//-------------------------------------------------------------------------------------
			if (tre <= tb0)
			{
				for (int k = 1; k <= nk; k++)
				{
					for (int i = 0; i < m; i++)
					{
						double sbi0 = Support.GetVal((IValueSet)_values[0], i, k);
						double sbi1 = Support.GetVal((IValueSet)_values[1], i, k);
						xr[i][k-1] = sbi0 - (1 - _relaxationFactor) * ((sbi1 - sbi0)/(tb1- tb0))*(tb0 - 0.5 * (trb + tre));
					}
				}
			}
			//-------------------------------------------------------------------------------------
        if (_values[0] is IVectorSet)
        {
          Vector [] vectors = new Vector[m]; 

          for (int i = 0; i < m; i++)
          {
            vectors[i] = new Vector(xr[i][0],xr[i][1],xr[i][2]);
          }

          VectorSet vectorSet = new VectorSet(vectors);

          return vectorSet;
        }
        else
        {
          double[] xx = new double[m];

          for (int i = 0; i < m; i++)
          {
            xx[i] = xr[i][0];
          }
				
          ScalarSet scalarSet = new ScalarSet(xx);

          return scalarSet;
        }
      }
      catch (Exception e)
      {
        throw new Exception("MapFromTimeStampsToTimeSpan Failed",e);
      }
		}
        /// <summary>
        /// Get values for:
        ///     flow at outlet
        ///     concentrations at outlet
        ///     ponded water column
        /// </summary>
        public IValueSet GetValues(string QuantityID, string ElementSetID)
        {

            getValuesWatch.Start();

            double[] returnValues;

            if (QuantityID == qtdOutflow.ID)
            {
                returnValues = new double[1];
                returnValues[0] = mohidLandEngine.GetOutletFlow(drainageNetworkInstanceID);
            }
            else if (QuantityID == qtdWaterColumn.ID)
            {

                returnValues = new double[numberOfWaterPoints];
                mohidLandEngine.GetPondedWaterColumn(runoffInstanceID, numberOfWaterPoints, ref returnValues);

            }
            else if (QuantityID == qtdFlowToStorm.ID)
            {
                int numberOfOutflowNodes = mohidLandEngine.GetNumberOfStormWaterOutFlowNodes(drainageNetworkInstanceID);
                returnValues = new double[numberOfOutflowNodes];
                mohidLandEngine.GetStormWaterOutflow(drainageNetworkInstanceID, numberOfOutflowNodes, ref returnValues);
            }
            else //Concentration of properties
            {
                returnValues = new double[1];
                
                int propertyID = Convert.ToInt32(QuantityID);

                returnValues[0] = mohidLandEngine.GetOutletFlowConcentration(drainageNetworkInstanceID, propertyID);
            }

            ScalarSet values = new ScalarSet(returnValues);

            getValuesWatch.Stop();

            return values;
        }
        public override IValueSet GetValues(ITime time, string LinkID)
        {
            // synchronize access to this method to ensure serial execution
            // in case it is called in parallel
            lock (syncLock)
            {
                try
                {
                    CheckTimeArgumentInGetvaluesMethod(time);
                    SendSourceAfterGetValuesCallEvent(time, LinkID);
                    IValueSet engineResult = new ScalarSet();

                    int outputLinkIndex = -999;
                    for (int i = 0; i < _smartOutputLinks.Count; i++)
                    {
                        if (((SmartOutputLink)_smartOutputLinks[i]).link.ID == LinkID)
                        {
                            outputLinkIndex = i;
                            break;
                        }
                    }

                    if (_isBusy == false)
                    {
                        //while(IsLater(time,_engineApiAccess.GetCurrentTime()))
                        while (IsLater(time, ((SmartOutputLink)_smartOutputLinks[outputLinkIndex]).GetLastBufferedTime()))
                        {
                            _isBusy = true;

                            //Update input links
                            // new parallel code
                            var threadList = new ArrayList();
                            foreach (SmartInputLink smartInputLink in _smartInputLinks)
                            {
                                var thread = new Thread(delegate()
                                {
                                    smartInputLink.UpdateInput();
                                });
                                thread.IsBackground = true;
                                threadList.Add(thread);
                                thread.Start();
                            }
                            foreach (Thread thread in threadList)
                            {
                                thread.Join();
                            }
                            // original serial code
                            //foreach(SmartInputLink smartInputLink in _smartInputLinks)
                            //{
                            //    smartInputLink.UpdateInput();
                            //}

                            _isBusy = false;

                            //Perform Timestep
                            if (_engineApiAccess.PerformTimeStep())
                            {
                                //Update buffer with engine values, Time is timestamp
                                foreach (SmartOutputLink smartOutputLink in _smartOutputLinks)
                                {
                                    smartOutputLink.UpdateBuffer();
                                }

                                SendEvent(EventType.DataChanged);
                            }
                        }
                    }

                    engineResult = ((SmartOutputLink)_smartOutputLinks[outputLinkIndex]).GetValue(time);

                    SendEvent(EventType.SourceBeforeGetValuesReturn);
                    return engineResult;

                }
                catch (System.Exception e)
                {
                    string message = "Exception in LinkableComponent. ComponentID: ";
                    message += this.ComponentID;
                    throw new System.Exception(message, e);
                }
            }
        }
示例#13
0
        public global::OpenMI.Standard.IValueSet GetValues(string QuantityID, string ElementSetID)
        {
            if (QuantityID != ElementSetID)
                throw new ApplicationException("Element sets should be named after quantities");
            if (!_elementSets.Contains(ElementSetID))
                throw new ApplicationException("Unknown element set '" + ElementSetID + "'");
            ElementSet elementSet = (ElementSet) _elementSets[ElementSetID];
            int count = elementSet.ElementCount;
            double[] returnValues = new double[count];

            // This is O (e * s * q) instead of O (1).
            for (int e = 0; e < count; e++)
            {
                Element element = elementSet.GetElement(e);
                string ColumnID = element.ID;
                if (!_elementScopes.Contains (element))
                    throw new ApplicationException("Unknown element for '" + ColumnID + "'/'" + QuantityID + "'");
                Scope scope = (Scope) _elementScopes[element];

                if (scope.HasNumber(QuantityID))
                    returnValues[e] = scope.Number(QuantityID);
                else
                    returnValues[e] = GetMissingValueDefinition();
            }
            Oatc.OpenMI.Sdk.Backbone.ScalarSet values = new Oatc.OpenMI.Sdk.Backbone.ScalarSet(returnValues);
            return values;
        }
		[Test] // testing the Initialise method
		public void MapValues()
		{
			ElementSet gridElementSet = new ElementSet("RegularGrid","RegularGrid",ElementType.XYPolygon, new SpatialReference("ref"));
			ElementSet fourPointsElementSet = new ElementSet("4 points","4P",ElementType.XYPoint,new SpatialReference("DummyID")); 

			Vertex v_0_20  = new Vertex(0,20,0);
			Vertex v_0_10  = new Vertex(0,10,0);
			Vertex v_0_0   = new Vertex(0, 0,0);
			Vertex v_0_15  = new Vertex(0,15,0);
			Vertex v_5_15  = new Vertex(5,15,0);
			Vertex v_10_20 = new Vertex(10,20,0);
			Vertex v_10_15 = new Vertex(10,15,0);
			Vertex v_10_10 = new Vertex(10,10,0);
			Vertex v_10_0  = new Vertex(10, 0,0);
			Vertex v_15_15 = new Vertex(15,15,0);
			Vertex v_15_5  = new Vertex(15,5,0);
			Vertex v_20_20 = new Vertex(20,20,0);
			Vertex v_20_10 = new Vertex(20,10,0);

			Element square1 = new Element("square1");
			Element square2 = new Element("square2");
			Element square3 = new Element("square3");

			square1.AddVertex(v_0_20);
			square1.AddVertex(v_0_10);
			square1.AddVertex(v_10_10);
			square1.AddVertex(v_10_20);

			square2.AddVertex(v_10_20);
			square2.AddVertex(v_10_10);
			square2.AddVertex(v_20_10);
			square2.AddVertex(v_20_20);

			square3.AddVertex(v_0_10);
			square3.AddVertex(v_0_0);
			square3.AddVertex(v_10_0);
			square3.AddVertex(v_10_10);

			gridElementSet.AddElement(square1);
			gridElementSet.AddElement(square2);
			gridElementSet.AddElement(square3);

			Element point_5_15  = new Element("point 5, 15");
			Element point_10_15 = new Element("point 10, 15");
			Element point_15_15 = new Element("point 15, 15");
			Element point_15_5  = new Element("point 15, 5");

			point_5_15.AddVertex(v_5_15);
			point_10_15.AddVertex(v_10_15);
			point_15_15.AddVertex(v_15_15);
			point_15_5.AddVertex(v_15_5);

			fourPointsElementSet.AddElement(point_5_15);
			fourPointsElementSet.AddElement(point_10_15);
			fourPointsElementSet.AddElement(point_15_15);
			fourPointsElementSet.AddElement(point_15_5);
			ScalarSet fourPointsScalarSet = new ScalarSet();
			ScalarSet gridScalarSet = new ScalarSet();      
      
			ElementMapper elementMapper = new ElementMapper();
      
			// point to polygon  
      
			ArrayList methodDescriptions = elementMapper.GetAvailableMethods(fourPointsElementSet.ElementType, gridElementSet.ElementType);
			elementMapper.Initialise(methodDescriptions[0].ToString(), fourPointsElementSet, gridElementSet);
			fourPointsScalarSet.data = new double[4] { 0, 10, 20, 30 };
			gridScalarSet = (ScalarSet)elementMapper.MapValues(fourPointsScalarSet);
			Assert.AreEqual(5, gridScalarSet.data[0]);
			Assert.AreEqual(20, gridScalarSet.data[1]);
			Assert.AreEqual(0, gridScalarSet.data[2]);
			// polygon to point
			methodDescriptions = elementMapper.GetAvailableMethods(gridElementSet.ElementType, fourPointsElementSet.ElementType);
			elementMapper.Initialise(methodDescriptions[0].ToString(), gridElementSet, fourPointsElementSet);
			fourPointsScalarSet = (ScalarSet)elementMapper.MapValues(gridScalarSet);
			Assert.AreEqual(5, fourPointsScalarSet.data[0]);
			Assert.AreEqual(5, fourPointsScalarSet.data[1]);
			Assert.AreEqual(20, fourPointsScalarSet.data[2]);
			Assert.AreEqual(0, fourPointsScalarSet.data[3]);
		}
示例#15
0
		/// <summary>
		/// Returns the ValueSet that corresponds to requestTime. The ValueSet may be found by 
		/// interpolation, extrapolation and/or aggregation.
		/// </summary>
		/// <param name="requestedTime">time for which the value is requested</param>
		/// <returns>valueSet that corresponds to requestTime</returns>
		public IValueSet GetValues(ITime requestedTime)
		{
			if (_doExtendedDataVerification)
			{
				CheckTime(requestedTime);
				CheckBuffer();
			}
			IValueSet returnValueSet;
			if (_values.Count == 0)
			{
				returnValueSet = new ScalarSet(); 
			}
			else if (_values.Count == 1)
			{
				returnValueSet = MakeCopyOfValues();
			}
			else if (requestedTime is ITimeStamp && _times[0] is ITimeStamp)
			{
				returnValueSet = MapFromTimeStampsToTimeStamp((ITimeStamp) requestedTime);
			}
			else if (requestedTime is ITimeSpan && _times[0] is ITimeSpan)
			{
				returnValueSet = MapFromTimeSpansToTimeSpan((ITimeSpan) requestedTime);
			}
			else if (requestedTime is ITimeSpan && _times[0] is ITimeStamp)
			{
				returnValueSet = MapFromTimeStampsToTimeSpan((ITimeSpan) requestedTime);
			}
			else if (requestedTime is ITimeStamp && _times[0] is ITimeSpan)
			{
				returnValueSet = MapFromTimeSpansToTimeStamp((ITimeStamp) requestedTime);
			}
			else
			{
				throw new Exception("Requested TimeMapping not available in SmartWrapper Class");
			}
			return returnValueSet;
		}
		public IValueSet GetValues(string QuantityID, string ElementSetID)
		{
			char[] separator = new char[]{':'};
			double[] x;
			
			if(ElementSetID.Split(separator)[0] == "Branch")
			{
				 x = new double[1];
				int branchIndex = Convert.ToInt32(ElementSetID.Split(separator)[1]);
				if (QuantityID == "Flow")
				{
					x[0] = _flow[branchIndex];
				}
				else if (QuantityID == "Leakage")
				{
					x[0] = _leakage[branchIndex];
				}
				else 
				{
					throw new Exception("Quanity ID not recognized in GetValues method");
				}

			}
			else if(ElementSetID.Split(separator)[0] == "WholeRiver")
			{
				x = new double[_leakage.Length];
				for (int i = 0; i < _leakage.Length; i++)
				{
					x[i] = _leakage[i];
				}
			}
			else 
			{
				throw new Exception("Failed to recognize ElementSetID in method GetValues");
			}

			ScalarSet scalarSet = new ScalarSet(x);
			return scalarSet;
		}
 /// <summary>
 ///     This method is used to extract values from an upstream component.
 /// </summary>
 /// <param name="QuantityID">The input Quantity ID</param>
 /// <param name="ElementSetID">The input Element Set ID</param>
 /// <returns>the values saved under the matching QuantityID and ElementSetID, from an upstream component</returns>
 public IValueSet GetValues(string QuantityID, string ElementSetID)
 {
     var rows = _values.Select("QuantityID = '" + QuantityID + "' AND ElementSetID = '" + ElementSetID + "'");
     if (rows.Length == 1)
         return (IValueSet)rows[0].ItemArray[2];
     var es = _elementSets[ElementSetID];
     var ss = new ScalarSet();
     ss.data = new double[es.ElementCount];
     return ss;
 }
示例#18
0
    /// <summary>
    /// A ValueSet corresponding to a TimeSpan is calculated using interpolation or
    /// extrapolation in corresponding lists of ValueSets and TimeSpans.
    /// </summary>
    /// <param name="requestedTimeStamp">Time for which the ValueSet is requested</param>
    /// <returns>ValueSet that corresponds to requestedTime</returns>
		private IValueSet MapFromTimeSpansToTimeStamp(ITimeStamp requestedTimeStamp)
		{
      try
      {
        int	     m  = ((IValueSet)_values[0]).Count;
        double[][] xr = new double[m][];                             // Values to return
        double   tr = requestedTimeStamp.ModifiedJulianDay; 	     // Requested TimeStamp

        int nk; // number of components (scalars has only 1 and vectors has 3 (3 axis))

        if (_values[0] is IVectorSet)
        {
          nk = 3;
        }
        else
        {
          nk = 1;
        }

        for (int i = 0; i < m; i++)
        {
          xr[i] = new double[nk];
        }

        //---------------------------------------------------------------------------
        //  Buffered TimesSpans:  |          >tbb0<  ..........  >tbbN<
        //  Requested TimeStamp:  |    >tr<
        //                         -----------------------------------------> t
        // --------------------------------------------------------------------------
        if (tr <= ((ITimeSpan)_times[0]).Start.ModifiedJulianDay )
        {
          double tbb0 = ((ITimeSpan) _times[0]).Start.ModifiedJulianDay;
          double tbb1 = ((ITimeSpan) _times[1]).Start.ModifiedJulianDay;
				
          for (int k = 1; k <= nk; k++)
          {
            for (int i = 0; i < m ; i++) //For each Vector in buffered VectorSet [0]
            {
              double sbi0       = Support.GetVal((IValueSet)_values[0], i, k);
              double sbi1       = Support.GetVal((IValueSet)_values[1], i, k);
              xr[i][k-1] = ((sbi0 - sbi1)/(tbb0 - tbb1))*(tr - tbb0) * (1 - _relaxationFactor) + sbi0;
            }
          }
        }

          //---------------------------------------------------------------------------
          //  Buffered TimesSpans:  |    >tbb0<   .................  >tbbN_1<
          //  Requested TimeStamp:  |                                             >tr<
          //                         ---------------------------------------------------> t
          // --------------------------------------------------------------------------
        else if (tr >= ((ITimeSpan) _times[_times.Count - 1]).End.ModifiedJulianDay)
        {
          double tbeN_2 = ((ITimeSpan) _times[_times.Count - 2]).End.ModifiedJulianDay;
          double tbeN_1 = ((ITimeSpan) _times[_times.Count - 1]).End.ModifiedJulianDay;

          for (int k = 1; k <= nk; k++)
          {
            for (int i = 0; i < m; i++) //For each Vector in buffered VectorSet [N-1]
            {
              double sbiN_2 = Support.GetVal((IValueSet)_values[_times.Count-2], i, k);
              double sbiN_1 = Support.GetVal((IValueSet)_values[_times.Count-1], i, k);

              xr[i][k-1] = ((sbiN_1 - sbiN_2)/(tbeN_1 - tbeN_2))*(tr - tbeN_1)*(1 - _relaxationFactor) + sbiN_1;
            }
          }
        }

          //---------------------------------------------------------------------------
          //  Availeble TimesSpans:  |    >tbb0<   ......................  >tbbN_1<
          //  Requested TimeStamp:   |                          >tr<
          //                         -------------------------------------------------> t
          // --------------------------------------------------------------------------
        else
        {
          for (int n = _times.Count - 1; n >= 0 ; n--)
          {
            double tbbn = ((ITimeSpan) _times[n]).Start.ModifiedJulianDay;
            double tben = ((ITimeSpan) _times[n]).End.ModifiedJulianDay;

            if ( tbbn <= tr && tr < tben)
            {
              for (int k = 1; k <= nk; k++)
              {
                for (int i = 0; i < m; i++) //For each Vector in buffered VectorSet [n]
                {
                  xr[i][k-1]     = Support.GetVal((IValueSet)_values[n], i, k);
                }
              }
              break;
            }
          }
        }

        //----------------------------------------------------------------------------------------------
	
		
        if (_values[0] is IVectorSet)
        {
          Vector [] vectors = new Vector[m]; 

          for (int i = 0; i < m; i++)
          {
            vectors[i] = new Vector(xr[i][0],xr[i][1],xr[i][2]);
          }

          VectorSet vectorSet = new VectorSet(vectors);

          return vectorSet;
        }
        else
        {
          double[] xx = new double[m];

          for (int i = 0; i < m; i++)
          {
            xx[i] = xr[i][0];
          }
				
          ScalarSet scalarSet = new ScalarSet(xx);

          return scalarSet;
        }
      }
      catch (Exception e)
      {
        throw new Exception("MapFromTimeSpansToTimeStamp Failed",e);
      }
		}
示例#19
0
    /// <summary>
    /// A ValueSet corresponding to a TimeStamp is calculated using interpolation or
    /// extrapolation in corresponding lists of ValueSets and TimeStamps.
    /// </summary>
		/// <param name="requestedTimeStamp">TimeStamp for which the values are requested</param>
    /// <returns>ValueSet that corresponds to the requested time stamp</returns>
		private IValueSet MapFromTimeStampsToTimeStamp(ITimeStamp requestedTimeStamp)
		{
      try
      {
        int	     m  = ((IValueSet)_values[0]).Count;
        double[][] xr = new double[m][];                             // Values to return
        double   tr = requestedTimeStamp.ModifiedJulianDay;		     // Requested TimeStamp

        int nk; // number of components (scalars has only 1 and vectors has 3 (3 axis))

        if (_values[0] is IVectorSet)
        {
          nk = 3;
        }
        else
        {
          nk = 1;
        }

        for (int i = 0; i < m; i++)
        {
          xr[i] = new double[nk];
        }

        //---------------------------------------------------------------------------
        //  Buffered TimesStamps: |          >tb0<   >tb1<   >tb2<  >tbN<
        //  Requested TimeStamp:  |    >tr<
        //                         -----------------------------------------> t
        // --------------------------------------------------------------------------
        if (tr <= ((ITimeStamp)_times[0]).ModifiedJulianDay )
        {
          double tb0 = ((ITimeStamp) _times[0]).ModifiedJulianDay;
          double tb1 = ((ITimeStamp) _times[1]).ModifiedJulianDay;

          for (int k = 1; k <= nk; k++)
          {
            for (int i = 0; i < m ; i++) //For each Vector in buffered VectorSet [0]
            {
              double sbi0 = Support.GetVal((IValueSet)_values[0], i, k);
              double sbi1       = Support.GetVal((IValueSet)_values[1], i, k);
              xr[i][k-1] = ((sbi0 - sbi1)/(tb0 - tb1))*(tr - tb0) * (1 - _relaxationFactor) + sbi0;
            }
          }
        }

          //---------------------------------------------------------------------------
          //  Buffered TimesStamps: |    >tb0<   >tb1<   >tb2<  >tbN_2<  >tbN_1<
          //  Requested TimeStamp:  |                                             >tr<
          //                         ---------------------------------------------------> t
          // --------------------------------------------------------------------------
        else if (tr > ((ITimeStamp) _times[_times.Count - 1]).ModifiedJulianDay)
        {
          double tbN_2 = ((ITimeStamp) _times[_times.Count - 2]).ModifiedJulianDay;
          double tbN_1 = ((ITimeStamp) _times[_times.Count - 1]).ModifiedJulianDay;

          for (int k = 1; k <= nk; k++)
          {
            for (int i = 0; i < m; i++) //For each Vector in buffered VectorSet [N-1]
            {
              double sbiN_2     = Support.GetVal((IValueSet)_values[_times.Count-2], i, k);
              double sbiN_1     = Support.GetVal((IValueSet)_values[_times.Count-1], i, k);

              xr[i][k-1] = ((sbiN_1 - sbiN_2)/(tbN_1 - tbN_2))*(tr - tbN_1)*(1 - _relaxationFactor) + sbiN_1;
            }
          }
        }

          //---------------------------------------------------------------------------
          //  Availeble TimesStamps: |    >tb0<   >tb1<  >tbna<       >tnb<   >tbN_1<  >tbN_2<
          //  Requested TimeStamp:   |                          >tr<
          //                         -------------------------------------------------> t
          // --------------------------------------------------------------------------
        else
        {
          for (int n = _times.Count - 2; n >= 0 ; n--)
          {
            double tbn1 = ((ITimeStamp) _times[n]).ModifiedJulianDay;
            double tbn2 = ((ITimeStamp) _times[n+1]).ModifiedJulianDay;

            if ( tbn1 <= tr && tr <= tbn2)
            {
              for (int k = 1; k <= nk; k++)
              {
                for (int i = 0; i < m; i++) //For each Vector in buffered VectorSet [n]
                {
					IValueSet valueSet_n = (IValueSet)_values[n];
					double sbin1 = Support.GetVal(valueSet_n, i, k);

					IValueSet valueSet_nPlus1 = (IValueSet)_values[n+1];
                	double sbin2 = Support.GetVal(valueSet_nPlus1, i, k);;

					if ( valueSet_n.IsValid(i) && valueSet_nPlus1.IsValid(i))
					{
						xr[i][k-1]     = ((sbin2 - sbin1)/(tbn2 - tbn1)) * (tr - tbn1) + sbin1;
					}
					else if ( valueSet_n.IsValid(i) ) 
					{
						xr[i][k-1] = sbin1;
					}
					else if ( valueSet_nPlus1.IsValid(i) ) 
					{
						xr[i][k-1] = sbin2;
					}
					else
					{
						// both invalid, set to one of the (==invalid) values
						xr[i][k-1] = sbin1;
					}
                }
              }
              break;
            }
          }
        }
        //----------------------------------------------------------------------------------------------
        if (_values[0] is IVectorSet)
        {
          Vector [] vectors = new Vector[m]; 

          for (int i = 0; i < m; i++)
          {
            vectors[i] = new Vector(xr[i][0],xr[i][1],xr[i][2]);
          }

          VectorSet vectorSet = new VectorSet(vectors);

          return vectorSet;
        }
        else
        {
          double[] xx = new double[m];

          for (int i = 0; i < m; i++)
          {
            xx[i] = xr[i][0];
          }
				
          ScalarSet scalarSet = new ScalarSet(xx);

          return scalarSet;
        }
      }
      catch (Exception e)
      {
        throw new Exception("MapFromTimeStampsToTimeStamp Failed",e);
      }
		}
示例#20
0
		/// <summary>
		/// Makes a copy of the first ValueSet in the list of valueSets
		/// </summary>
		/// <returns></returns>
		private IValueSet MakeCopyOfValues()
		{

			if (_values[0] is IScalarSet)
			{
				int NumberOfScalarsInEachScalarSet = ((IScalarSet) _values[0]).Count;
				double[] x = new Double[NumberOfScalarsInEachScalarSet];
				for (int i = 0; i < NumberOfScalarsInEachScalarSet; i++)
				{
					x[i] = ((IScalarSet) _values[0]).GetScalar(i);
				}
				ScalarSet scalarSet = new ScalarSet(x);
				return scalarSet;
			}

			else // _values[0] is VectorSet
			{
				
				int NumberOfVectorsInEachVectorSet = ((IVectorSet) _values[0]).Count;

				Vector[] vectors = new Vector[NumberOfVectorsInEachVectorSet];

				for (int i = 0; i < NumberOfVectorsInEachVectorSet; i++)
				{
          Vector vector;         
          double x = ((IVectorSet)_values[0]).GetVector(i).XComponent;
					double y = ((IVectorSet) _values[0]).GetVector(i).YComponent;
					double z = ((IVectorSet) _values[0]).GetVector(i).ZComponent;
					vector = new Vector(x, y, z);
					vectors[i] = vector;

				}
				VectorSet vectorSet = new VectorSet(vectors);
							
				return vectorSet;
			}
		}
示例#21
0
 public void Init()
 {
     double[] values = {1.0,2.0,3.0};
     scalarSet = new ScalarSet(values);
 }
示例#22
0
 /// <summary>
 /// MapValues calculates a IValueSet through multiplication of an inputValues IValueSet
 /// vector or matrix (ScalarSet or VectorSet) on to the mapping maprix. IScalarSets maps
 /// to IScalarSets and IVectorSets maps to IVectorSets.
 /// </summary>
 /// 
 /// <remarks>
 /// Mapvalues is called every time a georeferenced link is evaluated.
 /// </remarks>
 /// 
 /// <param name="inputValues">IValueSet of values to be mapped.</param>
 /// 
 /// <returns>
 /// A IValueSet found by mapping of the inputValues on to the toElementSet.
 /// </returns>
 public IValueSet MapValues(IValueSet inputValues)
 {
   if (!_isInitialised)
   {
     throw new System.Exception("ElementMapper objects needs to be initialised before the MapValue method can be used");
   }
   if (!inputValues.Count.Equals(_numberOfColumns))
   {
     throw new System.Exception("Dimension mismatch between inputValues and mapping matrix");
   }
   if (inputValues is IScalarSet)
   {
     double[] outValues = new double[_numberOfRows];
     //--- Multiply the Values vector with the MappingMatrix ---
     for (int i = 0; i < _numberOfRows; i++)
     {
       outValues[i] = 0;
       for (int n = 0; n < _numberOfColumns; n++)
       {
         outValues[i] += _mappingMatrix[i,n] * ((IScalarSet) inputValues).GetScalar(n); //(remove)inValues[n];
       }
     }
     ScalarSet outputValues = new ScalarSet(outValues);
     return outputValues;
   }
   else if (inputValues is IVectorSet)
   {
     Vector[] outValues = new Vector[_numberOfRows];
     //--- Multiply the Values vector with the MappingMatrix ---
     for (int i = 0; i < _numberOfRows; i++)
     {
       outValues[i].XComponent = 0;
       outValues[i].YComponent = 0;
       outValues[i].ZComponent = 0;
       for (int n = 0; n < _numberOfColumns; n++)
       {
         outValues[i].XComponent += _mappingMatrix[i,n] * ((IVectorSet) inputValues).GetVector(n).XComponent;
         outValues[i].YComponent += _mappingMatrix[i,n] * ((IVectorSet) inputValues).GetVector(n).YComponent;
         outValues[i].ZComponent += _mappingMatrix[i,n] * ((IVectorSet) inputValues).GetVector(n).ZComponent;
       }
     }
     VectorSet outputValues = new VectorSet(outValues);
     return outputValues;
   }
   else
   {
     throw new System.Exception("Invalid datatype used for inputValues parameter. MapValues failed");
   }
 }
示例#23
0
 public void Constructor()
 {
     ScalarSet scalarSet2 = new ScalarSet(scalarSet);
     Assert.AreEqual(scalarSet,scalarSet2);
 }
		/// <summary>
		/// Implementation of the same method in
		/// OpenMI.Standard.ILInkableComponent
		/// </summary>
		/// <param name="time">Time (ITimeSpan or ITimeStamp) for which values are requested</param>
		/// <param name="LinkID">LinkID associated to the requested values</param>
		/// <returns>The values</returns>
		public override IValueSet GetValues(ITime time, string LinkID)
		{
			try
			{
				CheckTimeArgumentInGetvaluesMethod(time);
				SendSourceAfterGetValuesCallEvent(time, LinkID);
				IValueSet engineResult = new ScalarSet();
 
                int outputLinkIndex = -999;
                for (int i = 0; i < _smartOutputLinks.Count; i++)
                {
                    if ( ((SmartOutputLink) _smartOutputLinks[i]).link.ID == LinkID)
                    {
                        outputLinkIndex = i;
                        break;
                    }
                }
			
				if (_isBusy==false)
				{
					//while(IsLater(time,_engineApiAccess.GetCurrentTime()))
                    while(IsLater(time, ((SmartOutputLink) _smartOutputLinks[outputLinkIndex]).GetLastBufferedTime()))
					{
						_isBusy=true;

						//Update input links
                        foreach(SmartInputLink smartInputLink in _smartInputLinks)
                        {
                            smartInputLink.UpdateInput();
                        }
						_isBusy=false; 

						//Perform Timestep
						if(_engineApiAccess.PerformTimeStep()) 
						{
							//Update buffer with engine values, Time is timestamp
                            foreach (SmartOutputLink smartOutputLink in _smartOutputLinks)
                            {
                                smartOutputLink.UpdateBuffer();
                            }

							SendEvent(EventType.DataChanged);
						}
					}
				}

                engineResult = ((SmartOutputLink)_smartOutputLinks[outputLinkIndex]).GetValue(time);
    
				SendEvent(EventType.SourceBeforeGetValuesReturn);
				return engineResult;
		
			}
			catch (System.Exception e)
			{
				string message = "Exception in LinkableComponent. ComponentID: ";
				message += this.ComponentID;
				throw new System.Exception(message,e);
			}
		}
        public void RunSimulationWithInputAndOutput()
        {
            ITimeSpan modelSpan = mohidWaterEngineWrapper.GetTimeHorizon();
            double now = modelSpan.Start.ModifiedJulianDay;

            Stopwatch win = new Stopwatch();
            Stopwatch wout = new Stopwatch();
            Stopwatch wengine = new Stopwatch();

            while (now < modelSpan.End.ModifiedJulianDay)
            {

                DateTime currentTime = CalendarConverter.ModifiedJulian2Gregorian(now);
                DateTime intitalTime =
                    CalendarConverter.ModifiedJulian2Gregorian(
                        mohidWaterEngineWrapper.GetTimeHorizon().Start.ModifiedJulianDay);

                Console.WriteLine(currentTime.ToString());

                wengine.Start();
                mohidWaterEngineWrapper.PerformTimeStep();
                wengine.Stop();

                wout.Start();
                //Gets outputs Items
                for (int i = 0; i < mohidWaterEngineWrapper.GetOutputExchangeItemCount(); i++)
                {
                    OutputExchangeItem ouputItem = mohidWaterEngineWrapper.GetOutputExchangeItem(i);

                    IValueSet values = mohidWaterEngineWrapper.GetValues(ouputItem.Quantity.ID, ouputItem.ElementSet.ID);

                }
                wout.Stop();

                //Sets Input Items
                win.Start();
                for (int i = 0; i < mohidWaterEngineWrapper.GetInputExchangeItemCount(); i++)
                {
                    InputExchangeItem inputItem = mohidWaterEngineWrapper.GetInputExchangeItem(i);

                    double[] aux = new double[inputItem.ElementSet.ElementCount];
                    for (int j = 0; j < inputItem.ElementSet.ElementCount; j++)
                    {
                        aux[j] = 0;
                    }
                    IValueSet values = new ScalarSet(aux);

                    //mohidLandEngineWrapper.SetValues(inputItem.Quantity.ID, inputItem.ElementSet.ID, values);

                }
                win.Stop();

                now = mohidWaterEngineWrapper.GetEarliestNeededTime().ModifiedJulianDay;
            }

            Console.WriteLine("Input Exchange:  " + win.ElapsedMilliseconds.ToString());
            Console.WriteLine("Output Exchange: " + wout.ElapsedMilliseconds.ToString());
            Console.WriteLine("Engine:          " + wengine.ElapsedMilliseconds.ToString());
        }
        public override IValueSet GetValues(ITime time, string LinkID)
        {
            try
            {
                CheckTimeArgumentInGetvaluesMethod(time);
                SendSourceAfterGetValuesCallEvent(time, LinkID);
                IValueSet engineResult = new ScalarSet();

                // find the output link being requested
                var link = FindLinkWithId(LinkID);

                // log this call
                traceFile.Append("GetValues:" + link.link.SourceQuantity.ID + "@" + ((TimeStamp)time).ModifiedJulianDay);

                // update our link monitor
                prefetchHelper.updateLastRequestedTime(link.link, (TimeStamp)time);

                // retrieve the result from the link
                engineResult = link.GetValue(time, prefetchHelper);

                SendEvent(EventType.SourceBeforeGetValuesReturn);
                return engineResult;
            }
            catch (Exception e)
            {
                var message = "Exception in LinkableComponent:ID: ";
                message += ComponentID;
                throw new Exception(message, e);
            }
        }
 public void AppendValueSet(String quantityId, IElementSet elementSet, ScalarSet valueSet)
 {
     Append(quantityId);
     for (var index = 0; index < elementSet.ElementCount; index++)
     {
         var id = elementSet.GetElementID(index);
         Append(id + ":" + valueSet.GetScalar(index));
     }
 }
示例#28
0
		/// <summary>
		/// A ValueSet corresponding to a TimeSpan is calculated using interpolation or
		/// extrapolation in corresponding lists of ValueSets and TimeSpans.
		/// </summary>
		/// <param name="requestedTime">Time for which the ValueSet is requested</param>
		/// <returns>ValueSet that corresponds to requestedTime</returns>
		private IValueSet MapFromTimeSpansToTimeSpan(ITimeSpan requestedTime)

		{
      try
      {
        int	       m  = ((IValueSet)_values[0]).Count;
        double[][] xr = new double[m][];                                       // Values to return
        double trb    = requestedTime.Start.ModifiedJulianDay;   // Begin time in requester time interval
        double tre    = requestedTime.End.ModifiedJulianDay;     // End time in requester time interval

        int nk; // number of components (scalars has only 1 and vectors has 3 (3 axis))

        if (_values[0] is IVectorSet)
        {
          nk = 3;
        }
        else
        {
          nk = 1;
        }
				
        for (int i = 0; i < m; i++)
        {
          xr[i] = new double[nk];
        }

        for (int i = 0; i < m; i++)
        {
          for (int k = 0; k < nk; k++)
          {
            xr[i][k] = 0;
          }
        }

        for (int n = 0; n < _times.Count; n++)
        {
          double tbbn = ((ITimeSpan) _times[n]).Start.ModifiedJulianDay;
          double tben = ((ITimeSpan) _times[n]).End.ModifiedJulianDay;

          //---------------------------------------------------------------------------
          //    B:           <-------------------------->
          //    R:        <------------------------------------->
          // --------------------------------------------------------------------------
          if (trb <= tbbn && tre >= tben ) //Buffered TimeSpan fully included in requested TimeSpan
          {
            for (int k = 1; k <= nk; k++)
            {
              for (int i = 0; i < m; i++) // for all values coorsponding to the same time interval
              {
                double sbin = Support.GetVal((IValueSet)_values[n], i, k);
                xr[i][k-1] += sbin * (tben - tbbn)/(tre - trb);
              }
            }
          }

            //---------------------------------------------------------------------------
            //           Times[i] Interval:        t1|-----------------------|t2
            //           Requested Interval:          rt1|--------------|rt2
            // --------------------------------------------------------------------------
          else if (tbbn <= trb && tre <= tben) //cover all
          {
            for (int k = 1; k <= nk; k++)
            {
              for (int i = 0; i < m; i++) // for all values coorsponding to the same time interval
              {
                xr[i][k-1] += Support.GetVal((IValueSet)_values[n], i, k);
              }
            }
          }

            //---------------------------------------------------------------------------
            //           Times[i] Interval:       t1|-----------------|t2
            //           Requested Interval:                 rt1|--------------|rt2
            // --------------------------------------------------------------------------
          else if (tbbn < trb && trb < tben && tre > tben)
          {
            for (int k = 1; k <= nk; k++)
            {
              for (int i = 0; i < m; i++) // for all values coorsponding to the same time interval
              {
                double sbin = Support.GetVal((IValueSet)_values[n], i, k);
                xr[i][k-1] += sbin * (tben - trb)/(tre - trb);
              }
            }
          }

            //---------------------------------------------------------------------------
            //           Times[i] Interval:             t1|-----------------|t2
            //           Requested Interval:      rt1|--------------|rt2
            // --------------------------------------------------------------------------
          else if (trb < tbbn && tre > tbbn && tre < tben)
          {
            for (int k = 1; k <= nk; k++)
            {
              for (int i = 0; i < m; i++) // for all values coorsponding to the same time interval
              {
                double sbin = Support.GetVal((IValueSet)_values[n], i, k);
                xr[i][k-1] += sbin * (tre - tbbn)/(tre - trb);
              }
            }
          }
        }

        //--------------------------------------------------------------------------
        //              |--------|---------|--------| B
        //        |----------------|                  R
        //---------------------------------------------------------------------------
        double tbb0 = ((ITimeSpan) _times[0]).Start.ModifiedJulianDay;
        double tbe0 = ((ITimeSpan) _times[0]).End.ModifiedJulianDay;
        //double tbb1 = ((ITimeSpan) _times[1]).Start.ModifiedJulianDay;
        double tbe1 = ((ITimeSpan) _times[1]).End.ModifiedJulianDay;

        if (trb < tbb0 && tre > tbb0)
        {
          for (int k = 1; k <= nk; k++)
          {
            for (int i = 0; i < m; i++)
            {
              double sbi0 = Support.GetVal((IValueSet)_values[0], i, k);
              double sbi1 = Support.GetVal((IValueSet)_values[1], i, k); 
              xr[i][k-1] += ((tbb0 - trb)/(tre - trb)) * (sbi0 - (1 - _relaxationFactor) * ((tbb0 - trb)*(sbi1 - sbi0)/(tbe1 - tbe0)));
            }
          }
        }

        //-------------------------------------------------------------------------------------
        //              |--------|---------|--------| B
        //                                    |----------------|                  R
        //-------------------------------------------------------------------------------------

        double tbeN_1 = ((ITimeSpan) _times[_times.Count-1]).End.ModifiedJulianDay;
        double tbbN_2 = ((ITimeSpan) _times[_times.Count-2]).Start.ModifiedJulianDay;

        if (tre > tbeN_1 && trb < tbeN_1)
        {
          //double tbeN_2 = ((ITimeSpan) _times[_times.Count-2]).End.ModifiedJulianDay;
          double tbbN_1 = ((ITimeSpan) _times[_times.Count-1]).Start.ModifiedJulianDay;

          for (int k = 1; k <= nk; k++)
          {
            for (int i = 0; i < m; i++)
            {
              double sbiN_1 = Support.GetVal((IValueSet)_values[_times.Count-1], i, k);
              double sbiN_2 = Support.GetVal((IValueSet)_values[_times.Count-2], i,k);
              xr[i][k-1] += ((tre - tbeN_1)/(tre - trb)) * (sbiN_1 + (1 - _relaxationFactor) * ((tre - tbbN_1)*(sbiN_1 - sbiN_2)/(tbeN_1 - tbbN_2)));
            }
          }
        }
        //-------------------------------------------------------------------------------------
        //              |--------|---------|--------| B
        //                                              |----------------|   R
        //-------------------------------------------------------------------------------------

        if (trb >= tbeN_1)
        {
          double tbeN_2 = ((ITimeSpan) _times[_times.Count-2]).End.ModifiedJulianDay;
          //double tbbN_1 = ((ITimeSpan) _times[_times.Count-1]).Start.ModifiedJulianDay;
			
          for (int k = 1; k <= nk; k++)
          {
            for (int i = 0; i < m; i++)
            {
              double sbiN_1 = Support.GetVal((IValueSet)_values[_times.Count-1], i, k);
              double sbiN_2 = Support.GetVal((IValueSet)_values[_times.Count-2], i, k);
              xr[i][k-1] = sbiN_1 + (1 - _relaxationFactor) * ((sbiN_1 - sbiN_2)/(tbeN_1 - tbbN_2)) * (trb + tre - tbeN_1 - tbeN_2);
            }
          }
        }

        //-------------------------------------------------------------------------------------
        //                           |--------|---------|--------| B
        //        |----------------|   R
        //-------------------------------------------------------------------------------------

        if (tre <= tbb0)
        {
          for (int k = 1; k <= nk; k++)
          {
            for (int i = 0; i < m; i++)
            {
              double sbi0 = Support.GetVal((IValueSet)_values[0], i, k);
              double sbi1 = Support.GetVal((IValueSet)_values[1], i, k);
              xr[i][k-1] = sbi0 - (1 - _relaxationFactor) * ((sbi1 - sbi0)/(tbe1- tbb0))*(tbe0 + tbb0 - tre - trb);
            }
          }
        }

        //-------------------------------------------------------------------------------------
        if (_values[0] is IVectorSet)
        {
          Vector [] vectors = new Vector[m]; 

          for (int i = 0; i < m; i++)
          {
            vectors[i] = new Vector(xr[i][0],xr[i][1],xr[i][2]);
          }

          VectorSet vectorSet = new VectorSet(vectors);

          return vectorSet;
        }
        else
        {
          double[] xx = new double[m];

          for (int i = 0; i < m; i++)
          {
            xx[i] = xr[i][0];
          }
				
          ScalarSet scalarSet = new ScalarSet(xx);

          return scalarSet;
        }
      }
      catch (Exception e)
      {
        throw new Exception("MapFromTimeSpansToTimeSpan Failed",e);
      }
		}