Skip to content

Commit

Permalink
Merge pull request #14 from FZJ-IEK3-VSA/develop
Browse files Browse the repository at this point in the history
Built new release version 10.8.0
  • Loading branch information
noah80 authored Feb 15, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 411c854 + f854377 commit e7c1873
Showing 8 changed files with 180 additions and 23 deletions.
1 change: 1 addition & 0 deletions Automation/UnitTestCategories.cs
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ public static class UnitTestCategories {
public const string ManualOnly = "ManualOnly";
public const string HouseholdsWithTransportation = "HouseholdsWithTransportation";
public const string HouseholdTest = "HouseholdTest";
public const string HouseholdTemplateTest = "HouseholdTemplateTest";
public const string HousetypeTests = "HousetypeTests";
public const string CalcOptionTests = "CalcOptionTests";
public const string LongTest5 = "LongTest5";
2 changes: 1 addition & 1 deletion Common/Logger.cs
Original file line number Diff line number Diff line change
@@ -367,7 +367,7 @@ private void ReportString([JetBrains.Annotations.NotNull] string message, Severi
}
if (severity <= Threshold)
{
if (OutputHelper!= null) {
if (OutputHelper != null && !Config.OutputToConsole) {
OutputHelper.WriteLine(message);
}
else {
20 changes: 20 additions & 0 deletions Common/WorkingDir.cs
Original file line number Diff line number Diff line change
@@ -148,6 +148,7 @@ public void CleanUp(int numberOfFilesToTolerate = 0, bool throwAllErrors = true)
[JetBrains.Annotations.NotNull]
private static string InitializeWorkingDirectory([JetBrains.Annotations.NotNull] string testname, bool useRamdisk) {
var baseWorkingDir = DetermineBaseWorkingDir(useRamdisk);
testname = ReplaceInvalidChars(testname);
var resultdir = Path.Combine(baseWorkingDir, testname);
try {
if (Directory.Exists(resultdir)) {
@@ -173,6 +174,25 @@ private static string InitializeWorkingDirectory([JetBrains.Annotations.NotNull]
return resultdir;
}

/// <summary>
/// Replaces all characters that are not allowed in file names with a fixed replacement character.
/// </summary>
/// <param name="filename">The file name to adjust</param>
/// <param name="replacementChar">The character to use instead of illegal characters</param>
/// <returns>The adjusted file name without any illegal characters</returns>
public static string ReplaceInvalidChars(string filename, char replacementChar = '_')
{
foreach (char c in Path.GetInvalidFileNameChars())
{
if (c == replacementChar)
{
throw new LPGException("Used an illegal character as replacementChar: '" + replacementChar + "'");
}
filename = filename.Replace(c, replacementChar);
}
return filename;
}

/// <summary>
/// Determines the base directory for any working directories used for tests.
/// </summary>
6 changes: 3 additions & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<AssemblyVersion>10.7.0.6</AssemblyVersion>
<FileVersion>10.7.0.6</FileVersion>
<AssemblyVersion>10.8.0.1</AssemblyVersion>
<FileVersion>10.8.0.1</FileVersion>
</PropertyGroup>
</Project>
</Project>
166 changes: 151 additions & 15 deletions SimulationEngine.Tests/CalculationTests.cs
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@

namespace SimulationEngine.Tests {
public enum TestDuration {
ThreeDays,
OneMonth,
ThreeMonths,
TwelveMonths
@@ -52,6 +53,7 @@ public static HouseCreationAndCalculationJob PrepareExistingHouseForTesting([Jet

public static HouseCreationAndCalculationJob PrepareNewHouseForHouseholdTesting(Simulator sim, string guid, TestDuration duration)
{

var hj = new HouseCreationAndCalculationJob
{
CalcSpec = JsonCalcSpecification.MakeDefaultsForTesting()
@@ -86,17 +88,22 @@ private static void SetEndDate(TestDuration duration, HouseCreationAndCalculatio
if (hj.CalcSpec == null) {
throw new LPGException("was null");
}
if (duration == TestDuration.OneMonth) {
hj.CalcSpec.EndDate = new DateTime(2020, 2, 1);
}
else if (duration == TestDuration.ThreeMonths) {
hj.CalcSpec.EndDate = new DateTime(2020, 4, 1);
}
else if (duration == TestDuration.TwelveMonths) {
hj.CalcSpec.EndDate = new DateTime(2020, 12, 31);
}
else {
throw new LPGException("Unkown duration");
switch (duration)
{
case TestDuration.ThreeDays:
hj.CalcSpec.EndDate = new DateTime(2020, 1, 4);
break;
case TestDuration.OneMonth:
hj.CalcSpec.EndDate = new DateTime(2020, 2, 1);
break;
case TestDuration.ThreeMonths:
hj.CalcSpec.EndDate = new DateTime(2020, 4, 1);
break;
case TestDuration.TwelveMonths:
hj.CalcSpec.EndDate = new DateTime(2020, 12, 31);
break;
default:
throw new LPGException("Unkown duration");
}
}

@@ -215,7 +222,7 @@ public static void CheckForResultfile(string wd, CalcOption option)
{

var peakWorkingSet = Process.GetCurrentProcess().PeakWorkingSet64;
const long memoryCap = 1024l * 1024l * 2000l * 2l;
const long memoryCap = 1024L * 1024L * 2000L * 2L;
peakWorkingSet.Should().BeLessThan(memoryCap);
GC.Collect();
GC.WaitForPendingFinalizers();
@@ -370,7 +377,7 @@ public static void CheckForResultfile(string wd, CalcOption option)
//}
}

public static void RunSingleHouse(Func<Simulator, HouseCreationAndCalculationJob> makeHj, Action<string> checkResults,
public static void RunSingleHouse(Func<Simulator, HouseCreationAndCalculationJob?> makeHj, Action<string> checkResults,
bool skipcleaning = false)
{
Logger.Get().StartCollectingAllMessages();
@@ -384,14 +391,64 @@ public static void RunSingleHouse(Func<Simulator, HouseCreationAndCalculationJob
var sim = new Simulator(db.ConnectionString);
var houseGenerator = new HouseGenerator();
var hj = makeHj(sim);
hj.Should().NotBeNull();
houseGenerator.ProcessSingleHouseJob(hj, sim);
if (hj.CalcSpec?.OutputDirectory == null)
if (hj!.CalcSpec?.OutputDirectory == null)
{
throw new LPGException("calcspec was null");
}

checkResults(wd.Combine(hj.CalcSpec.OutputDirectory));
}

/// <summary>
/// Generates one household from a household template and simulates them.
/// </summary>
/// <param name="testID">A unique name of the calling test for setting up a working directory</param>
/// <param name="hhTemplateGuid">The Guid of the household template to use</param>
/// <param name="duration">Duration for which the household should be simulated</param>
public static void GenerateAndSimulateHHFromTemplate(string testID, StrGuid hhTemplateGuid, TestDuration duration)
{
Logger.Get().StartCollectingAllMessages();
// set up a working directory
using var wd = new WorkingDir(testID);
wd.SkipCleaning = false;
// get a database connection
using var db = new DatabaseSetup(testID);
Directory.SetCurrentDirectory(wd.WorkingDirectory);
Simulator sim;
try
{
sim = new Simulator(db.ConnectionString);
}
catch (FormatException)
{
// during tests infrequent problems occur when loading and parsing dates from the database
// --> save the database in another folder
string backupPath = Path.Combine(WorkingDir.DetermineBaseWorkingDir(true), "InvalidDatetime_DB_Backup");
string dbFilename = Path.GetFileName(db.FileName);
string destinationFile = Path.Combine(backupPath, dbFilename);
Directory.CreateDirectory(backupPath);
File.Copy(db.FileName, destinationFile);
throw;
}
// find the household template to use
var hhTemplate = sim.HouseholdTemplates.FindByGuid(hhTemplateGuid);
hhTemplate.Should().NotBeNull();
Logger.Info("Testing generation and simulation of a household using household template \"" + hhTemplate.Name + "\"");
// generate one household
hhTemplate.Count = 1;
var households = hhTemplate.GenerateHouseholds(sim, false, new List<STTraitLimit>(), new List<TraitTag>());
households.Should().HaveCount(1);
var household = households[0];
// prepare and execute the house job
var hj = HouseJobCalcPreparer.PrepareNewHouseForHouseholdTesting(sim, household.Guid.StrVal, duration);
if (hj.CalcSpec?.CalcOptions == null) { throw new LPGException("No CalcOptions were set"); }
hj.CalcSpec.EnableIdlemode = true;
hj.CalcSpec.DefaultForOutputFiles = OutputFileDefault.OnlySums;
var houseGenerator = new HouseGenerator();
houseGenerator.ProcessSingleHouseJob(hj, sim);
}
}


@@ -813,6 +870,85 @@ public void GenerateSystematicHouseholdTestsWithTransport()
sw.Close();
}

/// <summary>
/// Tests generation of households using a household template. Generates 100 households for each
/// template, and simulates each household for 3 days.
/// </summary>
/// <param name="hhTemplateGuid">Guid of the template to test</param>
/// <param name="hhTemplateName">Name of the template to test</param>
/// <param name="repetition">Repetition of this test for the same template</param>
/// <remarks>Each call of this method only handles a single household</remarks>
[Theory]
[MemberData(nameof(GetEachHHTemplate100Times))]
[Trait(UnitTestCategories.Category, UnitTestCategories.HouseholdTemplateTest)]
public void SystematicHouseholdTemplateTestShort(StrGuid hhTemplateGuid, string hhTemplateName, int repetition)
{
string testID = "CalculationTests.SystematicHouseholdTemplateTestShort." + hhTemplateName + "." + repetition;
HouseJobTestHelper.GenerateAndSimulateHHFromTemplate(testID, hhTemplateGuid, TestDuration.ThreeDays);
}

/// <summary>
/// Tests generation of households using a household template. Generates 3 households for each
/// template, and simulates each household for one year.
/// </summary>
/// <param name="hhTemplateGuid">Guid of the template to test</param>
/// <param name="hhTemplateName">Name of the template to test</param>
/// <param name="repetition">Repetition of this test for the same template</param>
/// <remarks>Each call of this method only handles a single household</remarks>
[Theory]
[MemberData(nameof(GetEachHHTemplate3Times))]
[Trait(UnitTestCategories.Category, UnitTestCategories.HouseholdTemplateTest)]
public void SystematicHouseholdTemplateTestLong(StrGuid hhTemplateGuid, string hhTemplateName, int repetition)
{
string testID = "CalculationTests.SystematicHouseholdTemplateTestLong." + hhTemplateName + "." + repetition;
HouseJobTestHelper.GenerateAndSimulateHHFromTemplate(testID, hhTemplateGuid, TestDuration.TwelveMonths);
}

/// <summary>
/// Creates an enumerable of all household template guids and names, repeating each template 100 times.
/// </summary>
/// <returns>An <see cref="IEnumerable{T}"/> of household template guids and names</returns>
public static IEnumerable<object[]> GetEachHHTemplate100Times()
{
const string testname = "CalculationTests.GetEachHHTemplate100Times";
return CreateTemplateList(testname, 100);
}

/// <summary>
/// Creates an enumerable of all household template guids and names, repeating each template 3 times.
/// </summary>
/// <returns>An <see cref="IEnumerable{T}"/> of household template guids and names</returns>
public static IEnumerable<object[]> GetEachHHTemplate3Times()
{
const string testname = "CalculationTests.GetEachHHTemplate3Times";
return CreateTemplateList(testname, 3);
}

/// <summary>
/// Creates an <see cref="IEnumerable{T}"/> of the guids and names of all household templates, including every template multiple times.
/// Can be used to create test cases for template tests, where each template should be used to generate multiple households.
/// </summary>
/// <param name="testname">The name of the test, for creation of a unique database copy</param>
/// <param name="repetitionCount">How often each template will be repeated</param>
/// <returns>An <see cref="IEnumerable{T}"/> of object arrays, each including name and guid of one household template and the
/// repetition index (how often this template has been repeated already)</returns>
/// <remarks>The return type needs to be an <see cref="IEnumerable{T}"/> of object arrays for the testing framework.</remarks>
public static IEnumerable<object[]> CreateTemplateList(string testname, int repetitionCount = 1)
{
// configure output to console because the instance of class CalculationTests has not been
// created yet and therefore no TestOutputHelper is available
var outputToConsole = Config.OutputToConsole;
Config.OutputToConsole = true;
// get a database connection
using var db = new DatabaseSetup(testname);
var sim = new Simulator(db.ConnectionString);
// reset OutputToConsole, so that the actual tests might use TestOutputHelpers
Config.OutputToConsole = outputToConsole;
// return each template as often as specified, together with the index
return sim.HouseholdTemplates.Items.SelectMany(
template => Enumerable.Range(0, repetitionCount).Select(i => new object[] { template.Guid, template.Name, i }));
}

[Fact]
[Trait(UnitTestCategories.Category, UnitTestCategories.ManualOnly)]
public void GenerateHouseTypeTests()
@@ -1183,4 +1319,4 @@ public SingleCalculationTests([JetBrains.Annotations.NotNull] ITestOutputHelper
{
}
}
}
}
2 changes: 1 addition & 1 deletion SimulationEngineLib/PythonGenerator.cs
Original file line number Diff line number Diff line change
@@ -57,7 +57,7 @@ public void MakePythonData([JetBrains.Annotations.NotNull] string connectionStri
//sw.WriteLine("from dataclasses import dataclass, field");
//sw.WriteLine("from dataclasses_json import dataclass_json # type: ignore");
//sw.WriteLine("from typing import List, Optional, Any, Dict");
sw.WriteLine("from lpgpythonbindings import *");
sw.WriteLine("from pylpg.lpgpythonbindings import *");
//sw.WriteLine("from enum import Enum");
sw.WriteLine();
WriteNames(sim.LoadTypes.Items.Select(x => (DBBase)x).ToList(), sw, "LoadTypes");
4 changes: 2 additions & 2 deletions VersionInfo.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[assembly: AssemblyVersion("10.7.0.6")]
[assembly: AssemblyVersion("10.8.0.1")]

[assembly: AssemblyFileVersion("10.7.0.6")]
[assembly: AssemblyFileVersion("10.8.0.1")]
2 changes: 1 addition & 1 deletion copynewToPython.cmd
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ pause


REM create pylpg
set "releasedirectory=C:\LPGReleaseMakerResults\LPGReleases\releases10.7.0"
set "releasedirectory=C:\LPGReleaseMakerResults\LPGReleases\releases10.8.0"
set "pylpgdirectory=C:\LPGPythonBindings\pylpg\"

cd %releasedirectory%\net48

0 comments on commit e7c1873

Please sign in to comment.