Beispiel #1
0
        public void Add1Dimensional(string name, IEnumerable <double> data)
        {
            string parameterName = name.ToLower();
            int    parameterId   = -1;

            if (!_parameterIdsByName.TryGetValue(parameterName, out parameterId))
            {
                parameterId = _nextFreeId;
                _parameterIdsByName.Add(parameterName, parameterId);
                _nextFreeId++;
            }

            var data2 = new List <ParameterElement <double> >();

            foreach (double v in data)
            {
                data2.Add(new ParameterElementConstant <double>(new ParameterElementKey(parameterId, parameterName), v));
            }

            var parameter = new ParameterOneDimensional <double>(parameterName, parameterId, data2.ToArray());

            Add(parameter);
        }
        private void ReadExcel2007File(string filename)
        {
            var excelParameters = new Esmf.FundExcelParameterFile(filename);

            excelParameters.Load();

            foreach (Esmf.FundExcelParameterFile.Parameter p in excelParameters.Parameters)
            {
                string parameterName = p.Name.ToLower();
                int    parameterId   = -1;

                if (!_parameterIdsByName.TryGetValue(parameterName, out parameterId))
                {
                    parameterId = _nextFreeId;
                    _parameterIdsByName.Add(parameterName, parameterId);
                    _nextFreeId++;
                }

                if (p is Esmf.FundExcelParameterFile.NonDimensionalParameter)
                {
                    var typedExcelParameter = (Esmf.FundExcelParameterFile.NonDimensionalParameter)p;

                    var key = new ParameterElementKey(parameterId, parameterName);

                    var parameterValueDef = GetParameterValueDefinition(key, p.Type, typedExcelParameter.Value);

                    if (parameterValueDef.GetElementType() == typeof(double))
                    {
                        var parameter = new ParameterNonDimensional <double>(parameterName, parameterId, (ParameterElement <double>)parameterValueDef);

                        Add(parameter);
                    }
                    else if (parameterValueDef.GetElementType() == typeof(Timestep))
                    {
                        var parameter = new ParameterNonDimensional <Timestep>(parameterName, parameterId, (ParameterElement <Timestep>)parameterValueDef);

                        Add(parameter);
                    }
                    else
                    {
                        throw new InvalidOperationException();
                    }
                }
                else if (p is Esmf.FundExcelParameterFile.OneDimensionalParameter)
                {
                    var pp = (Esmf.FundExcelParameterFile.OneDimensionalParameter)p;

                    var parameterValueDef0 = GetParameterValueDefinition(null, p.Type, pp[0]);

                    if (parameterValueDef0.GetElementType() == typeof(double))
                    {
                        ParameterElement <double>[] values = new ParameterElement <double> [pp.Length];

                        for (int i = 0; i < pp.Length; i++)
                        {
                            var key = new ParameterElementKey1Dimensional(parameterId, parameterName, i);
                            values[i] = (ParameterElement <double>)GetParameterValueDefinition(key, p.Type, pp[i]);
                        }
                        var parameter = new ParameterOneDimensional <double>(parameterName, parameterId, values);

                        Add(parameter);
                    }
                    else if (parameterValueDef0.GetElementType() == typeof(Timestep))
                    {
                        ParameterElement <Timestep>[] values = new ParameterElement <Timestep> [pp.Length];

                        for (int i = 0; i < pp.Length; i++)
                        {
                            var key = new ParameterElementKey1Dimensional(parameterId, parameterName, i);
                            values[i] = (ParameterElement <Timestep>)GetParameterValueDefinition(key, p.Type, pp[i]);
                        }
                        var parameter = new ParameterOneDimensional <Timestep>(parameterName, parameterId, values);

                        Add(parameter);
                    }
                    else if (parameterValueDef0.GetElementType() == typeof(string))
                    {
                        ParameterElement <string>[] values = new ParameterElement <string> [pp.Length];

                        for (int i = 0; i < pp.Length; i++)
                        {
                            var key = new ParameterElementKey1Dimensional(parameterId, parameterName, i);
                            values[i] = (ParameterElement <string>)GetParameterValueDefinition(key, p.Type, pp[i]);
                        }
                        var parameter = new ParameterOneDimensional <string>(parameterName, parameterId, values);

                        Add(parameter);
                    }
                    else
                    {
                        throw new InvalidOperationException();
                    }
                }
                else if (p is Esmf.FundExcelParameterFile.TwoDimensionalParameter)
                {
                    var pp = (Esmf.FundExcelParameterFile.TwoDimensionalParameter)p;

                    var parameterValueDef0 = GetParameterValueDefinition(null, p.Type, pp[0, 0]);

                    if (parameterValueDef0.GetElementType() == typeof(double))
                    {
                        ParameterElement <double>[,] values = new ParameterElement <double> [pp.Count0, pp.Count1];

                        for (int i = 0; i < pp.Count0; i++)
                        {
                            for (int l = 0; l < pp.Count1; l++)
                            {
                                var key = new ParameterElementKey2Dimensional(parameterId, parameterName, i, l);
                                values[i, l] = (ParameterElement <double>)GetParameterValueDefinition(key, p.Type, pp[i, l]);
                            }
                        }

                        var parameter = new Parameter2Dimensional <double>(parameterName, parameterId, values);

                        Add(parameter);
                    }
                    else if (parameterValueDef0.GetElementType() == typeof(Timestep))
                    {
                        ParameterElement <Timestep>[,] values = new ParameterElement <Timestep> [pp.Count0, pp.Count1];

                        for (int i = 0; i < pp.Count0; i++)
                        {
                            for (int l = 0; l < pp.Count1; l++)
                            {
                                var key = new ParameterElementKey2Dimensional(parameterId, parameterName, i, l);
                                values[i, l] = (ParameterElement <Timestep>)GetParameterValueDefinition(key, p.Type, pp[i, l]);
                            }
                        }

                        var parameter = new Parameter2Dimensional <Timestep>(parameterName, parameterId, values);

                        Add(parameter);
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }
                }
                ;
            }
            ;
        }
Beispiel #3
0
        public void ReadDirectory(string directoryname)
        {
            var filenames = Directory.EnumerateFiles(directoryname);

            foreach (var filename in filenames)
            {
                string parameterName = Path.GetFileNameWithoutExtension(filename).ToLowerInvariant();
                int    parameterId   = -1;

                if (!_parameterIdsByName.TryGetValue(parameterName, out parameterId))
                {
                    parameterId = _nextFreeId;
                    _parameterIdsByName.Add(parameterName, parameterId);
                    _nextFreeId++;
                }

                var lines = (from line in File.ReadLines(filename)
                             let uncommented = line.Contains('#') ? line.Substring(0, line.IndexOf('#')) : line
                                               let trimmed = uncommented.Trim()
                                                             where trimmed.Length > 0
                                                             let columns = trimmed.Split(',')
                                                                           select columns).ToArray();

                if (lines.Select(l => l.Length).Distinct().Count() > 1)
                {
                    throw new InvalidOperationException(String.Format("Parameter file {0} has different columns counts in different lines.", filename));
                }

                int columncount = lines.Select(l => l.Length).Distinct().First();

                if (columncount == 1)
                {
                    var key = new ParameterElementKey(parameterId, parameterName);

                    var parameterValueDef = GetParameterValueDefinition2(key, lines[0][0]);

                    if (parameterValueDef.GetElementType() == typeof(double))
                    {
                        var parameter = new ParameterNonDimensional <double>(parameterName, parameterId, (ParameterElement <double>)parameterValueDef);

                        Add(parameter);
                    }
                    else if (parameterValueDef.GetElementType() == typeof(Timestep))
                    {
                        var parameter = new ParameterNonDimensional <Timestep>(parameterName, parameterId, (ParameterElement <Timestep>)parameterValueDef);

                        Add(parameter);
                    }
                    else
                    {
                        throw new InvalidOperationException();
                    }
                }
                else if (columncount == 2)
                {
                    var values = (from i in Enumerable.Range(0, lines.Length)
                                  let key = new ParameterElementKey1Dimensional(parameterId, parameterName, i)
                                            select GetParameterValueDefinition2(key, lines[i][1])).ToArray();

                    if (values.Select(i => i.GetElementType()).Distinct().Count() != 1)
                    {
                        throw new InvalidOperationException("Cannot load 1 dimensional parameter with different data types");
                    }

                    Type datatype = values.Select(i => i.GetElementType()).First();

                    if (datatype == typeof(double))
                    {
                        var parameter = new ParameterOneDimensional <double>(parameterName, parameterId, values.Cast <ParameterElement <double> >().ToArray());
                        Add(parameter);
                    }
                    else if (datatype == typeof(string))
                    {
                        var parameter = new ParameterOneDimensional <string>(parameterName, parameterId, values.Cast <ParameterElement <string> >().ToArray());
                        Add(parameter);
                    }
                    else
                    {
                        throw new InvalidOperationException();
                    }
                }
                else if (columncount == 3)
                {
                    var index0 = lines.Select(i => i[0]).Distinct().ToList();
                    var index1 = lines.Select(i => i[1]).Distinct().ToList();

                    var values = (from i in Enumerable.Range(0, lines.Length)
                                  let key = new ParameterElementKey2Dimensional(parameterId, parameterName, index0.IndexOf(lines[i][0]), index1.IndexOf(lines[i][1]))
                                            select GetParameterValueDefinition2(key, lines[i][2])).ToArray();

                    if (values.Select(i => i.GetElementType()).Distinct().Count() != 1)
                    {
                        throw new InvalidOperationException("Cannot load 2 dimensional parameter with different data types");
                    }

                    Type datatype = values.Select(i => i.GetElementType()).First();

                    if (datatype == typeof(double))
                    {
                        ParameterElement <double>[,] vals = new ParameterElement <double> [index0.Count, index1.Count];

                        foreach (var v in values)
                        {
                            var key = (ParameterElementKey2Dimensional)v.Key;
                            vals[key.D1, key.D2] = (ParameterElement <double>)v;
                        }

                        for (int i = 0; i < index0.Count; i++)
                        {
                            for (int l = 0; l < index1.Count; l++)
                            {
                                if (vals[i, l] == null)
                                {
                                    throw new InvalidOperationException("Missing element in 2 dimensional parameter");
                                }
                            }
                        }

                        var parameter = new Parameter2Dimensional <double>(parameterName, parameterId, vals);
                        Add(parameter);
                    }
                    else
                    {
                        throw new InvalidOperationException();
                    }
                }
                else
                {
                    throw new InvalidOperationException("Cannot read parameter files with more than three columns.");
                }
            }
            ;
        }