Skip to content

Commit 5de95e2

Browse files
committed
downgrade to .NET 4.0
add docs
1 parent 866ce5a commit 5de95e2

9 files changed

+135
-59
lines changed

Diff for: build.proj

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<SolutionDir>$(MSBuildProjectDirectory)</SolutionDir>
66
<ProjectsToBuild>src\BasicModelInterface\BasicModelInterface.csproj;src\BasicModelInterfaceRunner\BasicModelInterfaceRunner.csproj</ProjectsToBuild>
77
<NuGetCmd>$(BuildPath)\tools\NuGet</NuGetCmd>
8-
<ProgramVersion>1.0.0.5</ProgramVersion>
8+
<ProgramVersion>1.0.0.10</ProgramVersion>
99
</PropertyGroup>
1010

1111
<Target Name="Clean">

Diff for: src/BasicModelInterface.Tests/BasicModelInterface.Tests.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>BasicModelInterface.Tests</RootNamespace>
1111
<AssemblyName>BasicModelInterface.Tests</AssemblyName>
12-
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
12+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
14+
<TargetFrameworkProfile />
1415
</PropertyGroup>
1516
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1617
<DebugSymbols>true</DebugSymbols>

Diff for: src/BasicModelInterface.Tests/BasicModelInterfaceLibraryTest.cs

+5
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ public void GetValues_2D_Int()
9696

9797
var valuesExpected = new[,] { { 3, 2, 1 }, { 6, 4, 2 } };
9898

99+
100+
var v = 5.0;
101+
library.SetValue("a", v);
102+
99103
Assert.AreEqual(valuesExpected, values);
100104
}
101105

@@ -114,6 +118,7 @@ public void SetValues_2D_Int()
114118
{
115119
var values = new[,] { { 1, 2, 3 }, { 4, 5, 6 } };
116120

121+
library.SetValues(library.VariableNames[1], values);
117122
library.SetValues(library.VariableNames[1], values);
118123

119124
Assert.AreEqual(values, library.GetValues(library.VariableNames[1]));

Diff for: src/BasicModelInterface/BasicModelInterface.csproj

+12-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>BasicModelInterface</RootNamespace>
1111
<AssemblyName>BasicModelInterface</AssemblyName>
12-
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
12+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
14+
<TargetFrameworkProfile />
1415
</PropertyGroup>
1516
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1617
<DebugSymbols>true</DebugSymbols>
@@ -30,6 +31,12 @@
3031
<ErrorReport>prompt</ErrorReport>
3132
<WarningLevel>4</WarningLevel>
3233
</PropertyGroup>
34+
<PropertyGroup>
35+
<SignAssembly>true</SignAssembly>
36+
</PropertyGroup>
37+
<PropertyGroup>
38+
<AssemblyOriginatorKeyFile>BasicModelInterface.snk</AssemblyOriginatorKeyFile>
39+
</PropertyGroup>
3340
<ItemGroup>
3441
<Reference Include="System" />
3542
<Reference Include="System.Core" />
@@ -40,13 +47,17 @@
4047
<Reference Include="System.Xml" />
4148
</ItemGroup>
4249
<ItemGroup>
50+
<Compile Include="BasicModelInterfaceExtensions.cs" />
4351
<Compile Include="BasicModelInterfaceLibrary.cs" />
4452
<Compile Include="Interop\DynamicDllImport.cs" />
4553
<Compile Include="Interop\DynamicDllImportMetaObject.cs" />
4654
<Compile Include="IBasicModelInterface.cs" />
4755
<Compile Include="Logger.cs" />
4856
<Compile Include="Properties\AssemblyInfo.cs" />
4957
</ItemGroup>
58+
<ItemGroup>
59+
<None Include="BasicModelInterface.snk" />
60+
</ItemGroup>
5061
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5162
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
5263
Other similar extension points exist, see Microsoft.Common.targets.

Diff for: src/BasicModelInterface/BasicModelInterface.nuspec

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
<language>en-US</language>
1212
<projectUrl>https://github.com/openearth/bmi-csharp</projectUrl>
1313
<licenseUrl>http://www.gnu.org/licenses/lgpl.html</licenseUrl>
14-
<releaseNotes>... a list of changes in this release ...</releaseNotes>
14+
<releaseNotes>... list of changes in this release ...</releaseNotes>
1515
</metadata>
1616
<files>
17-
<file src="bin\Debug\BasicModelInterface.dll " target="lib\net45\BasicModelInterface.dll" />
18-
<file src="..\..\target\bmi-runner.exe " target="tools\net45\bmi-runner.exe" />
17+
<file src="bin\Debug\BasicModelInterface.dll" target="lib\net40\BasicModelInterface.dll" />
18+
<file src="bin\Debug\BasicModelInterface.pdb" target="lib\net40\BasicModelInterface.pdb" />
19+
<file src="..\..\target\bmi-runner.exe " target="tools\net40\bmi-runner.exe" />
1920
</files>
2021
</package>

Diff for: src/BasicModelInterface/BasicModelInterfaceLibrary.cs

+36-44
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class BasicModelInterfaceLibrary : IBasicModelInterface
1717

1818
private const int MAXSTRLEN = 1024;
1919

20-
private readonly dynamic lib;
20+
private dynamic lib;
2121

2222
private string originalCurrentDirectory;
2323

@@ -126,14 +126,15 @@ public int Finish()
126126
return lib.finalize();
127127
}
128128

129-
public T GetValue<T>(string variable)
129+
public int[] GetShape(string variable)
130130
{
131-
throw new NotImplementedException("implement using set_var, array with a single value");
132-
}
131+
var rank = 0;
132+
lib.get_var_rank(variable, ref rank);
133133

134-
public void SetValue<T>(string variable, T value)
135-
{
136-
throw new NotImplementedException("implement using get_var, array with a single value");
134+
var shape = new int[MAXDIMS];
135+
lib.get_var_shape(variable, shape);
136+
137+
return shape.Take(rank).ToArray();
137138
}
138139

139140
public string[] VariableNames
@@ -154,9 +155,14 @@ public Array GetValues(string variable)
154155
Trace.Assert(!string.IsNullOrEmpty(variable));
155156

156157
// get values (pointer)
157-
IntPtr ptr = IntPtr.Zero;
158+
var ptr = IntPtr.Zero;
158159
lib.get_var(variable, ref ptr);
159160

161+
if (ptr == IntPtr.Zero)
162+
{
163+
return null;
164+
}
165+
160166
// get rank
161167
int rank = 0;
162168
lib.get_var_rank(variable, ref rank);
@@ -169,52 +175,34 @@ public Array GetValues(string variable)
169175
// get value type
170176
var typeNameBuilder = new StringBuilder(MAXSTRLEN);
171177
lib.get_var_type(variable, typeNameBuilder);
172-
string typeName = typeNameBuilder.ToString();
178+
var typeName = typeNameBuilder.ToString();
173179

174180
// copy to 1D array
175181
var totalLength = GetTotalLength(shape);
182+
176183
var values1D = ToArray1D(ptr, typeName, totalLength);
177184

178185
if (rank == 1)
179186
{
180187
return values1D;
181188
}
182189

183-
// convert to nD array (unfotrunately can't copy to nD array at once :()
184-
var values = Array.CreateInstance(ToType(typeName), shape);
185-
186-
var index = new int[rank];
187-
var dim = rank - 1;
188-
var reset = false;
189-
for (var i = 0; i < totalLength; i++)
190-
{
191-
// increment the lowest dimension
192-
while (index[dim] == shape[dim])
193-
{
194-
for (var j = dim; j < rank; j++)
195-
{
196-
index[j] = 0;
197-
}
198-
199-
dim--;
200-
index[dim]++;
201-
reset = true;
202-
}
203-
204-
205-
// reset to the last dimension
206-
if (reset)
207-
{
208-
dim = rank - 1;
209-
reset = false;
210-
}
190+
// convert to nD array
191+
var valueType = ToType(typeName);
192+
var values = Array.CreateInstance(valueType, shape);
193+
Buffer.BlockCopy(values1D, 0, values, 0, values1D.Length * Marshal.SizeOf(valueType));
211194

212-
values.SetValue(values1D.GetValue(i), index);
195+
return values;
196+
}
213197

214-
index[dim]++;
215-
}
198+
public Array GetValues(string variable, int[] index)
199+
{
200+
throw new NotImplementedException();
201+
}
216202

217-
return values;
203+
public Array GetValues(string variable, int[] start, int[] count)
204+
{
205+
throw new NotImplementedException();
218206
}
219207

220208
public void SetValues(string variable, Array values)
@@ -265,6 +253,11 @@ public void SetValues(string variable, int[] start, int[] count, Array values)
265253
}
266254
}
267255

256+
public void SetValues(string variable, int[] index, Array values)
257+
{
258+
throw new NotImplementedException();
259+
}
260+
268261
/// <summary>
269262
/// Run model in one step from start to end.
270263
/// </summary>
@@ -276,10 +269,9 @@ public static void Run(string library, string configPath)
276269
{
277270
Logger = (level, message) => Console.WriteLine("{0}: {1}", level, message)
278271
};
279-
280272
model.Initialize(configPath);
281273

282-
int sameTimeCounter = 0;
274+
var sameTimeCounter = 0;
283275

284276
DateTime t = model.StartTime;
285277
while (t < model.StopTime)
@@ -357,7 +349,7 @@ private Array ToArray1D(IntPtr ptr, string valueType, int totalLength)
357349
throw new NotSupportedException("Unsupported type: " + valueType);
358350
}
359351

360-
private static int GetTotalLength(int[] shape)
352+
internal static int GetTotalLength(int[] shape)
361353
{
362354
return shape.Aggregate(1, (current, t) => current * t);
363355
}

Diff for: src/BasicModelInterface/IBasicModelInterface.cs

+70-5
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,89 @@ public interface IBasicModelInterface
1616

1717
TimeSpan TimeStep { get; }
1818

19+
/// <summary>
20+
/// Gets names of all available variables.
21+
/// </summary>
1922
string[] VariableNames { get; }
2023

24+
/// <summary>
25+
/// Injects logger into the model so that it can log messages. A callback method.
26+
/// </summary>
27+
Logger Logger { get; set; }
28+
29+
/// <summary>
30+
/// Initializes model using a given configuration file..
31+
/// </summary>
32+
/// <param name="path"></param>
33+
/// <returns></returns>
2134
int Initialize(string path);
2235

23-
int Update(double dt);
36+
/// <summary>
37+
/// Performs simulation.
38+
/// </summary>
39+
/// <param name="dt">When default value -1 is used - model computes time step automatically.</param>
40+
/// <returns></returns>
41+
int Update(double dt = -1);
2442

43+
/// <summary>
44+
/// Finishes model. Deallocates all arrays, closes all handles, frees all other used resources.
45+
/// </summary>
46+
/// <returns></returns>
2547
int Finish();
2648

27-
T GetValue<T>(string variable);
28-
29-
void SetValue<T>(string variable, T value);
49+
/// <summary>
50+
/// Gets shape of the variable (sizes of all dimensions).
51+
/// </summary>
52+
/// <param name="variable"></param>
53+
/// <returns></returns>
54+
int[] GetShape(string variable);
3055

56+
/// <summary>
57+
/// Gets variable values.
58+
/// </summary>
59+
/// <param name="variable"></param>
60+
/// <returns></returns>
3161
Array GetValues(string variable);
3262

63+
/// <summary>
64+
/// Gets variable values by index (flattened nD index).
65+
/// </summary>
66+
/// <param name="variable"></param>
67+
/// <param name="index"></param>
68+
/// <returns></returns>
69+
Array GetValues(string variable, int[] index);
70+
71+
/// <summary>
72+
/// Gets variable values by slice (flattened nD array of Rank x NumberOfValues).
73+
/// </summary>
74+
/// <param name="variable"></param>
75+
/// <param name="start"></param>
76+
/// <param name="count"></param>
77+
/// <returns></returns>
78+
Array GetValues(string variable, int[] start, int[] count);
79+
80+
/// <summary>
81+
/// Sets variable values.
82+
/// </summary>
83+
/// <param name="variable"></param>
84+
/// <param name="values"></param>
3385
void SetValues(string variable, Array values);
3486

87+
/// <summary>
88+
/// Sets variable values by slice (start + count for every dimension).
89+
/// </summary>
90+
/// <param name="variable"></param>
91+
/// <param name="start"></param>
92+
/// <param name="count"></param>
93+
/// <param name="values"></param>
3594
void SetValues(string variable, int[] start, int[] count, Array values);
3695

37-
Logger Logger { get; set; }
96+
/// <summary>
97+
/// Sets variable values by indices (flattened nD array of Rank x NumberOfValues).
98+
/// </summary>
99+
/// <param name="variable"></param>
100+
/// <param name="index"></param>
101+
/// <param name="values"></param>
102+
void SetValues(string variable, int[] index, Array values);
38103
}
39104
}

Diff for: src/BasicModelInterfaceRunner/App.config

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
<?xml version="1.0" encoding="utf-8" ?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
33
<startup>
4-
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
55
</startup>
6-
</configuration>
6+
</configuration>

Diff for: src/BasicModelInterfaceRunner/BasicModelInterfaceRunner.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>BasicModelInterfaceRunner</RootNamespace>
1111
<AssemblyName>bmi-runner</AssemblyName>
12-
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
12+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
14+
<TargetFrameworkProfile />
1415
</PropertyGroup>
1516
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1617
<PlatformTarget>AnyCPU</PlatformTarget>

0 commit comments

Comments
 (0)