public void ElmtGridWrapperTest() { ICoordinate[,] coordinates = new ICoordinate[4, 3]; coordinates[0, 0] = new Coordinate(0, 0); coordinates[1, 0] = new Coordinate(1, 0); coordinates[2, 0] = new Coordinate(2, 0); coordinates[3, 0] = new Coordinate(3, 0); coordinates[0, 1] = new Coordinate(0, 1); coordinates[1, 1] = new Coordinate(1.2, 1.1); coordinates[2, 1] = new Coordinate(2.4, 1.2); coordinates[3, 1] = new Coordinate(3.8, 1.3); coordinates[0, 2] = new Coordinate(0, 2); coordinates[1, 2] = new Coordinate(1.3, 2.1); coordinates[2, 2] = new Coordinate(2.9, 2.2); coordinates[3, 2] = new Coordinate(4.5, 2.3); Spatial2DCurveLinearGrid grid = new Spatial2DCurveLinearGrid() { Coordinates = coordinates, IsNodeBased = false, }; Spatial2DGridWrapper wrapper = new Spatial2DGridWrapper(grid); Assert.AreEqual(ElementType.Polygon, wrapper.ElementType); Assert.AreEqual(6, wrapper.ElementCount); Assert.AreEqual(4, wrapper.GetVertexCount(0)); Assert.AreEqual(4, wrapper.GetVertexCount(3)); Assert.AreEqual(4, wrapper.GetVertexCount(8)); Assert.AreEqual(4, wrapper.GetVertexCount(11)); Assert.AreEqual(0, wrapper.GetVertexXCoordinate(0, 0)); Assert.AreEqual(0, wrapper.GetVertexYCoordinate(0, 0)); Assert.AreEqual(1, wrapper.GetVertexXCoordinate(0, 1)); Assert.AreEqual(0, wrapper.GetVertexYCoordinate(0, 1)); Assert.AreEqual(1.2, wrapper.GetVertexXCoordinate(0, 2)); Assert.AreEqual(1.1, wrapper.GetVertexYCoordinate(0, 2)); Assert.AreEqual(0, wrapper.GetVertexXCoordinate(0, 3)); Assert.AreEqual(1, wrapper.GetVertexYCoordinate(0, 3)); Assert.AreEqual(2.4, wrapper.GetVertexXCoordinate(5, 0)); Assert.AreEqual(1.2, wrapper.GetVertexYCoordinate(5, 0)); Assert.AreEqual(3.8, wrapper.GetVertexXCoordinate(5, 1)); Assert.AreEqual(1.3, wrapper.GetVertexYCoordinate(5, 1)); Assert.AreEqual(4.5, wrapper.GetVertexXCoordinate(5, 2)); Assert.AreEqual(2.3, wrapper.GetVertexYCoordinate(5, 2)); Assert.AreEqual(2.9, wrapper.GetVertexXCoordinate(5, 3)); Assert.AreEqual(2.2, wrapper.GetVertexYCoordinate(5, 3)); }
public void ElmtGridWrapperTest() { Spatial2DRegularGrid grid = new Spatial2DRegularGrid { X0 = 10, Y0 = 20, Dx = 2, Dy = 4, XCount = 4, YCount = 3, Orientation = 30, IsNodeBased = false, }; Spatial2DGridWrapper gridWrapper = new Spatial2DGridWrapper(grid); double rotxx = Math.Cos(30.0 / 180.0 * Math.PI); double rotxy = Math.Sin(30.0 / 180.0 * Math.PI); double rotyx = -Math.Sin(30.0 / 180.0 * Math.PI); double rotyy = Math.Cos(30.0 / 180.0 * Math.PI); Assert.AreEqual(ElementType.Polygon, gridWrapper.ElementType); Assert.AreEqual(12, gridWrapper.ElementCount); Assert.AreEqual(4, gridWrapper.GetVertexCount(0)); Assert.AreEqual(4, gridWrapper.GetVertexCount(3)); Assert.AreEqual(4, gridWrapper.GetVertexCount(8)); Assert.AreEqual(4, gridWrapper.GetVertexCount(11)); // Check first element Assert.AreEqual(10, gridWrapper.GetVertexXCoordinate(0, 0)); Assert.AreEqual(20, gridWrapper.GetVertexYCoordinate(0, 0)); Assert.AreEqual(10 + rotxx * 2, gridWrapper.GetVertexXCoordinate(0, 1)); Assert.AreEqual(20 + rotyx * 2, gridWrapper.GetVertexYCoordinate(0, 1)); Assert.AreEqual(10 + rotxx * 2 + rotxy * 4, gridWrapper.GetVertexXCoordinate(0, 2)); Assert.AreEqual(20 + rotyx * 2 + rotyy * 4, gridWrapper.GetVertexYCoordinate(0, 2)); Assert.AreEqual(10 + rotxy * 4, gridWrapper.GetVertexXCoordinate(0, 3)); Assert.AreEqual(20 + rotyy * 4, gridWrapper.GetVertexYCoordinate(0, 3)); // Check last element Assert.AreEqual(10 + rotxx * 6 + rotxy * 8, gridWrapper.GetVertexXCoordinate(11, 0)); // 19,19 Assert.AreEqual(20 + rotyx * 6 + rotyy * 8, gridWrapper.GetVertexYCoordinate(11, 0)); // 23.92 Assert.AreEqual(10 + rotxx * 8 + rotxy * 8, gridWrapper.GetVertexXCoordinate(11, 1)); // 20.92 Assert.AreEqual(20 + rotyx * 8 + rotyy * 8, gridWrapper.GetVertexYCoordinate(11, 1)); // 22.92 Assert.AreEqual(10 + rotxx * 8 + rotxy * 12, gridWrapper.GetVertexXCoordinate(11, 2)); // 22.92 Assert.AreEqual(20 + rotyx * 8 + rotyy * 12, gridWrapper.GetVertexYCoordinate(11, 2)); // 26.39 Assert.AreEqual(10 + rotxx * 6 + rotxy * 12, gridWrapper.GetVertexXCoordinate(11, 3)); // 21.19 Assert.AreEqual(20 + rotyx * 6 + rotyy * 12, gridWrapper.GetVertexYCoordinate(11, 3)); // 27.39 }
public void NodeGridWrapperTest() { Spatial2DRegularGrid grid = new Spatial2DRegularGrid { X0 = 10, Y0 = 20, Dx = 2, Dy = 4, XCount = 4, YCount = 3, Orientation = 30, IsNodeBased = true, }; Spatial2DGridWrapper gridWrapper = new Spatial2DGridWrapper(grid); double rotxx = Math.Cos(30.0 / 180.0 * Math.PI); double rotxy = Math.Sin(30.0 / 180.0 * Math.PI); double rotyx = -Math.Sin(30.0 / 180.0 * Math.PI); double rotyy = Math.Cos(30.0 / 180.0 * Math.PI); Assert.AreEqual(ElementType.Point, gridWrapper.ElementType); Assert.AreEqual(12, gridWrapper.ElementCount); Assert.AreEqual(1, gridWrapper.GetVertexCount(0)); Assert.AreEqual(1, gridWrapper.GetVertexCount(3)); Assert.AreEqual(1, gridWrapper.GetVertexCount(8)); Assert.AreEqual(1, gridWrapper.GetVertexCount(11)); Assert.AreEqual(10, gridWrapper.GetVertexXCoordinate(0, 0)); Assert.AreEqual(20, gridWrapper.GetVertexYCoordinate(0, 0)); Assert.AreEqual(10 + rotxx * 6, gridWrapper.GetVertexXCoordinate(3, 0)); // 15.19 Assert.AreEqual(20 + rotyx * 6, gridWrapper.GetVertexYCoordinate(3, 0)); // 17 Assert.AreEqual(10 + rotxy * 8, gridWrapper.GetVertexXCoordinate(8, 0)); // 14 Assert.AreEqual(20 + rotyy * 8, gridWrapper.GetVertexYCoordinate(8, 0)); // 26.92 Assert.AreEqual(10 + rotxx * 6 + rotxy * 8, gridWrapper.GetVertexXCoordinate(11, 0)); // 19,19 Assert.AreEqual(20 + rotyx * 6 + rotyy * 8, gridWrapper.GetVertexYCoordinate(11, 0)); // 23.92 }
private void ReadFile(string inputFile) { TimeBufferer output = new TimeInterpolator(); TimeSet timeSet = output.TTimeSet; IElementSet elementSet; StreamReader reader = new StreamReader(inputFile); // Read quantity and imply unit string line = GetNextLine(reader).Trim(' ', '"'); Quantity quantity = new Quantity(line); if (line.Equals("Flow", StringComparison.InvariantCultureIgnoreCase)) { quantity.Unit = new Unit(PredefinedUnits.CubicMeterPerSecond); } else if (line.Equals("WaterLevel", StringComparison.InvariantCultureIgnoreCase)) { quantity.Unit = new Unit(PredefinedUnits.Meter); } else { quantity.Unit = new Unit("Unspecified unit"); } output.ValueDefinition = quantity; // Read elementset line = GetNextLine(reader); string[] elements = line.Split(';'); string elementTypeString = elements[0].Trim('"'); if (elementTypeString.Equals("IdBased", StringComparison.InvariantCultureIgnoreCase)) { var idelementSet = new ElementSet(inputFile + "-" + quantity.Caption, inputFile, ElementType.IdBased, ""); for (int i = 1; i < elements.Length; i++) { idelementSet.AddElement(new Element(elements[i].Trim('"'))); } elementSet = idelementSet; } else if (elementTypeString.Equals("Points", StringComparison.InvariantCultureIgnoreCase)) { var pelementSet = new ElementSet(inputFile + "-" + quantity.Caption, inputFile, ElementType.Point, ""); for (int i = 1; i < elements.Length; i++) { string[] coordinates = elements[i].Trim('"', '(', ')').Split(','); if (coordinates.Length < 2) { throw new InvalidDataException("Invalid file format: only one coordinate for point: " + inputFile); } Element element = new Element(); double x = Double.Parse(coordinates[0], NumberFormatInfo.InvariantInfo); double y = Double.Parse(coordinates[1], NumberFormatInfo.InvariantInfo); Coordinate elmtCoor = new Coordinate(x, y); element.Vertices = new Coordinate[] { elmtCoor }; pelementSet.AddElement(element); } elementSet = pelementSet; } else if (elementTypeString.Equals("RegularGrid", StringComparison.InvariantCultureIgnoreCase)) { Spatial2DRegularGrid grid = new Spatial2DRegularGrid(); grid.IsNodeBased = false; string[] parts = elements[1].Trim('"', '(', ')').Split(','); grid.X0 = Double.Parse(parts[0], NumberFormatInfo.InvariantInfo); grid.Y0 = Double.Parse(parts[1], NumberFormatInfo.InvariantInfo); grid.Dx = Double.Parse(parts[2], NumberFormatInfo.InvariantInfo); grid.Dy = Double.Parse(parts[3], NumberFormatInfo.InvariantInfo); grid.XCount = Int32.Parse(parts[4], NumberFormatInfo.InvariantInfo); grid.YCount = Int32.Parse(parts[5], NumberFormatInfo.InvariantInfo); grid.Orientation = (parts.Length < 5) ? 0 : Int32.Parse(parts[6], NumberFormatInfo.InvariantInfo); elementSet = new Spatial2DGridWrapper(grid); } else { throw new InvalidDataException("Invalid file format: Element type not reckognized: " + inputFile); } output.SpatialDefinition = elementSet; // Read times and values. First element is time, following are element values. ITimeSpaceValueSet valueSet = output.Values; while ((line = GetNextLine(reader)) != null) { string[] fileValues = line.Split(';'); if (fileValues.Length - 1 != elementSet.ElementCount) { throw new InvalidDataException("Number of data does not match number of elements on line: \n" + line); } DateTime timestamp = DateTime.ParseExact(fileValues[0].Trim('"'), "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture); Time time = new Time(timestamp); double[] locationValues = new double[fileValues.Length - 1]; for (int i = 1; i < fileValues.Length; i++) { locationValues[i - 1] = Double.Parse(fileValues[i].Trim('"'), NumberFormatInfo.InvariantInfo); } timeSet.Times.Add(time); valueSet.Values2D.Add(locationValues); } reader.Close(); timeSet.SetTimeHorizonFromTimes(); _outputs.Add(output); }
public override void Initialize() { Status = LinkableComponentStatus.Initializing; ReadArguments(); // Initialize storage vector _storage = new double[NumberOfElements]; for (int i = 0; i < NumberOfElements; i++) { _storage[i] = 0; } // -- Populate Exchange Items --- // Element set for a grid based (polygons) item Spatial2DRegularGrid regularGrid = new Spatial2DRegularGrid() { Description = "RegularGrid", Caption = "RegularGrid", Dx = _dx, Dy = _dy, X0 = _x0, Y0 = _y0, XCount = _xCount, YCount = _yCount, Orientation = _orientation, IsNodeBased = false, }; Spatial2DGridWrapper regularElmtSet = new Spatial2DGridWrapper(regularGrid); // Element set for a Polygon based item, of the lower left cell in the grid Element element0 = new Element("element:0"); element0.AddVertex(new Coordinate(_x0, _y0, 0)); element0.AddVertex(new Coordinate(_x0 + _dx, _y0, 0)); element0.AddVertex(new Coordinate(_x0 + _dx, _y0 + _dy, 0)); element0.AddVertex(new Coordinate(_x0, _y0 + _dy, 0)); // Element set for an ID based item, of the lower left cell in the grid ElementSet idSet = new ElementSet("FirstElement", "FirstElement", ElementType.IdBased); idSet.AddElement(element0); // is an IdBased set required to have elements? // Dimensions Dimension dimVolume = new Dimension(); dimVolume.SetPower(DimensionBase.Length, 3); Dimension dimLength = new Dimension(); dimLength.SetPower(DimensionBase.Length, 1); Dimension dimDischarge = new Dimension(); dimDischarge.SetPower(DimensionBase.Length, 3); dimDischarge.SetPower(DimensionBase.Time, -1); // Units Unit unitLiterStorage = new Unit("Storage", 0.001, 0.0, "Storage"); unitLiterStorage.Dimension = dimVolume; Unit unitGwLevel = new Unit("gw level", 1.0, 0.0, "Ground water level"); unitGwLevel.Dimension = dimLength; Unit unitDischarge = new Unit("Discharge", 0.001, 0, "Discharge into ground water model, [L/s]"); unitDischarge.Dimension = dimDischarge; // Quantities Quantity quantityStorage = new Quantity(unitLiterStorage, "Storage", "Storage"); Quantity quantityGwLevel = new Quantity(unitGwLevel, "Ground water level", "Ground water level"); Quantity quantityInflow = new Quantity(unitDischarge, "Inflow into ground water model", "Inflow"); // Storage input on the grid _storageInput = new EngineEInputItem("Grid.Storage", quantityStorage, regularElmtSet, this); _firstElementStorageInput = new EngineEInputItem("FirstElement.Storage", quantityStorage, idSet, this); // Storage output on the grid _storageOutput = new EngineEOutputItem("Grid.Storage", quantityStorage, regularElmtSet, this); _storageOutput.TimeSet.SetSingleTime(StartTime); _storageOutput.TimeSet.SetTimeHorizon(new Time(StartTime, EndTime)); // Storage output in the first element of the grid _firstElementStorageOutput = new EngineEOutputItem("FirstElement.Storage", quantityStorage, idSet, this); _firstElementStorageOutput.TimeSet.SetSingleTime(StartTime); _firstElementStorageOutput.TimeSet.SetTimeHorizon(new Time(StartTime, EndTime)); // Ground water level output. Calculated as (_gwBaseLevel + 0.1*storage height), assuming that the // water can populate 10% of the ground volume. EngineDOutputItem gwLevelOutput = new EngineDOutputItem("Grid.gwLevel", quantityGwLevel, regularElmtSet, this); gwLevelOutput.StoreValuesInExchangeItem = false; gwLevelOutput.ValueGetter = delegate() { IList <double> res = new List <double>(NumberOfElements); for (int i = 0; i < NumberOfElements; i++) { // Convert storage from liters to m3 and find the height. double storageHeight = 0.001 * _storage[i] / (_dx * _dy); res.Add(_gwBaseLevel + 10 * storageHeight); } return(new ValueSet(new List <IList> { (IList)res })); }; // Ground water inflow in the grid EngineDInputItem gwInflow = new EngineDInputItem("Grid.Inflow", quantityInflow, regularElmtSet, this); gwInflow.ValueSetter = delegate(ITimeSpaceValueSet values) { IList elmtValues = values.GetElementValuesForTime(0); for (int i = 0; i < NumberOfElements; i++) { // Values arrive in [L/s], storage is in liters. _storage[i] += (double)elmtValues[i] * _timeStepLengthInSeconds; if (_storage[i] < 0) // inflow can be negative, but storage can not be zero. Mass error here :-( { _storage[i] = 0; } } }; EngineOutputItems.Add(_storageOutput); EngineOutputItems.Add(_firstElementStorageOutput); EngineOutputItems.Add(gwLevelOutput); EngineInputItems.Add(_storageInput); EngineInputItems.Add(_firstElementStorageInput); if (_useMultiInput) { EngineInputItems.Add(new EngineMultiInputItemWrapper(gwInflow, this)); } else { EngineInputItems.Add(gwInflow); } Status = LinkableComponentStatus.Initialized; }