Skip to content

Commit 5b93549

Browse files
authored
Merge pull request #7 from chaossoftware/feature/update-to-num-methods-v2
Feature/update to num methods v2
2 parents 9b6a970 + bbd318f commit 5b93549

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+2061
-2337
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Xml.Serialization;
2+
3+
namespace ModelledSystems.Configuration;
4+
5+
public class ChartsCfg
6+
{
7+
[XmlAttribute("width")]
8+
public int Width { get; set; }
9+
10+
[XmlAttribute("height")]
11+
public int Height { get; set; }
12+
13+
[XmlAttribute("scale")]
14+
public double Scale { get; set; }
15+
16+
[XmlAttribute("labelSize")]
17+
public float LabelSize { get; set; }
18+
19+
[XmlAttribute("tickSize")]
20+
public float TickSize { get; set; }
21+
22+
[XmlAttribute("padding")]
23+
public float Padding { get; set; }
24+
25+
[XmlAttribute("grid")]
26+
public bool Grid { get; set; }
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Linq;
2+
using System.Xml;
3+
using System.Xml.Serialization;
4+
5+
namespace ModelledSystems.Configuration;
6+
7+
[XmlRoot("Config")]
8+
public class Config
9+
{
10+
[XmlElement("ModelingTask")]
11+
public TaskCfg Task { get; set; }
12+
13+
[XmlElement("Output")]
14+
public OutputCfg Out { get; set; }
15+
16+
[XmlArray("Routines")]
17+
[XmlArrayItem("Routine", typeof(RoutineCfg))]
18+
public RoutineCfg[] RoutinesList { get; set; }
19+
20+
[XmlArray("Systems")]
21+
[XmlArrayItem("System", typeof(SystemCfg))]
22+
public SystemCfg[] SystemsList { get; set; }
23+
24+
public RoutineCfg Routine =>
25+
RoutinesList.First(r => r.Name == Task.Action);
26+
27+
public SystemCfg System =>
28+
SystemsList.First(s => s.Name == Task.System);
29+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Linq;
3+
4+
namespace ModelledSystems.Configuration;
5+
6+
internal static class ConfigUtils
7+
{
8+
internal static double ParseParameterValue(string value)
9+
{
10+
string[] _operations = new string[] { "/", "*" };
11+
string operation = _operations.FirstOrDefault(o => value.Contains(o));
12+
13+
if (operation == null)
14+
{
15+
return Convert.ToDouble(value);
16+
}
17+
18+
string[] pair = value.Split(operation[0]);
19+
20+
double val1 = ParseValue(pair[0]);
21+
double val2 = ParseValue(pair[1]);
22+
23+
return GetOperationResult(val1, val2, operation);
24+
}
25+
26+
private static double ParseValue(string value)
27+
{
28+
value = value.Trim();
29+
30+
return value.Equals("pi", StringComparison.InvariantCultureIgnoreCase) ?
31+
Math.PI :
32+
Convert.ToDouble(value);
33+
}
34+
35+
private static double GetOperationResult(double val1, double val2, string operation)
36+
{
37+
return operation switch
38+
{
39+
"*" => val1 * val2,
40+
"/" => val1 / val2,
41+
_ => throw new NotImplementedException($"operation {operation} is not recognized"),
42+
};
43+
}
44+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Xml.Serialization;
2+
3+
namespace ModelledSystems.Configuration;
4+
5+
public class OrthogonalizationCfg
6+
{
7+
[XmlAttribute("type")]
8+
public string Type { get; set; }
9+
10+
[XmlAttribute("interval")]
11+
public int Interval { get; set; }
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Xml.Serialization;
2+
3+
namespace ModelledSystems.Configuration;
4+
5+
public class OutputCfg
6+
{
7+
[XmlElement("Charts")]
8+
public ChartsCfg Charts { get; set; }
9+
10+
[XmlAttribute("outDir")]
11+
public string Dir { get; set; }
12+
13+
[XmlAttribute("binaryOutput")]
14+
public bool BinOutput { get; set; }
15+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Linq;
4+
using System.Xml;
5+
using System.Xml.Serialization;
6+
7+
namespace ModelledSystems.Configuration;
8+
9+
public class RoutineCfg
10+
{
11+
[XmlAttribute("name")]
12+
public string Name { get; set; }
13+
14+
[XmlAnyAttribute]
15+
public XmlAttribute[] XAttributes { get; set; }
16+
17+
public string GetString(string name) =>
18+
XAttributes.First(a => a.Name.Equals(name)).Value;
19+
20+
public int GetInt(string name) =>
21+
Convert.ToInt32(GetString(name), CultureInfo.InvariantCulture);
22+
23+
public double GetDouble(string name) =>
24+
Convert.ToDouble(GetString(name), CultureInfo.InvariantCulture);
25+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Text.RegularExpressions;
3+
using System.Xml.Serialization;
4+
5+
namespace ModelledSystems.Configuration;
6+
7+
public class SysParamCfg
8+
{
9+
private double from = double.NaN;
10+
private double to = double.NaN;
11+
private double value = double.NaN;
12+
13+
[XmlAttribute("range")]
14+
public string rangeText;
15+
16+
[XmlAttribute("value")]
17+
public string valueText;
18+
19+
[XmlAttribute("name")]
20+
public string Name { get; set; }
21+
22+
public double From
23+
{
24+
get
25+
{
26+
if (double.IsNaN(from))
27+
{
28+
from = Convert.ToDouble(Regex.Split(rangeText, "\\.\\.")[0].Trim());
29+
}
30+
31+
return from;
32+
}
33+
}
34+
35+
public double To
36+
{
37+
get
38+
{
39+
if (double.IsNaN(to))
40+
{
41+
to = Convert.ToDouble(Regex.Split(rangeText, "\\.\\.")[1].Trim());
42+
}
43+
44+
return to;
45+
}
46+
}
47+
48+
public double Value
49+
{
50+
get
51+
{
52+
if (double.IsNaN(value))
53+
{
54+
value = ConfigUtils.ParseParameterValue(valueText);
55+
}
56+
57+
return value;
58+
}
59+
}
60+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using ChaosSoft.NumericalMethods.Ode;
2+
using System;
3+
using System.Xml.Serialization;
4+
5+
namespace ModelledSystems.Configuration;
6+
7+
public class SysSolverCfg
8+
{
9+
[XmlAttribute("name")]
10+
public string Name { get; set; }
11+
12+
[XmlAttribute("time")]
13+
public double ModellingTime { get; set; }
14+
15+
[XmlAttribute("dt")]
16+
public double Dt { get; set; }
17+
18+
public SolverType Type =>
19+
Name.ToLowerInvariant() switch
20+
{
21+
"discrete" => SolverType.Discrete,
22+
"rk4" => SolverType.RK4,
23+
"rk5" => SolverType.RK5,
24+
_ => throw new NotSupportedException($"Unknown solver {Name}"),
25+
};
26+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Linq;
4+
using System.Xml.Serialization;
5+
6+
namespace ModelledSystems.Configuration;
7+
8+
public class SystemCfg
9+
{
10+
[XmlAttribute("name")]
11+
public string Name { get; set; }
12+
13+
[XmlElement("Solver")]
14+
public SysSolverCfg Solver { get; set; }
15+
16+
[XmlArray("Parameters")]
17+
[XmlArrayItem("Param", typeof(SysParamCfg))]
18+
public SysParamCfg[] Params { get; set; }
19+
20+
[XmlElement("InitialConditions")]
21+
public string InitialConditionsValue { get; set; }
22+
23+
[XmlElement("LinearInitialConditions")]
24+
public string LinearInitialConditionsValue { get; set; }
25+
26+
public double[] InitialConditions =>
27+
InitialConditionsValue.Split(' ').Select(v => ConfigUtils.ParseParameterValue(v)).ToArray();
28+
29+
public double[,] LinearInitialConditions
30+
{
31+
get
32+
{
33+
string[] rows = LinearInitialConditionsValue.Split(';');
34+
double[,] conditions = new double[rows.Length, rows.Length];
35+
36+
for (int i = 0; i < rows.Length; i++)
37+
{
38+
string[] columns = rows[i].Trim().Split(' ');
39+
40+
for (int j = 0; j < rows.Length; j++)
41+
{
42+
conditions[i, j] = Convert.ToDouble(columns[j].Trim(), CultureInfo.InvariantCulture);
43+
}
44+
}
45+
46+
return conditions;
47+
}
48+
}
49+
50+
public double[] ParamsValues => Params.Select(p => p.Value).ToArray();
51+
}

0 commit comments

Comments
 (0)