Exemple #1
0
        public void SetValues(string QuantityID, string ElementSetID, global::OpenMI.Standard.IValueSet value)
        {
            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;
            uint       scope_size = _daisyEngine.ScopeSize();

            if (count != value.Count)
            {
                throw new ApplicationException("Wrong number of values given to '" + QuantityID + "'");
            }
            global::OpenMI.Standard.IScalarSet val = (global::OpenMI.Standard.IScalarSet)value;

            // 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];
                scope.SetNumber(QuantityID, val.GetScalar(e));
            }
        }
Exemple #2
0
        /// <summary>
        /// Calculates the potential evapotranspiration using the Hargreaves-Samani method
        /// </summary>
        /// <param name="T">Averaged daily temperature</param>
        /// <param name="Tmin">Minimum daily temperature</param>
        /// <param name="Tmax">Maximum daily temperature</param>
        /// <param name="e">element index</param>
        /// <returns>PET in mm/day</returns>
        public double CalculatePET(double T, double Tmin, double Tmax, int eid)
        {
            //calc Ra from http://www.civil.uwaterloo.ca/watflood/Manual/02_03_2.htm

            //---- calculate the relative distance between the earth and sun
            //-- get julien day
            TimeStamp ts = (TimeStamp)this.GetCurrentTime();
            DateTime  dt = CalendarConverter.ModifiedJulian2Gregorian(ts.ModifiedJulianDay);
            int       j  = dt.DayOfYear;
            double    dr = 1 + 0.033 * Math.Cos((2 * Math.PI * j) / 365);

            //---- calculate the solar declination
            double d = 0.4093 * Math.Sin((2 * Math.PI * j) / 365 - 1.405);

            //---- calculate the sunset hour angle
            //-- get latitude in degrees
            ElementSet es = (ElementSet)this.GetInputExchangeItem(0).ElementSet;
            Element    e  = es.GetElement(eid);
            double     p  = e.GetVertex(0).y *Math.PI / 180;
            //-- calc ws
            double ws = Math.Acos(-1 * Math.Tan(p) * Math.Tan(d));

            //---- calculate the total incoming extra terrestrial solar radiation (tested against http://www.engr.scu.edu/~emaurer/tools/calc_solar_cgi.pl)
            double Ra = 15.392 * dr * (ws * Math.Sin(p) * Math.Sin(d) + Math.Cos(p) * Math.Cos(d) * Math.Sin(ws));

            //---- calculate PET (From Hargreaves and Samani 1985)
            //-- calculate latent heat of vaporization (from Water Resources Engineering, David A. Chin)
            double L   = 2.501 - 0.002361 * T;
            double PET = (0.0023 * Ra * Math.Sqrt(Tmax - Tmin) * (T + 17.8)) / L;

            return(PET);
        }
Exemple #3
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);
        }
Exemple #4
0
 public void GetElement()
 {
     Assert.AreEqual(element1, elementSet.GetElement(0));
     Assert.AreEqual(element2, elementSet.GetElement(1));
 }
        public override void AddLink(ILink link)
        {
            base.AddLink(link);

            //save the link
            //

            //get the id of the target element set
            string id = link.TargetElementSet.ID;

            //get the corresponding input and output exchange item index
            int inIndex = 0; int outIndex = 0;

            for (int i = 0; i <= _inputExchangeItems.Count - 1; i++)
            {
                if (_inputExchangeItems[i].ElementSet.ID == id)
                {
                    inIndex = i; break;
                }
            }
            for (int i = 0; i <= _outputExchangeItems.Count - 1; i++)
            {
                if (_outputExchangeItems[i].ElementSet.ID == id)
                {
                    outIndex = i; break;
                }
            }

            //update input and output exchange item elementsets and quantities
            //Unit u = new Unit();

            //u.Description = link.SourceQuantity.Unit.Description;
            //u.ID = link.SourceQuantity.Unit.ID;

            Quantity q = new Quantity();

            q = (Quantity)link.SourceQuantity;
            //q.Description = link.SourceQuantity.Description;
            //q.ID = link.SourceQuantity.ID;
            //q.ValueType = link.SourceQuantity.ValueType;
            //q.Unit = link.SourceQuantity.Unit;

            ElementSet eset = new ElementSet();

            //eset.Description = link.SourceElementSet.Description;
            //eset.ID = link.SourceElementSet.ID;
            //eset.ElementType = link.SourceElementSet.ElementType;
            eset = (ElementSet)link.SourceElementSet;

            //HACK:  Right now this assumes there is only one outputexchangeitem!
            //HACK:  Also assumes that the output elementset will contain the same elements as the element set of the first link
            //check to see if the output exchange item has been defined yet
            if (_outputExchangeItems[0].ElementSet.GetElementID(0) == "Empty")
            {
                OutputExchangeItem Output = (OutputExchangeItem)_outputExchangeItems[0];
                //Quantity OutputQ = (Quantity)Output.Quantity;
                ElementSet OutputE = (ElementSet)Output.ElementSet;
                OutputE.ElementType = eset.ElementType;

                //int min = 100000;
                //int index = 0;
                //for (int o = 0; o <= _inputExchangeItems.Count - 1; o++)
                //    if (_inputExchangeItems[o].ElementSet != OutputE)
                //        if (_inputExchangeItems[o].ElementSet.ElementCount < min)
                //        {
                //            min = _inputExchangeItems[o].ElementSet.ElementCount;
                //            index = o;
                //        }


                Element e = OutputE.GetElement(0);
                e.ID = eset.GetElementID(0);
                Vertex v = new Vertex();
                e.Vertices[0].x = eset.GetXCoordinate(0, 0);
                e.Vertices[0].y = eset.GetYCoordinate(0, 0);
                //e.AddVertex(v);

                for (int o = 1; o <= eset.Elements.Length - 1; o++)
                {
                    e   = new Element(eset.GetElementID(o));
                    v   = new Vertex();
                    v.x = eset.GetXCoordinate(o, 0);
                    v.y = eset.GetYCoordinate(o, 0);
                    e.AddVertex(v);
                    OutputE.AddElement(e);
                }

                Output.ElementSet = OutputE;
                //_outputExchangeItems.Add(output);
            }

            //generate an output exchange item based on the input its getting
            OutputExchangeItem output = new OutputExchangeItem();

            output.Quantity   = q;
            output.ElementSet = eset;
            if (!_outputExchangeItems.Contains(output))
            {
                eset.Description  = "Time Matched: " + output.Quantity.ID;
                output.ElementSet = eset;
                _outputExchangeItems.Add(output);
            }

            //create a link to reflect the info we're getting from the source component
            Link l = new Link();

            l.ID = link.ID;
            l.SourceComponent  = link.SourceComponent;
            l.SourceElementSet = link.SourceElementSet;
            l.SourceQuantity   = link.SourceQuantity;
            l.TargetComponent  = link.TargetComponent;
            l.TargetElementSet = eset;
            l.TargetQuantity   = q;

            //get the earliest time (this will determine a start time based on source component, unless one is provided in the omi)
            if (this.start == -999)
            {
                if (_currentTime < l.SourceComponent.TimeHorizon.Start.ModifiedJulianDay)
                {
                    _currentTime = l.SourceComponent.TimeHorizon.Start.ModifiedJulianDay;
                }
            }

            //add updated link instead of the placeholder one
            _links.Add(link.ID, l);

            //Subscribe to events


            System.Collections.Hashtable hashtable = new Hashtable();
            if (link.SourceComponent != this)
            {
                ////get the exchange item info
                //string value = link.SourceQuantity.ID + "," + link.SourceElementSet.ID;
                //hashtable.Add("OutputExchangeItem", value);

                string value = link.TargetQuantity.ID + "," + link.TargetElementSet.ID;
                hashtable.Add("InputExchangeItem", value);

                //add the time horizon info
                hashtable.Add("TimeHorizon", link.SourceComponent.TimeHorizon);

                //add the link info
                hashtable.Add("Link", link);

                //pass this info to the IRunEngine, via Intitialize
                _engineApiAccess.Initialize(hashtable);

                //set the time horizon for the LinkableRunEngine
                Oatc.OpenMI.Sdk.Backbone.TimeSpan timehorizon = (Oatc.OpenMI.Sdk.Backbone.TimeSpan)link.SourceComponent.TimeHorizon;
                if (timehorizon.Start.ModifiedJulianDay > start)
                {
                    start = timehorizon.Start.ModifiedJulianDay;
                }
                if (timehorizon.End.ModifiedJulianDay < end)
                {
                    end = timehorizon.End.ModifiedJulianDay;
                }
            }
            else
            {
                //get the exchange item info
                string value = link.SourceQuantity.ID + "," + link.SourceElementSet.ID;
                hashtable.Add("OutputExchangeItem", value);

                //add the link info
                hashtable.Add("Link", link);

                //pass this info to the IRunEngine, via Intitialize
                _engineApiAccess.Initialize(hashtable);

                //lc = link.TargetComponent;
            }
        }