public void ExchangeItemsDefinedByConfig() { _engine = new LoadCalculator.LoadCalculatorLinkableEngine(); ArrayList componentArguments = new ArrayList(); componentArguments.Add(new Argument("ConfigFile", "./config.xml", true, "none")); _engine.Initialize((IArgument[])componentArguments.ToArray(typeof(IArgument))); int in_count = _engine.InputExchangeItemCount; int out_count = _engine.OutputExchangeItemCount; for (int i = 0; i <= in_count - 1; i++) { InputExchangeItem ie = (InputExchangeItem)_engine.GetInputExchangeItem(i); Debug.Write("Testing Input Element Count..."); Assert.IsFalse(ie.ElementSet.ElementCount > 0); Debug.WriteLine("done."); Debug.Write("Testing Input Conv2SI..."); Assert.IsTrue(ie.Quantity.Unit.ConversionFactorToSI >= 0); Debug.WriteLine("done."); Debug.Write("Testing Input Offset2SI..."); Assert.IsTrue(ie.Quantity.Unit.OffSetToSI >= 0); Debug.WriteLine("done."); } for (int i = 0; i <= out_count - 1; i++) { OutputExchangeItem oe = (OutputExchangeItem)_engine.GetOutputExchangeItem(i); Debug.Write("Testing Output Element Count..."); Assert.IsTrue(oe.ElementSet.ElementCount > 0); Debug.WriteLine("done."); Debug.Write("Testing Output Conv2SI..."); Assert.IsTrue(oe.Quantity.Unit.ConversionFactorToSI >= 0); Debug.WriteLine("done."); Debug.Write("Testing Output Offset2SI..."); Assert.IsTrue(oe.Quantity.Unit.OffSetToSI >= 0); Debug.WriteLine("done."); } Assert.IsTrue(_engine.TimeHorizon.Start.ModifiedJulianDay == CalendarConverter. Gregorian2ModifiedJulian(new DateTime(2009, 10, 27, 08, 30, 00))); _engine.Finish(); }
/// <summary> /// Reads the Configuration file, and creates OpenMI exchange items /// </summary> /// <param name="configFile">path pointing to the components comfiguration (XML) file</param> public void SetVariablesFromConfigFile(string configFile) { //Read config file XmlDocument doc = new XmlDocument(); doc.Load(configFile); XmlElement root = doc.DocumentElement; XmlNodeList outputExchangeItems = root.SelectNodes("//OutputExchangeItem"); foreach (XmlNode outputExchangeItem in outputExchangeItems) { OutputExchangeItem o = (OutputExchangeItem)CreateExchangeItemsFromXMLNode(outputExchangeItem, "OutputExchangeItem"); _outputExchangeItems.Add(o); string Key = o.ElementSet.ID + ":" + o.Quantity.ID; System.Collections.Hashtable hashtable = new Hashtable(); hashtable.Add("OutputExchangeItem", Key); //pass this info to the IRunEngine, via Intitialize _engineApiAccess.Initialize(hashtable); } XmlNodeList inputExchangeItems = root.SelectNodes("//InputExchangeItem"); foreach (XmlNode inputExchangeItem in inputExchangeItems) { InputExchangeItem i = (InputExchangeItem)CreateExchangeItemsFromXMLNode(inputExchangeItem, "InputExchangeItem"); _inputExchangeItems.Add(i); } XmlNode timeHorizon = root.SelectSingleNode("//TimeHorizon"); this.start = CalendarConverter.Gregorian2ModifiedJulian(Convert.ToDateTime(timeHorizon["StartDateTime"].InnerText)); }
/// <summary> /// This method is called in <see cref="Run">Run</see> method. /// </summary> private void RunThreadFunction() { Trigger trigger = GetTrigger(); Debug.Assert(trigger != null); Thread.Sleep(0); try { // run it !!! trigger.Run(new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(TriggerInvokeTime))); // close models down if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Closing models down..."; _runListener.OnEvent(theEvent); } foreach (UIModel uimodel in _models) { if (_runListener != null) { string ModelID = uimodel.ModelID; Event theEvent = new Event(EventType.Informative); theEvent.Description = "Calling Finish() on model " + ModelID; _runListener.OnEvent(theEvent); } uimodel.LinkableComponent.Finish(); } // thread finishes - send well known event if (_runListener != null) { _simulationFinishedEvent.Description = "Simulation finished successfuly at " + DateTime.Now.ToString() + "..."; _runListener.OnEvent(SimulationFinishedEvent); } } catch (Exception e) { if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Exception occured during simulation: " + e.ToString(); _runListener.OnEvent(theEvent); _simulationFailedEvent.Description = "Simulation FAILED at " + DateTime.Now.ToString() + "..."; _runListener.OnEvent(SimulationFailedEvent); } } finally { _running = false; _runListener = null; // release listener } }
private void Evaluate(DateTime inGregDate) { double modJulDate = CalendarConverter.Gregorian2ModifiedJulian(inGregDate); long mjdInt = (long)modJulDate; DateTime outGregDate = CalendarConverter.ModifiedJulian2Gregorian(modJulDate); Assert.AreEqual(inGregDate.ToString(), outGregDate.ToString(), modJulDate.ToString()); }
public override void Initialize(IArgument[] properties) { double start = CalendarConverter.Gregorian2ModifiedJulian(new DateTime(2004, 12, 31, 0, 0, 0)); for (int i = 0; i < 30; i++) { buffer.AddValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(start + i), new TimeStamp(start + i + 1)), new ScalarSet(new double[] { (double)i })); } }
private int _timeIncrement = 300; //in seconds (should be specifiec by the user) public LoadCalculatorLinkableEngine() { _links = new Dictionary <string, ILink>(); _inputExchangeItems = new List <IInputExchangeItem>(); _inputExchangeItemsTransformed = new List <IInputExchangeItem>(); _outputExchangeItems = new List <IOutputExchangeItem>(); start = -999; end = CalendarConverter.Gregorian2ModifiedJulian(new DateTime(2011, 01, 01, 00, 00, 00)); _earliestInputTime = end; _currentTime = -999; _timeIncrement = 20; }
/// <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; }
public ITimeSpan GetTimeHorizon() { DateTime modelStart = MohidWaterEngine.GetStartInstant(); DateTime modelEnd = MohidWaterEngine.GetStopInstant(); double start = CalendarConverter.Gregorian2ModifiedJulian(modelStart); double end = CalendarConverter.Gregorian2ModifiedJulian(modelEnd); ITimeStamp tStart = new Oatc.OpenMI.Sdk.Backbone.TimeStamp(start); ITimeStamp tEnd = new TimeStamp(end); return(new Oatc.OpenMI.Sdk.Backbone.TimeSpan(tStart, tEnd)); }
public void GetValues() { Console.Write("Begin Get Values Test..."); //create the his component DbReader his = new DbReader(); IArgument[] arguments = new IArgument[2]; arguments[0] = new Argument("DbPath", @"..\data\cuahsi-his\demo.db", true, "Database"); arguments[1] = new Argument("Relaxation", "1", true, "Time interpolation factor, btwn 0(linear inter.) and 1(nearest neigbor)"); his.Initialize(arguments); //create a trigger component Trigger trigger = new Trigger(); trigger.Initialize(null); //link the two components Link link = new Link(); link.ID = "link-1"; link.TargetElementSet = trigger.GetInputExchangeItem(0).ElementSet; link.TargetQuantity = trigger.GetInputExchangeItem(0).Quantity; link.TargetComponent = trigger; link.SourceElementSet = his.GetOutputExchangeItem(0).ElementSet; link.SourceQuantity = his.GetOutputExchangeItem(0).Quantity; link.TargetComponent = his; //run configuration his.AddLink(link); //prepare his.Prepare(); DateTime dt = Convert.ToDateTime("2009-08-20"); while (dt <= Convert.ToDateTime("2009-09-20")) { ITimeStamp time_stmp = new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(dt)); Application.DoEvents(); ScalarSet scalarset = (ScalarSet)his.GetValues(time_stmp, "link-1"); Console.WriteLine("GetValues: " + dt.ToString("s")); int i = 0; foreach (double d in scalarset.data) { Console.WriteLine(link.SourceElementSet.GetElementID(i).ToString() + " " + d.ToString()); ++i; } dt = dt.AddMinutes(5); } Console.Write("done. \n"); }
public void Initialize(IArgument[] properties) { //get the number of elements from the omi file //int _numElements = -1; //foreach (Argument property in properties) //{ // if(property.Key == "ElementCount") // _numElements = Convert.ToInt32(property.Value); //} //if (_numElements == -1) // throw new Exception("Invalid ElementCount, please fix in *.omi file"); //create output exchange item (so that this component can be linked to others) Quantity quantity = new Quantity(); Oatc.OpenMI.Sdk.Backbone.Unit unit = new Oatc.OpenMI.Sdk.Backbone.Unit(); Dimension dimension = new Dimension(); ElementSet elementset = new ElementSet(); OutputExchangeItem outputexchangeitem = new OutputExchangeItem(); quantity.ID = "Input Generator"; quantity.Description = "Supplies random numbers as input values"; quantity.ValueType = global::OpenMI.Standard.ValueType.Scalar; unit.ID = "any units"; quantity.Unit = unit; quantity.Dimension = dimension; elementset.ID = "any elementset"; elementset.Description = "AllSites"; elementset.ElementType = ElementType.IDBased; string beginDateTimeString = "1/1/1900 12:00:00 AM"; string endDateTimeString = "1/1/2500 12:00:00 AM"; DateTime beginDateTime = Convert.ToDateTime(beginDateTimeString); DateTime endDateTime = Convert.ToDateTime(endDateTimeString); _earliestInputTime = CalendarConverter.Gregorian2ModifiedJulian(beginDateTime); _latestInputTime = CalendarConverter.Gregorian2ModifiedJulian(endDateTime); outputexchangeitem.Quantity = quantity; outputexchangeitem.ElementSet = elementset; if (outputexchangeitem != null) { _outputExchangeItems.Add(outputexchangeitem); } }
public LoadCalculatorEngine(int timeIncrement, string modelID) { _inputExchangeItems = new Dictionary <string, List <string> >(); _outputExchangeItems = new Dictionary <string, List <string> >(); _values = new Dictionary <string, ScalarSet>(); _links = new Dictionary <string, Link>(); start = CalendarConverter.Gregorian2ModifiedJulian(new DateTime(1900, 01, 01, 00, 00, 00)); end = CalendarConverter.Gregorian2ModifiedJulian(new DateTime(2011, 01, 01, 00, 00, 00)); _earliestInputTime = end; _currentTime = -999; this._timeIncrement = timeIncrement; this._modelID = modelID; }
public void IdBasedElementSet_DefinedInConfig() { _engine = new LoadCalculator.LoadCalculatorLinkableEngine(); System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.Load("./config.xml"); XmlElement root = doc.DocumentElement; XmlNodeList outputExchangeItems = root.SelectNodes("//OutputExchangeItem"); foreach (XmlNode exchangeItem in outputExchangeItems) { XmlNode elementSet = exchangeItem.SelectSingleNode("ElementSet"); XmlNode numelem = doc.CreateElement("NumberOfElements"); numelem.InnerText = "10"; elementSet.AppendChild(numelem); } doc.Save("./config_temp.xml"); ArrayList componentArguments = new ArrayList(); componentArguments.Add(new Argument("ConfigFile", "./config_temp.xml", true, "none")); _engine.Initialize((IArgument[])componentArguments.ToArray(typeof(IArgument))); int out_count = _engine.OutputExchangeItemCount; for (int i = 0; i <= out_count - 1; i++) { OutputExchangeItem oe = (OutputExchangeItem)_engine.GetOutputExchangeItem(i); Debug.Write("Testing Element Count..."); Assert.IsTrue(oe.ElementSet.ElementCount == 10); Debug.WriteLine("done."); Debug.Write("Testing Element Type..."); Assert.IsTrue(oe.ElementSet.ElementType == ElementType.IDBased); Debug.WriteLine("done."); Debug.Write("Testing Element Conv2SI..."); Assert.IsTrue(oe.Quantity.Unit.ConversionFactorToSI >= 0); Debug.WriteLine("done."); Debug.Write("Testing Element Offset2SI..."); Assert.IsTrue(oe.Quantity.Unit.OffSetToSI >= 0); Debug.WriteLine("done."); } Assert.IsTrue(_engine.TimeHorizon.Start.ModifiedJulianDay == CalendarConverter. Gregorian2ModifiedJulian(new DateTime(2009, 10, 27, 08, 30, 00))); doc = null; System.IO.File.Delete("./config_temp.xml"); _engine.Finish(); }
/// <summary> /// Reads the Configuration file, and creates OpenMI exchange items /// </summary> /// <param name="configFile">path pointing to the components comfiguration (XML) file</param> public void SetVariablesFromConfigFile(string configFile) { //set output path variable _path = Path.GetDirectoryName(configFile) + "\\"; //Read config file XmlDocument doc = new XmlDocument(); doc.Load(configFile); XmlElement root = doc.DocumentElement; XmlNode ID = root.SelectSingleNode("ModelInfo//ID"); _modelID = ID.InnerText; XmlNode Desc = root.SelectSingleNode("ModelInfo//Description"); _componentDescription = Desc.InnerText; XmlNodeList outputExchangeItems = root.SelectNodes("//OutputExchangeItem"); int eid = 0; foreach (XmlNode outputExchangeItem in outputExchangeItems) { CreateExchangeItemsFromXMLNode(outputExchangeItem, "OutputExchangeItem", eid); eid++; } XmlNodeList inputExchangeItems = root.SelectNodes("//InputExchangeItem"); foreach (XmlNode inputExchangeItem in inputExchangeItems) { CreateExchangeItemsFromXMLNode(inputExchangeItem, "InputExchangeItem", eid); eid++; } XmlNode timeHorizon = root.SelectSingleNode("//TimeHorizon"); //Set IEngine properties this._simulationStartTime = CalendarConverter.Gregorian2ModifiedJulian(Convert.ToDateTime(timeHorizon["StartDateTime"].InnerText)); this._simulationEndTime = CalendarConverter.Gregorian2ModifiedJulian(Convert.ToDateTime(timeHorizon["EndDateTime"].InnerText)); this._timeStep = Convert.ToDouble(timeHorizon["TimeStepInSeconds"].InnerText); }
public void ExchangeItemsDefinedByOmi() { _engine = new LoadCalculator.LoadCalculatorLinkableEngine(); ArrayList componentArguments = new ArrayList(); componentArguments.Add(new Argument("StartDateTime", "10/27/2009 8:30:00AM", true, "none")); componentArguments.Add(new Argument("TimeStepInSeconds", "86400", true, "none")); componentArguments.Add(new Argument("InputTimeSeries", "Discharge:[cms]", true, "none")); componentArguments.Add(new Argument("InputTimeSeries", "Concentration:[mg/l]", true, "none")); componentArguments.Add(new Argument("OutputTimeSeries", "GillsCreek:Nitrogen Loading:[kg/day]:NumElements=1", true, "none")); _engine.Initialize((IArgument[])componentArguments.ToArray(typeof(IArgument))); int in_count = _engine.InputExchangeItemCount; int out_count = _engine.OutputExchangeItemCount; for (int i = 0; i <= in_count - 1; i++) { InputExchangeItem ie = (InputExchangeItem)_engine.GetInputExchangeItem(i); Debug.Write("Testing Input Element Count..."); Assert.IsFalse(ie.ElementSet.ElementCount > 0); Debug.WriteLine("done."); Debug.Write("Testing Input Conv2SI..."); Assert.IsTrue(ie.Quantity.Unit.ConversionFactorToSI >= 0); Debug.WriteLine("done."); Debug.Write("Testing Input Offset2SI..."); Assert.IsTrue(ie.Quantity.Unit.OffSetToSI >= 0); Debug.WriteLine("done."); } for (int i = 0; i <= out_count - 1; i++) { OutputExchangeItem oe = (OutputExchangeItem)_engine.GetOutputExchangeItem(i); Debug.Write("Testing Output Element Count..."); Assert.IsTrue(oe.ElementSet.ElementCount > 0); Debug.WriteLine("done."); Debug.Write("Testing Output Conv2SI..."); Assert.IsTrue(oe.Quantity.Unit.ConversionFactorToSI >= 0); Debug.WriteLine("done."); Debug.Write("Testing Output Offset2SI..."); Assert.IsTrue(oe.Quantity.Unit.OffSetToSI >= 0); Debug.WriteLine("done."); } Assert.IsTrue(_engine.TimeHorizon.Start.ModifiedJulianDay == CalendarConverter. Gregorian2ModifiedJulian(new DateTime(2009, 10, 27, 08, 30, 00))); _engine.Finish(); }
/// <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 } } }
public bool PerformTimeStep() { //request values for all input exchange items List <ScalarSet> invals = new List <ScalarSet>(); List <string> inIds = new List <string>(); Dictionary <string, ScalarSet> invalues = new Dictionary <string, ScalarSet>(); List <Link> links = new List <Link>(); bool setvals = true; foreach (KeyValuePair <string, Link> link in _links) { Link l = link.Value; //get data from all incoming links if (l.TargetComponent.ModelID == this._modelID) { string quantity = l.SourceQuantity.ID; string elementset = l.SourceElementSet.ID; ScalarSet ss = (ScalarSet)this.GetValues(l.TargetQuantity.ID, l.TargetElementSet.ID); if (invalues.ContainsKey(quantity + ":" + elementset)) { invalues[quantity + ":" + elementset] = ss; } else { invalues.Add(quantity + ":" + elementset, ss); } invals.Add(ss); links.Add(l); if (ss.data.Length == 0) { setvals = false; } } } string outstring = null; //Perform Loading Calculation if (setvals) { outstring = this._currentTime.ToString() + ","; double[] loading = new double[invals[0].data.Length]; //loop through the lesser of invals[0].data.length and invals[1].data.length int max = 0; for (int i = 0; i <= invals.Count - 1; i++) { if (invals[i].data.Length > max) { max = invals[i].data.Length; } } for (int i = 0; i <= max - 1; i++) { try { //string id1 = links[0].SourceElementSet.GetElementID(i); //int id2 = links[1].SourceElementSet.GetElementIndex(id1); loading[i] = invals[0].data[i] * invals[1].data[i] * 86400; //check to see if either input value was unknown (i.e. zero) if (loading[i] == 0) { loading[i] = -1; } //HACK: Convert from m3/s to ft3/s //loading[i] *= 0.02831685; outstring += loading[i].ToString() + ","; } catch (IndexOutOfRangeException) { } } //set output values foreach (KeyValuePair <string, Link> link in _links) { Link l = link.Value; if (l.SourceComponent.ModelID == this._modelID) { string quantity = l.SourceQuantity.ID; string elementset = l.SourceElementSet.ID; string out_quantity = l.TargetQuantity.ID; string out_elementset = l.TargetElementSet.ID; //if (_inputExchangeItems.ContainsKey(quantity)) if (invalues.ContainsKey(quantity + ":" + elementset)) { try { this.SetValues(quantity, elementset, invalues[quantity + ":" + elementset]); } catch (Exception) { } } else { this.SetValues(quantity, elementset, new ScalarSet(loading)); } } } } else { foreach (KeyValuePair <string, Link> link in _links) { Link l = link.Value; if (l.SourceComponent.ModelID == this._modelID) { string quantity = l.SourceQuantity.ID; string elementset = l.SourceElementSet.ID; this.SetValues(quantity, elementset, new ScalarSet(new double[0])); //set null } } } //save the results locally if (outstring != null) { this._results.Add(outstring); } //advance time DateTime newTime = CalendarConverter.ModifiedJulian2Gregorian(_currentTime).AddSeconds(_timeIncrement); this._currentTime = CalendarConverter.Gregorian2ModifiedJulian(newTime); return(true); }
/// <summary> /// This method is used to construct the component /// </summary> /// <param name="properties">arguments stored in the *.omi file</param> public void Initialize(IArgument[] properties) { //extract argument(s) from OMI file foreach (IArgument property in properties) { //overwrite the connection string, if one is given in the *.omi if (property.Key == "DbPath") { _dbPath = property.Value; } //default value for relationFactor is 1; if (property.Key == "Relaxation") { _smartBuffer.RelaxationFactor = Convert.ToDouble(property.Value); } if (property.Key == "IgnoreValue") { _ignore = Convert.ToDouble(property.Value); } } //---- set database to default if dbpath is invalid string fullpath = ""; //-- first check if dbpath is null bool pass = true; if (String.IsNullOrWhiteSpace(_dbPath)) { pass = false; } //-- next, check that dbpath points to an actual file else { //-- if relative path is given if (!Path.IsPathRooted(_dbPath)) { fullpath = System.IO.Path.GetFullPath(System.IO.Directory.GetCurrentDirectory() + _dbPath); } //-- if absolute path else { fullpath = System.IO.Path.GetFullPath(_dbPath); } if (!File.Exists(fullpath)) { pass = false; //-- warn the user that the database could not be found System.Windows.Forms.MessageBox.Show("The database supplied in DbWriter.omi could not be found. As a result the DbWriter will connect to the current HydroDesktop database." + "\n\n--- The following database could not be found --- \n" + fullpath, "An Error Occurred While Loading Database...", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning); } } //-- set the connection string if (!pass) { conn = Settings.Instance.DataRepositoryConnectionString; } else { //FileInfo fi = new FileInfo(fullpath); //conn = @"Data Source = " + fi.FullName + ";New=False;Compress=True;Version=3"; conn = @"Data Source = " + fullpath + ";New=False;Compress=True;Version=3"; } //---- read db info provided by omi dbargs = ReadDbArgs(properties); //---- create generic input and output exchange items InputExchangeItem inExchangeItem = new InputExchangeItem(); inExchangeItem.ElementSet = new ElementSet("any element set", "any element set", ElementType.IDBased, new Oatc.OpenMI.Sdk.Backbone.SpatialReference("1")); inExchangeItem.Quantity = new Quantity("any quantity"); _inputExchangeItems.Add(inExchangeItem); OutputExchangeItem outExchangeItem = new OutputExchangeItem(); outExchangeItem.ElementSet = new ElementSet("dummy element set", "dummy element set", ElementType.IDBased, new Oatc.OpenMI.Sdk.Backbone.SpatialReference("1")); outExchangeItem.Quantity = new Quantity("dummy quantity"); _outputExchangeItems.Add(outExchangeItem); //---- define arbitrary start and end times _earliestInputTime = CalendarConverter.Gregorian2ModifiedJulian(new DateTime(1900, 1, 1)); _latestInputTime = CalendarConverter.Gregorian2ModifiedJulian(new DateTime(2100, 12, 31)); }
public override void Initialize(IArgument[] properties) { //create a new instance of the Engine Class //_engine = new testEngine(); System.Collections.Hashtable hashtable = new Hashtable(); int option = 0; string configPath = null; for (int i = 0; i < properties.Length; i++) { if (properties[i].Key == "ConfigFile") { configPath = properties[i].Value; XmlDocument doc = new XmlDocument(); doc.Load(configPath); XmlElement root = doc.DocumentElement; XmlNode timeHorizon = root.SelectSingleNode("//TimeHorizon"); this._timeIncrement = Convert.ToInt32(timeHorizon["TimeStepInSeconds"].InnerText); doc = null; option = 1; break; } else if (properties[i].Key == "StartDateTime") { this.start = CalendarConverter.Gregorian2ModifiedJulian(Convert.ToDateTime(properties[i].Value)); this._currentTime = this.start; } else if (properties[i].Key == "TimeStepInSeconds") { this._timeIncrement = Convert.ToInt32(properties[i].Value); } else if (properties[i].Key.Contains("Input")) { hashtable.Add(i.ToString() + ":" + properties[i].Key, properties[i].Value); } else if (properties[i].Key.Contains("Output")) { hashtable.Add(i.ToString() + ":" + properties[i].Key, properties[i].Value); } } //initialize the IRunEngine class SetEngineApiAccess(); this._engineWasAssigned = true; //_engineApiAccess.Initialize(hashtable); if (!_engineWasAssigned) { throw new System.Exception("The Initialize method in the SmartWrapper cannot be invoked before the EngineApiAccess is assigned"); } _initializeWasInvoked = true; //initialize this the LinkableRunEngine class if (option == 0) { this.Initialize(hashtable); } else if (option == 1) { SetVariablesFromConfigFile(configPath); } }
private OutputExchangeItem buildExchangeItemFromFolder(string folder) { //The routine will construct an OpenMI exchange item from a folder of WaterML files. //assumptions: (1) all files within the folder are waterML files // (2) all files within folder have the same variable element // // --- MAPPING BETWEEN OPENMI AND WATERML // Qunatity <-- from the first file in the directory // ID [REQUIRED] = WaterML's variableParam element inner text // Description [optional] = WaterML's variableName element inner text // ValueType [hard coded] = Scalar // Unit // ID [optional]= WaterML's units element attribute unitsAbbreviation // ConversionFactortoSI [optional] = not in WaterML // ConverstionOffsettoSI [optional] = not in WaterML // Dimension // Power [optional] = not in WaterML // ElementSet // ID [REQUIRED] = folder name // Description [REQUIRED] = folder relative path // ElementType [hard coded] = XYPoint or IDBased // Element // ID [optional]= WaterML's SiteCode element inner text [changing to locationParam] // Vertex // X = WaterML's longitude element inner text // Y = WaterML's latitude element inner text // ... // TimeHorizon <-- union of all file-level time horizons //get list of files within the folder string[] files = Directory.GetFiles(folder); //load the first file in the directory as an XML document XmlDocument xmldoc = new XmlDocument(); // load the first xml file in the directory StreamReader sr = new StreamReader(files[0]); //deserialize XmlSerializer xml_reader = new XmlSerializer(typeof(TimeSeriesResponseType)); TimeSeriesResponseType tsr = (TimeSeriesResponseType)xml_reader.Deserialize(sr); Quantity quantity = new Quantity(); Unit unit = new Unit(); Dimension dimension = new Dimension(); ElementSet elementset = new ElementSet(); OutputExchangeItem outputexchangeitem = new OutputExchangeItem(); //Quantity ID -- REQUIRED try { quantity.ID = tsr.queryInfo.criteria.variableParam; } catch { throw new Exception("waterML document must contain a variableParam element"); } //Quantity Description -- optional try { quantity.Description = tsr.timeSeries.variable.variableName; } catch { quantity.Description = ""; } //Quantity Variable Type -- hard coded quantity.ValueType = global::OpenMI.Standard.ValueType.Scalar; //Unit ID -- optional try { unit.ID = tsr.timeSeries.variable.units.unitsAbbreviation; } catch { unit.ID = ""; } //Unit Converstion Factor to SI //TODO WaterML does not include conversion factors to SI //unit.ConversionFactorToSI = 0; //Unit Converstion Offset to SI //TODO WaterML does not include conversion offest to SI //unit.OffSetToSI = 0; quantity.Unit = unit; //Dimension Powers -- optional //TODO WaterML does not include dimension info for units //Examples below ... //dimension.SetPower(DimensionBase.Length, 3); //dimension.SetPower(DimensionBase.Time, -1); quantity.Dimension = dimension; //ElementSet ID -- folder name elementset.ID = new DirectoryInfo(folder).Name; //ElementSet Description -- folder relative path elementset.Description = folder; //ElementSet ElementType -- hard coded elementset.ElementType = ElementType.XYPoint; // ------------------------------------------------------------------- // The remaining objects require access to all files in the directory. // ------------------------------------------------------------------- foreach (string fileName in files) { //load the first file in the directory as an XML document sr = new StreamReader(fileName); tsr = (TimeSeriesResponseType)xml_reader.Deserialize(sr); Element element = new Element(); Vertex vertex = new Vertex(); //Element ID -- optional try { element.ID = tsr.queryInfo.criteria.locationParam; } catch { element.ID = ""; } //Vertex X and Y -- optional //tsr.timeSeries. TODO fix this. //if (xml_location != null && xml_location["longitude"] != null && xml_location["latitude"] != null) //{ // vertex.x = Convert.ToDouble(xml_location["longitude"].InnerText); // vertex.y = Convert.ToDouble(xml_location["latitude"].InnerText); //} //else { vertex.x = double.NaN; vertex.y = double.NaN; elementset.ElementType = ElementType.IDBased; } element.AddVertex(vertex); elementset.AddElement(element); //TimeHorizon -- REQUIRED //if (_earliestInputTime == 0.0) // { string beginDateTimeString; try { beginDateTimeString = tsr.queryInfo.criteria.timeParam.beginDateTime; } catch { throw new Exception("waterML document must contain a beginDateTime element"); } string endDateTimeString; try { endDateTimeString = tsr.queryInfo.criteria.timeParam.endDateTime; } catch { throw new Exception("waterML document must contain an endDateTime element"); } DateTime beginDateTime = Convert.ToDateTime(beginDateTimeString); DateTime endDateTime = Convert.ToDateTime(endDateTimeString); double beginDateTimeDouble = CalendarConverter.Gregorian2ModifiedJulian(beginDateTime); double endDateTimeDouble = CalendarConverter.Gregorian2ModifiedJulian(endDateTime); //update time horizon to be inclusive of this time horizon if (_earliestInputTime == 0.0) { _earliestInputTime = beginDateTimeDouble; } if (beginDateTimeDouble < _earliestInputTime) { _earliestInputTime = beginDateTimeDouble; } ; if (endDateTimeDouble > _latestInputTime) { _latestInputTime = endDateTimeDouble; } ; // } } outputexchangeitem.Quantity = quantity; outputexchangeitem.ElementSet = elementset; // add data operations and return return(addDataOperations(outputexchangeitem)); }
private SmartBuffer CreateBuffer(string elementSet) { Dictionary <DateTime, ArrayList> dict = new Dictionary <DateTime, ArrayList>(); SmartBuffer smartbuffer = new SmartBuffer(); //Move to the .dll directory try { Directory.SetCurrentDirectory(_fullPath); } catch (System.IO.IOException) { } elementSet = _dbPath + "\\" + elementSet; string[] files = Directory.GetFiles(elementSet); //read all files within element set foreach (string file in files) { //load the first file in the directory as an XML document XmlDocument xmldoc = new XmlDocument(); // load the first xml file in the directory StreamReader sr = new StreamReader(file); //deserialize XmlSerializer xml_reader = new XmlSerializer(typeof(TimeSeriesResponseType)); TimeSeriesResponseType tsr = (TimeSeriesResponseType)xml_reader.Deserialize(sr); ValueSingleVariable[] values = tsr.timeSeries.values.value; foreach (ValueSingleVariable value in values) { DateTime dt = value.dateTime; double v = Convert.ToDouble(value.Value); //check to see if time/value combination has been already added if (dict.ContainsKey(dt)) { ArrayList a = dict[dt]; a.Add(v); } //Add key to dictionary else { ArrayList a = new ArrayList(); a.Add(v); dict.Add(dt, a); } } } //put values in oder, starting with the earliest time (from http://dotnetperls.com/sort-dictionary-values) var items = from k in dict.Keys orderby dict[k] ascending select k; //load values into the smart buffer foreach (KeyValuePair <DateTime, ArrayList> kvp in dict) { ITimeStamp time_stmp = new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(kvp.Key)); double[] valueset = (double[])kvp.Value.ToArray(typeof(double)); smartbuffer.AddValues(time_stmp, new ScalarSet(valueset)); } return(smartbuffer); }
public void LinearTimeInterpolation() { Console.Write("Begin Linear Interpolation Test..."); //create the his component DbReader his = new DbReader(); IArgument[] arguments = new IArgument[1]; arguments[0] = new Argument("DbPath", @"..\data\cuahsi-his\demo.db", true, "Database"); his.Initialize(arguments); //create a trigger component Trigger trigger = new Trigger(); trigger.Initialize(null); //link the two components Link link = new Link(); link.ID = "link-1"; link.TargetElementSet = trigger.GetInputExchangeItem(0).ElementSet; link.TargetQuantity = trigger.GetInputExchangeItem(0).Quantity; link.TargetComponent = trigger; link.SourceElementSet = his.GetOutputExchangeItem(1).ElementSet; link.SourceQuantity = his.GetOutputExchangeItem(1).Quantity; link.TargetComponent = his; //Spatial interpolation IDataOperation dataOp = (his).GetOutputExchangeItem(0).GetDataOperation(7); link.AddDataOperation(dataOp); //run configuration his.AddLink(link); trigger.Validate(); his.Validate(); //prepare his.Prepare(); DateTime dt = Convert.ToDateTime("2009-08-20T21:40:00"); SmartBuffer _smartBuffer = new SmartBuffer(); //Add all values to buffer in 10min intervals Console.Write("Storing values in the smart buffer (10min resolution)... "); while (dt <= Convert.ToDateTime("2009-08-21T02:00:00")) { ITimeStamp time_stmp = new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(dt)); ScalarSet scalarset = (ScalarSet)his.GetValues(time_stmp, "link-1"); if (scalarset.Count == 0) { int f = his.GetOutputExchangeItem(1).ElementSet.ElementCount; ArrayList zeroArray = new ArrayList(); for (int i = 0; i <= f - 1; i++) { zeroArray.Add(0.0); } double[] zeros = (double[])zeroArray.ToArray(typeof(double)); scalarset = new ScalarSet(zeros); } _smartBuffer.AddValues(time_stmp, scalarset); dt = dt.AddMinutes(10); } Console.WriteLine("done.\n\n"); //request values from the smart buffer at 5min intervals dt = Convert.ToDateTime("2009-08-20T21:40:00"); while (dt <= Convert.ToDateTime("2009-08-21T02:00:00")) { ITimeStamp time_stmp = new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(dt)); //Get values at requested time ScalarSet scalarset = (ScalarSet)_smartBuffer.GetValues(time_stmp); Console.WriteLine("GetValues: " + dt.ToString("s")); //loop through interpolated values int i = 0; foreach (double d in scalarset.data) { Console.WriteLine(link.SourceElementSet.GetElementID(i).ToString() + " " + d.ToString()); ++i; } dt = dt.AddMinutes(5); } Console.Write("done. \n"); }
public void DataConversion() { Console.Write("Begin Data Conversion Test..."); //create the his component DbReader his = new DbReader(); IArgument[] arguments = new IArgument[2]; arguments[0] = new Argument("DbPath", @"../../../../../Databases/MyDataRepository.sqlite", true, "Database"); arguments[1] = new Argument("Relaxation", "1", true, "Time interpolation factor, btwn 0(linear inter.) and 1(nearest neigbor)"); his.Initialize(arguments); //create a trigger component Trigger trigger = new Trigger(); trigger.Initialize(null); // //-- Test Transfer from ft3/s to m3/s // int item = 0; for (int i = 0; i <= his.OutputExchangeItemCount - 1; i++) { string quantity = his.GetOutputExchangeItem(i).Quantity.ID; string elementset = his.GetOutputExchangeItem(i).ElementSet.ID; if (quantity == "Discharge" && elementset == "Gills Creek Discharge") { item = i; break; } } //link the two components Link link = new Link(); link.ID = "link-1"; link.TargetElementSet = trigger.GetInputExchangeItem(0).ElementSet; link.TargetQuantity = trigger.GetInputExchangeItem(0).Quantity; link.TargetComponent = trigger; link.SourceElementSet = his.GetOutputExchangeItem(item).ElementSet; link.SourceQuantity = his.GetOutputExchangeItem(item).Quantity; link.TargetComponent = his; his.AddLink(link); his.Prepare(); DateTime dt = Convert.ToDateTime("2009-09-07 00:00:00"); while (dt <= Convert.ToDateTime("2010-09-06 00:00:00")) { ITimeStamp time_stmp = new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(dt)); Application.DoEvents(); ScalarSet scalarset = (ScalarSet)his.GetValues(time_stmp, "link-1"); Console.WriteLine("GetValues: " + dt.ToString("s")); int i = 0; foreach (double d in scalarset.data) { Console.WriteLine(link.SourceElementSet.GetElementID(i).ToString() + " " + d.ToString()); ++i; } dt = dt.AddDays(1); } Console.Write("done. \n"); }
/// <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(Logger logger, bool runInSameThread) { LoggerListener loggerListener = new LoggerListener(logger); ProgressListener progressListener = new ProgressListener(); progressListener.ModelProgressChangedHandler += new ModelProgressChangedDelegate(delegate(ILinkableComponent linkableComponent, ITimeStamp simTime) { string guid = cmGuidMapping[linkableComponent]; string progress = CalendarConverter.ModifiedJulian2Gregorian(simTime.ModifiedJulianDay).ToString(); CompositionModelProgressChangedHandler(this, guid, progress); }); ArrayList listeners = new ArrayList(); listeners.Add(loggerListener); listeners.Add(progressListener); ProxyListener proxyListener = new ProxyListener(); proxyListener.Initialize(listeners); startTime = DateTime.Now; if (_running) { throw (new Exception("Simulation is already running.")); } _running = true; _runListener = proxyListener; 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 (Model 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); } // 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 (Model 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(RunThreadFunction) { Priority = ThreadPriority.Normal }; // 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 endTime = DateTime.Now; var ev = new Event { Description = "Elapsed time: " + (endTime - startTime), Type = EventType.GlobalProgress }; _runListener.OnEvent(ev); } } finally { } }
public global::OpenMI.Standard.ITimeStamp GetEarliestNeededTime() { return(new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(MohidWaterEngine.GetCurrentTime().AddSeconds(MohidWaterEngine.GetCurrentTimeStep())))); }
public global::OpenMI.Standard.ITime GetCurrentTime() { return(new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(MohidWaterEngine.GetCurrentTime()))); }
public IValueSet GetValues(ITime time, string linkID) { // ---------------------------------------------------------------------------------------------------- // The method queries a HydroDesktop database and builds an IValueSet object for the given a time // and link. // ---------------------------------------------------------------------------------------------------- Link l = (Link)_links[linkID]; IValueSet values; if (_exactMatch) { // // Perform Exact Match Routine // // covert ITime to a DateTime data type TimeStamp timestamp = (TimeStamp)time; DateTime dt = CalendarConverter.ModifiedJulian2Gregorian( (double)timestamp.ModifiedJulianDay); //check to see if the buffer contains this time if (_times[linkID].Contains(timestamp.ModifiedJulianDay)) { values = new ScalarSet(new double[_elementCount[linkID]]); values = _buffer[linkID].GetValues(time); //convert the values to the correct units values = ConvertUnit(values, l); } else { values = new ScalarSet(new double[0]); } } else if (_range != -999) { // // Perform Range Searching Routine // List <ScalarSet> FoundVals = new List <ScalarSet>(); // covert ITime to a DateTime data type TimeStamp timestamp = (TimeStamp)time; DateTime dt = CalendarConverter.ModifiedJulian2Gregorian( (double)timestamp.ModifiedJulianDay); values = new ScalarSet(new double[_elementCount[linkID]]); //check to see if the current time is within the known time span if (timestamp.ModifiedJulianDay <= _endTimes[linkID].ModifiedJulianDay) { TimeStamp currTime = (TimeStamp)time; DateTime dateTime = CalendarConverter.ModifiedJulian2Gregorian(currTime.ModifiedJulianDay); //dateTime = dateTime.AddSeconds(_range); //get the earliest and latest times based on the specified time range TimeStamp latestTime = new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(dateTime.AddSeconds(_range / 2))); TimeStamp earliestTime = new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(dateTime.AddSeconds(-1 * _range / 2))); //check to make sure tht the latest time is less than the last time in the buffer if (latestTime.ModifiedJulianDay > ((TimeStamp)_buffer[linkID].GetTimeAt(_buffer[linkID].TimesCount - 1)).ModifiedJulianDay) { values = new ScalarSet(new double[0]); } //check to make sure tht the earliest time is greater than the first time in the buffer else if (earliestTime.ModifiedJulianDay < ((TimeStamp)_buffer[linkID].GetTimeAt(0)).ModifiedJulianDay) { values = new ScalarSet(new double[0]); } else { for (int i = _lastIndex[linkID]; i <= _buffer[linkID].TimesCount - 1; i++) { ////loop through the times to find the start and end indices if (((TimeStamp)_buffer[linkID].GetTimeAt(i)).ModifiedJulianDay >= earliestTime.ModifiedJulianDay && ((TimeStamp)_buffer[linkID].GetTimeAt(i)).ModifiedJulianDay <= latestTime.ModifiedJulianDay) { ScalarSet ss = (ScalarSet)_buffer[linkID].GetValues(_buffer[linkID].GetTimeAt(i)); if (ss.data.Length > 0) { FoundVals.Add(ss); } } //if the latest time has been passed then break else if (((TimeStamp)_buffer[linkID].GetTimeAt(i)).ModifiedJulianDay > latestTime.ModifiedJulianDay) { //save the index that should be used to start with on the next iteration _lastIndex[linkID] = Convert.ToInt32(Math.Floor(Convert.ToDouble(i) - Convert.ToDouble(_lastIndex[linkID]) / 2.0)); break; } } if (FoundVals.Count == 1) { values = new ScalarSet(FoundVals[0].data); //convert the values to the correct units values = ConvertUnit(values, l); } else if (FoundVals.Count > 1) { double[] ave = FoundVals[0].data; int[] divider = new int[ave.Length]; for (int i = 0; i <= ave.Length - 1; i++) { if (ave[i] != 0) { divider[i]++; } } //HACK: I'm taking the average of all the values. Probably should do this smarter. for (int i = 0; i <= FoundVals[0].data.Length - 1; i++) { for (int j = 1; j <= FoundVals.Count - 1; j++) { if (FoundVals[j].data[i] != 0) { ave[i] += FoundVals[j].data[i]; divider[i]++; } } } for (int i = 0; i <= ave.Length - 1; i++) { if (divider[i] != 0) { ave[i] = ave[i] / (divider[i]); } } values = new ScalarSet(ave); //convert the values to the correct units values = ConvertUnit(values, l); } else { values = new ScalarSet(new double[0]); } } } else { values = new ScalarSet(new double[0]); } } else { // //Perform Relaxation Interpolation Routine // // covert ITime to a DateTime data type TimeStamp timestamp = (TimeStamp)time; DateTime dt = CalendarConverter.ModifiedJulian2Gregorian( (double)timestamp.ModifiedJulianDay); values = new ScalarSet(new double[_elementCount[linkID]]); //check to see if the current time is within the known time span if (timestamp.ModifiedJulianDay <= _endTimes[linkID].ModifiedJulianDay) { //get scalar set //IValueSet values = _smartBuffer.GetValues(time); values = _buffer[linkID].GetValues(time); //convert the values to the correct units values = ConvertUnit(values, l); } } #region Map Values if (mapper.Count > 0) { if (mapper.ContainsKey(linkID)) { IValueSet mappedValues = mapper[linkID].MapValues(values); // return values return(mappedValues); } } #endregion //else return unmapped values (i.e. index based) return(values); }
public global::OpenMI.Standard.ITime GetInputTime(string QuantityID, string ElementSetID) { return(new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(MohidWaterEngine.GetCurrentTime().AddSeconds(MohidWaterEngine.GetCurrentTimeStep())))); }
private void AddToBuffer(ILink link) { // ---------------------------------------------------------------------------------------------------- // The method adds values associated with the link.source to a SmartBuffer. // ---------------------------------------------------------------------------------------------------- //dictionary of values indexed by their date\time SortedDictionary <DateTime, ArrayList> dict = new SortedDictionary <DateTime, ArrayList>(); //create a buffer instance to temporarily store data SmartBuffer _smartBuffer = new SmartBuffer(); //set the relaxation factor, if specifed in *.omi file. if (_relaxationFactor > 0) { _smartBuffer.RelaxationFactor = _relaxationFactor; } //get link.source quantity and elementset IQuantity sourceQuantity = link.SourceQuantity; IElementSet sourceElementSet = link.SourceElementSet; string sql = "SELECT DISTINCT ds.SeriesID " + "FROM DataValues dv " + "INNER JOIN DataSeries ds ON dv.SeriesID = ds.SeriesID " + "INNER JOIN DataThemes dt ON dv.SeriesID = dt.SeriesID " + "INNER JOIN Sites s ON ds.SiteID = s.SiteID " + "INNER JOIN DataThemeDescriptions dtd On dt.ThemeID = dtd.ThemeID " + "WHERE dtd.ThemeName = '" + sourceElementSet.ID.ToString() + "' " + "ORDER BY s.SiteName ASC"; DataTable tbl = _db.LoadTable("values", sql); //get the number of series' in this theme Dictionary <string, int> sites = new Dictionary <string, int>(); //get the number of sites in this series int k = 0; foreach (DataRow row in tbl.Rows) { if (!sites.ContainsKey(Convert.ToString(row["SeriesID"]))) { sites.Add(Convert.ToString(row["SeriesID"]), k); k++; } } //query the db for values associated with source quantity and elementset //TODO: LOOKUP BY THEMENAME, NOT THEMEID sql = "SELECT ds.SeriesID, dv.LocalDateTime, dv.DataValue " + "FROM DataValues dv " + "INNER JOIN DataSeries ds ON dv.SeriesID = ds.SeriesID " + "INNER JOIN DataThemes dt ON dv.SeriesID = dt.SeriesID " + "INNER JOIN DataThemeDescriptions dtd On dt.ThemeID = dtd.ThemeID " + "WHERE dtd.ThemeName = '" + sourceElementSet.ID.ToString() + "' " + "ORDER BY dv.LocalDateTime ASC"; //"ORDER BY dv.DataValue ASC"; tbl = _db.LoadTable("values", sql); //get the number of series' in this theme List <DateTime> t = new List <DateTime>(); Dictionary <DateTime, double[]> Times = new Dictionary <DateTime, double[]>(); //get the number of sites in this series //int k = 0; //foreach (DataRow row in tbl.Rows) //{ // if (!sites.ContainsKey(Convert.ToString(row["SeriesID"]))) // { // sites.Add(Convert.ToString(row["SeriesID"]), k); // k++; // } // if(!t.Contains(Convert.ToDateTime(row["LocalDateTime"]))) // t.Add(Convert.ToDateTime(row["LocalDateTime"])); //} //initialize a dictionary to hold the times and values foreach (DataRow row in tbl.Rows) { if (!Times.ContainsKey(Convert.ToDateTime(row["LocalDateTime"]))) { Times.Add(Convert.ToDateTime(row["LocalDateTime"]), new double[sites.Count]); } } //Times.OrderBy<pair, Times.OrderBy(pair => pair.Value); foreach (DataRow row in tbl.Rows) { double v = Convert.ToDouble(row["DataValue"]); string id = Convert.ToString(row["SeriesID"]); DateTime dt = Convert.ToDateTime(row["LocalDateTime"]); Times[dt][sites[id]] = v; } for (int i = 0; i <= t.Count - 1; i++) { double[] vals = new double[sites.Count]; DateTime dt = t[i]; foreach (DataRow row in tbl.Rows) { double v = Convert.ToDouble(row["DataValue"]); string id = Convert.ToString(row["SeriesID"]); //add v to vals in the location defined by its site id vals[sites[id]] = v; } ArrayList a = new ArrayList(); a.Add(vals); dict.Add(t[i], a); } ////check to see if time/value combination has been already added //if (dict.ContainsKey(dt)) //{ // //if yes, add value to existing dictionary // ArrayList a = dict[dt]; // a.Add(v); //} //else //{ // //if not, add value to new dictionary // ArrayList a = new ArrayList(); // a.Add(v); // dict.Add(dt, a); //} //double[] valueset = null; //ITimeStamp time_stmp = null; ////add dictionary to the smart buffer //foreach (KeyValuePair<DateTime, ArrayList> kvp in dict) //{ // time_stmp = new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(kvp.Key)); // valueset = (double[])kvp.Value.ToArray(typeof(double)); // _smartBuffer.AddValues(time_stmp, new ScalarSet(valueset)); //} // //sort the dictionary //var sortDict = from keys in Times.Keys // orderby Times[keys] ascending // select keys; ////Times = (Dictionary<DateTime, double[]>)sortDict; //foreach (KeyValuePair<DateTime, double[]> kvp in Times.OrderBy(key => key.Value)) //{ // time_stmp = new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(kvp.Key)); // valueset = kvp.Value; // _smartBuffer.AddValues(time_stmp, new ScalarSet(valueset)); //} //add dictionary to the smart buffer double[] valueset = null; ITimeStamp time_stmp = null; foreach (KeyValuePair <DateTime, double[]> kvp in Times) { time_stmp = new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(kvp.Key)); valueset = kvp.Value; _smartBuffer.AddValues(time_stmp, new ScalarSet(valueset)); } //if ExactMatch is requested, then save the times for using in the GetValues method try { if (_exactMatch) { List <double> times = new List <double>(); foreach (KeyValuePair <DateTime, double[]> kvp in Times) { time_stmp = new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(kvp.Key)); times.Add(time_stmp.ModifiedJulianDay); } _times.Add(link.ID, times); } } catch (Exception) { } ////if ExactMatch is requested, then save the times for using in the GetValues method //try //{ // if (_exactMatch) // { // List<double> times = new List<double>(); // foreach (KeyValuePair<DateTime, ArrayList> kvp in dict) // { // time_stmp = new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(kvp.Key)); // times.Add(time_stmp.ModifiedJulianDay); // } // _times.Add(link.ID, times); // } //} //catch (Exception) { } //store the number of elements for this link _elementCount.Add(link.ID, valueset.Length); //store the lastest known time for this link _endTimes.Add(link.ID, (TimeStamp)time_stmp); //store the smart buffer based on linkID _buffer.Add(link.ID, _smartBuffer); //initialize the last index variable _lastIndex.Add(link.ID, 0); //adjust start time based on target component if (link.TargetComponent.TimeHorizon.Start.ModifiedJulianDay > this.EarliestInputTime.ModifiedJulianDay) { this._earliestInputTime = link.TargetComponent.TimeHorizon.Start.ModifiedJulianDay; } #region Initialize Element Mapper try { //get the first (stored) data operation IDataOperation dataOp = link.GetDataOperation(0); //get dataOperation description string dataOpDesc = dataOp.GetArgument(1).Value; //add a element mapper instance to the mapper dictionary mapper.Add(link.ID, new ElementMapper()); //initialize the element mapper and create a mapping matrix mapper[link.ID].Initialise(dataOpDesc, link.SourceElementSet, link.TargetElementSet); } catch (Exception e) { } #endregion }
/// <summary> /// This method is called in <see cref="Run">Run</see> method. /// </summary> private void RunThreadFunction() { bool succeed = false; foreach (Model 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(); } //Trigger trigger = GetTrigger(); //Debug.Assert(trigger != null); //WHAT THE HELL IS THIS FOR? Thread.Sleep(0); try { // run it !!! // trigger.Run(new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(TriggerInvokeTime))); ITime triggerTime = new TimeStamp(CalendarConverter.Gregorian2ModifiedJulian(TriggerInvokeTime)); CompositionRunner runner = new CompositionRunner(_models, _runListener); if (Parallelized) { runner.RunParallel(triggerTime, ParallelizeMode); } else { runner.RunSequence(triggerTime); } // close models down if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Closing models down..."; _runListener.OnEvent(theEvent); } foreach (Model uimodel in _models) { if (_runListener != null) { string ModelID = uimodel.ModelID; Event theEvent = new Event(EventType.Informative); theEvent.Description = "Calling Finish() on model " + ModelID; _runListener.OnEvent(theEvent); } uimodel.LinkableComponent.Finish(); } // thread finishes - send well known event if (_runListener != null) { _simulationFinishedEvent.Description = "Simulation finished successfuly at " + DateTime.Now.ToString() + "..."; _runListener.OnEvent(SimulationFinishedEvent); } succeed = true; } catch (Exception e) { if (_runListener != null) { Event theEvent = new Event(EventType.Informative); theEvent.Description = "Exception occured during simulation: " + e.ToString(); _runListener.OnEvent(theEvent); _simulationFailedEvent.Description = "Simulation FAILED at " + DateTime.Now.ToString() + "..."; _runListener.OnEvent(SimulationFailedEvent); } } finally { endTime = DateTime.Now; var ev = new Event { Description = "Elapsed time: " + (endTime - startTime), Type = EventType.GlobalProgress }; _runListener.OnEvent(ev); _running = false; _runListener = null; // release listener if (AfterSimulationHandler != null) { AfterSimulationHandler(this, succeed); } } }
private OutputExchangeItem buildExchangeItemFromTheme(string themeID, string themeName) { // ---------------------------------------------------------------------------------------------------- // The function queries a HydroDesktop database and builds OpenMI exchange item based on theme id // and name. // ---------------------------------------------------------------------------------------------------- // Mapping from db to OpenMI // // db OpenMI // --- ------- // Variables.VariableCode quantity.ID // Variables.VariableName quantity.Description // [Assume OpenMI.Standard.ValueType.Scalar] quantity.ValueType // Units.UnitAbbreviation unit.ID // [Not Impemented] unit.ConversionFactorToSI // [Not Implemented] unit.OffsetToSI // [Not Implemented] unit.dimension // DataThemeDiscriptions.ThemeName elementset.ID // DataThemeDiscriptions.ThemeDescription elementset.Description // [Assume OpenMI.Standard.ElementType.XYPoint] elementset.ElementType // DataSeries.SeriesID element.ID // DataSeries.BeginDateTime time horizon start // DataSeries.EndDateTime time horizon end Quantity quantity = new Quantity(); Unit unit = new Unit(); Dimension dimension = new Dimension(); ElementSet elementset = new ElementSet(); OutputExchangeItem outputexchangeitem = new OutputExchangeItem(); DataTable dtSeries = null; try { //query db to gather required information based on theme id (including dimension, conversion, offset) string sql = "SELECT ds.SeriesID, v.VariableName, v.VariableCode, u.UnitsAbbreviation, td.ThemeName, " + "td.ThemeDescription, ds.BeginDateTime, ds.EndDateTime, ds.SiteID, uc.ConversionFactor " + "FROM DataThemeDescriptions td " + "INNER JOIN DataThemes t ON td.ThemeID = t.ThemeID " + "INNER JOIN DataSeries ds ON t.SeriesID = ds.SeriesID " + "INNER JOIN Variables v ON ds.VariableID = v.VariableID " + "INNER JOIN Sites s ON ds.SiteID = s.SiteID " + "INNER JOIN Units u ON v.VariableUnitsID = u.UnitsID " + "INNER JOIN UnitConversions uc ON u.UnitsID = uc.ConversionID " + "WHERE t.themeID = '" + themeID.ToString() + "' " + "ORDER BY s.SiteName ASC"; dtSeries = _db.LoadTable("series", sql); } catch (Exception) { //query db to gather required information based on theme id (omitting dimension, conversion, offset) string sql = "SELECT ds.SeriesID, v.VariableName, v.VariableCode, u.UnitsAbbreviation, td.ThemeName, " + "td.ThemeDescription, ds.BeginDateTime, ds.EndDateTime, ds.SiteID " + "FROM DataThemeDescriptions td " + "INNER JOIN DataThemes t ON td.ThemeID = t.ThemeID " + "INNER JOIN DataSeries ds ON t.SeriesID = ds.SeriesID " + "INNER JOIN Variables v ON ds.VariableID = v.VariableID " + "INNER JOIN Sites s ON ds.SiteID = s.SiteID " + "INNER JOIN Units u ON v.VariableUnitsID = u.UnitsID " + "WHERE t.themeID = '" + themeID.ToString() + "' " + "ORDER BY s.SiteName ASC"; dtSeries = _db.LoadTable("series", sql); } if (dtSeries.Rows.Count == 0) { //query db to gather required information based on theme id (omitting dimension, conversion, offset) string sql = "SELECT ds.SeriesID, v.VariableName, v.VariableCode, u.UnitsAbbreviation, td.ThemeName, " + "td.ThemeDescription, ds.BeginDateTime, ds.EndDateTime, ds.SiteID " + "FROM DataThemeDescriptions td " + "INNER JOIN DataThemes t ON td.ThemeID = t.ThemeID " + "INNER JOIN DataSeries ds ON t.SeriesID = ds.SeriesID " + "INNER JOIN Variables v ON ds.VariableID = v.VariableID " + "INNER JOIN Sites s ON ds.SiteID = s.SiteID " + "INNER JOIN Units u ON v.VariableUnitsID = u.UnitsID " + "WHERE t.themeID = '" + themeID.ToString() + "' " + "ORDER BY s.SiteName ASC"; dtSeries = _db.LoadTable("series", sql); } foreach (DataRow row in dtSeries.Rows) { string seriesID = Convert.ToString(row["SeriesID"]); string variableName = Convert.ToString(row["VariableName"]); string variableCode = Convert.ToString(row["VariableCode"]); string unitsAbbreviation = Convert.ToString(row["UnitsAbbreviation"]); //string themeName = Convert.ToString(row["ThemeName"]); PASSED IN AS ARGUMENT string themeDescription = Convert.ToString(row["ThemeDescription"]); DateTime beginDateTime = Convert.ToDateTime(row["BeginDateTime"]); DateTime endDateTime = Convert.ToDateTime(row["EndDateTime"]); string siteID = Convert.ToString(row["SiteID"]); //try to get the Dimension, Conversion, and Offset string Dimension = null; double Conversion2Si = 1; double Offset2SI = 0; try { Conversion2Si = Convert.ToDouble(row["ConversionFactor"]); //Broken: This needs to be implemented in the DB Dimension = Convert.ToString(row["Dimension"]); //Broken: This needs to be implemented in the DB Offset2SI = Convert.ToDouble(row["OffsetToSI"]); } catch (Exception) { } if (!String.IsNullOrEmpty(Dimension)) { string[] Dimensions = Regex.Split(Dimension.ToUpper(), @"([A-Z])([^A-Z]+)"); //for (int i = 0; i <= Dimension.Length - 2; i++) //{ for (int j = 0; j <= Dimensions.Length - 2; j++) { char dim; int pow; if (Dimensions[j].Length == 0) { dim = 'M'; pow = 0; } else { dim = Dimensions[j][0]; if (Char.IsLetter(Dimensions[j + 1][0])) { pow = 1; } else if (Dimensions[j + 1].Length == 0) { pow = 1; j++; } else { pow = Convert.ToInt32(Dimensions[j + 1]); j++; } } //int pow = Convert.ToInt32(Regex.Split(Dimensions[i], @"/(^[A-Z]+)/")); if (dim == 'M') { dimension.SetPower(DimensionBase.Mass, pow); } else if (dim == 'L') { dimension.SetPower(DimensionBase.Length, pow); } else if (dim == 'D') { dimension.SetPower(DimensionBase.Temperature, pow); } else if (dim == 'T') { dimension.SetPower(DimensionBase.Time, pow); } else if (dim == 'E') { dimension.SetPower(DimensionBase.ElectricCurrent, pow); } else if (dim == 'C') { dimension.SetPower(DimensionBase.Currency, pow); } else { throw new Exception(dim + " is an Invalid Unit Dimension!"); } } //} //string Dimensions[] = Dimension.Split('/A-Z/'); } //will be updated for each row, but each row should be the same so it is OK. //TODO: include check to make sure these values are the same for each row. quantity.ID = variableName; quantity.Description = variableCode; quantity.ValueType = global::OpenMI.Standard.ValueType.Scalar; unit.ID = unitsAbbreviation; unit.ConversionFactorToSI = Conversion2Si; unit.OffSetToSI = Offset2SI; quantity.Unit = unit; //TODO: ignoring unit dimensions for now. //Examples below ... //dimension.SetPower(DimensionBase.Length, 3); //dimension.SetPower(DimensionBase.Time, -1); quantity.Dimension = dimension; elementset.ID = themeName; elementset.Description = themeDescription; elementset.ElementType = ElementType.XYPoint; Element element = new Element(); element.ID = seriesID; string get_lat_lon = "SELECT s.SiteID, s.Latitude, s.Longitude " + "FROM Sites s " + "INNER JOIN DataSeries ds ON ds.SiteID = s.SiteID " + "WHERE ds.SiteID = " + siteID; DataTable t = _db.LoadTable("values", get_lat_lon); Vertex vertex = new Vertex(); //TODOD: Add x, y for vertex vertex.x = Convert.ToDouble(t.Rows[0]["Longitude"]); vertex.y = Convert.ToDouble(t.Rows[0]["Latitude"]); element.AddVertex(vertex); elementset.AddElement(element); //update time horizon to be inclusive of this time horizon double beginDateTimeDouble = CalendarConverter.Gregorian2ModifiedJulian(beginDateTime); double endDateTimeDouble = CalendarConverter.Gregorian2ModifiedJulian(endDateTime); if (_earliestInputTime == 0.0) { _earliestInputTime = beginDateTimeDouble; } if (beginDateTimeDouble < _earliestInputTime) { _earliestInputTime = beginDateTimeDouble; } ; if (endDateTimeDouble > _latestInputTime) { _latestInputTime = endDateTimeDouble; } ; } outputexchangeitem.Quantity = quantity; outputexchangeitem.ElementSet = elementset; // add data operations and return return(addDataOperations(outputexchangeitem)); }