Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
- All unit tests pass
- Installer successful
  • Loading branch information
Iain Buchanan committed Oct 30, 2013
2 parents 84cb5e2 + c4a85eb commit d716212
Show file tree
Hide file tree
Showing 39 changed files with 318 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.1.*")]
[assembly: AssemblyFileVersion("2.1.*")]
[assembly: AssemblyVersion("2.2.*")]
[assembly: AssemblyFileVersion("2.2.*")]
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.1.*")]
[assembly: AssemblyFileVersion("2.1.*")]
[assembly: AssemblyVersion("2.2.*")]
[assembly: AssemblyFileVersion("2.2.*")]
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.1.*")]
[assembly: AssemblyFileVersion("2.1.*")]
[assembly: AssemblyVersion("2.2.*")]
[assembly: AssemblyFileVersion("2.2.*")]
Binary file modified PWC.Asim/Data/Asim Addin.xla
Binary file not shown.
Binary file modified PWC.Asim/Data/Example.xls
Binary file not shown.
Binary file modified PWC.Asim/Data/Example.xlsx
Binary file not shown.
Binary file added PWC.Asim/Data/load.zip
Binary file not shown.
12 changes: 10 additions & 2 deletions PWC.Asim/Data/tma.bat
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ set FILES=
echo starting tma with %* > tma.log
call :splitFiles %ASIM_OUTPUTFILES%
set "FILESBS=%FILES:\=/%"
echo call %1 %FILESBS% >> tma.log
set FILE_NAME="%~nx1"
set FILE_FOLDER="%~dp1"
cd %FILE_FOLDER%
echo call %FILE_NAME% %FILESBS% >> tma.log
call %1 %FILESBS%
endlocal
GOTO:EOF
Expand All @@ -15,7 +18,8 @@ GOTO:EOF
set tosplit=%*
for /F "tokens=1,* delims=," %%A in ("%tosplit%") DO (
rem echo parsing %%A
call :FullPath "%%A" FP
call :QuoteString %%A str
call :FullPath !str! FP
echo received path !FP! >> tma.log
set FILES=!FILES! -get "!FP!"
IF [%%B] == [] exit /b
Expand All @@ -26,3 +30,7 @@ GOTO:EOF
:FullPath
set "%~2=%~dpnx1"
GOTO:EOF

:QuoteString
for /f "useback tokens=*" %%A in ('%1') do set %~2="%%~A"
GOTO:EOF
5 changes: 5 additions & 0 deletions PWC.Asim/PWC.Asim.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ enum Arguments
Algorithm,
GeneratorStats,
Report,
Eval,
Debug
}

Expand Down Expand Up @@ -163,6 +164,10 @@ static void Main(string[] args)
_simulator.AddReport(arglist.Dequeue(), arglist.Dequeue());
break;

case Arguments.Eval:
_simulator.AddEval(arglist.Dequeue());
break;

case Arguments.Debug:
log.Debug = true;
log.WriteLine("Turning on debug logging");
Expand Down
4 changes: 2 additions & 2 deletions PWC.Asim/PWC.Asim.ConsoleApp/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.1.*")]
[assembly: AssemblyFileVersion("2.1.*")]
[assembly: AssemblyVersion("2.2.*")]
[assembly: AssemblyFileVersion("2.2.*")]
2 changes: 1 addition & 1 deletion PWC.Asim/PWC.Asim.Core/Actors/OutputData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public OutputData(string filename, string[] vars, uint outputEvery = 1, DateTime
_filename = filename;
try
{
_file = new FileStream(_filename, FileMode.OpenOrCreate, FileAccess.Write);
_file = new FileStream(_filename, FileMode.Create, FileAccess.Write);
var buf = new BufferedStream(_file);
_stream = new StreamWriter(buf);
}
Expand Down
183 changes: 183 additions & 0 deletions PWC.Asim/PWC.Asim.Core/Actors/RunTimeExtentions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.CSharp;
using PWC.Asim.Core.Contracts;
using PWC.Asim.Core.Exceptions;
using PWC.Asim.Core.Utils;

namespace PWC.Asim.Core.Actors
{
class RunTimeExtentions : IActor
{
private readonly SharedContainer _sharedVars = SharedContainer.Instance;
private int _asimRteCnt = 0;
private readonly IList<string> _runTimeExtensions = new List<string>();
private Delegates.EvalBlock[] _evalBlocks;
// private readonly IList<double> _times = new List<double>();

public RunTimeExtentions(IEnumerable<string> files = null)
{
if (files == null)
return;
foreach (var file in files)
{
File.ReadAllLines(file).ToList().ForEach(l => _runTimeExtensions.Add(l));
}
}

public void Init()
{

}

public void Run(ulong iteration)
{
// DateTime st, et;
if (iteration == 0)
{
if (!_runTimeExtensions.Any())
return;
Console.WriteLine("RTE Parsing run-time extensions");
// st = DateTime.Now;
ParseLines(_runTimeExtensions);
// et = DateTime.Now;
// Console.WriteLine("RTE Loaded run-time extensions in {0}s", (et - st).TotalSeconds);
Console.WriteLine("RTE Loaded run-time extensions");
}

if (_asimRteCnt == 0)
return;
// run all evals
// st = DateTime.Now;
for (int i = 0; i < _asimRteCnt; i++)
{
_evalBlocks[i](iteration);
}
//et = DateTime.Now;
//_times.Add((et - st).TotalSeconds);
}

public void Finish()
{
if (_asimRteCnt == 0)
return;
double tot = 0;
//_times.ToList().ForEach(t => tot += t);
//Console.WriteLine("RTE total time {0}s, ave time {1}s", tot, tot / _times.Count);
}

private void ParseLines(IList<String> lines)
{
var c = new CSharpCodeProvider();
ICodeCompiler icc = c.CreateCompiler();
var cp = new CompilerParameters();

cp.CompilerOptions = "/t:library";
cp.GenerateInMemory = true;

cp.ReferencedAssemblies.Add("system.dll");
var thisAsembly = typeof(RunTimeExtentions).Assembly.CodeBase;
thisAsembly = new Uri(thisAsembly).LocalPath;
// cp.ReferencedAssemblies.Add("PWC.Asim.Core.dll");
cp.ReferencedAssemblies.Add(thisAsembly);

var codeBuilder = new StringBuilder();
var methods = new StringBuilder();
var varInstance = new StringBuilder();

var sharedVars = new List<string>();
var wordRegex = new Regex(@"\b[A-Z][A-Za-z0-9]+\b");
// look for MixedCaseWords in the eval lines
foreach (var line in lines)
{
var matches = wordRegex.Matches(line);
foreach (Match match in matches)
{
sharedVars.Add(match.Value);
}
}
// trim words into an actual list of shared vars:
sharedVars = _sharedVars.GetAllNames().Intersect(sharedVars).ToList();

// now go back and replace real shared "Name" with "Name.Val"
foreach (var line in lines)
{
if (string.IsNullOrWhiteSpace(line))
continue;
string replacedLine = line;
sharedVars.ForEach(s => replacedLine = replacedLine.Replace(s, s + ".Val"));
// build the list of methods
methods.Append(string.Format(MethodFmt, _asimRteCnt++, replacedLine, _asimRteCnt));
}

foreach (var v in sharedVars)
{
// build the list of private shared var instances
varInstance.Append(string.Format(InstanceFmt, v));
}
// build the code
codeBuilder.Append(string.Format(PlumbingFmt, varInstance, methods));

var code = codeBuilder.ToString();
CompilerResults cr = icc.CompileAssemblyFromSource(cp, code);
if (cr.Errors.Count > 0)
{
var e = cr.Errors[0];
Console.WriteLine("RTE ERROR on line {0}: {1}", e.Line, e.ErrorText);
Console.WriteLine("==========");
var l = 1;
foreach (var line in code.Split('\n'))
{
Console.WriteLine("{0,3}: {1}", l++, line);
}
Console.WriteLine("==========");
throw new SimulationException("The Run Time Evalution file failed to compile.");
}

Console.WriteLine("RTE compiled successfully.");

Assembly a = cr.CompiledAssembly;
object rteClass = a.CreateInstance("PWC.Asim.Core.Eval.AsimRteClass");
Type t = rteClass.GetType();
_evalBlocks = new Delegates.EvalBlock[_asimRteCnt];
for (int i = 0; i < _asimRteCnt; i++)
{
MethodInfo evalFunc = t.GetMethod("EvalMethod" + i);
var d = Delegate.CreateDelegate(typeof(Delegates.EvalBlock), rteClass, evalFunc);
_evalBlocks[i] = (Delegates.EvalBlock)d;
}
}

const string PlumbingFmt = @"
using System;
using PWC.Asim.Core.Utils;
namespace PWC.Asim.Core.Eval
{{
public class AsimRteClass
{{
private static readonly SharedContainer Share = SharedContainer.Instance;
// Begin Instances of shared variables
{0}
// End Instances of shared variables
// Begin definition of methods to evaluate
{1}
// End definition of methods to evaluate
}}
}}";
const string InstanceFmt = @" private readonly Shared {0} = Share.GetOrNew(""{0}"");
";
const string MethodFmt = @"
public object EvalMethod{0}(ulong it)
{{
return {1}; // eval line {2}
}}
";
}
}
7 changes: 7 additions & 0 deletions PWC.Asim/PWC.Asim.Core/Contracts/Delegates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,12 @@ public class Delegates
public delegate double SolarController(double pvAvailP, double lastSetP,
double genP, double genSpinP, double genIdealP,
double loadP, double statSpinSetP, double switchDownP);

/// <summary>
/// All eval blocks will be run in a method with this signature. The return type is ignored.
/// </summary>
/// <param name="it">The current iteration</param>
/// <returns></returns>
public delegate object EvalBlock(ulong it);
}
}
1 change: 1 addition & 0 deletions PWC.Asim/PWC.Asim.Core/PWC.Asim.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Actors\RunTimeExtentions.cs" />
<Compile Include="Actors\SheddableLoadMgr.cs" />
<Compile Include="Actors\GenMgr.cs" />
<Compile Include="Actors\Load.cs" />
Expand Down
4 changes: 2 additions & 2 deletions PWC.Asim/PWC.Asim.Core/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.1.*")]
[assembly: AssemblyFileVersion("2.1.*")]
[assembly: AssemblyVersion("2.2.*")]
[assembly: AssemblyFileVersion("2.2.*")]
11 changes: 9 additions & 2 deletions PWC.Asim/PWC.Asim.Core/Utils/Simulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ private class ReportFile
private Timer _timer;
private List<InputOption> _inputActors = new List<InputOption>();
private List<OutputOption> _outputActors = new List<OutputOption>();
private List<ReportFile> _reportFiles = new List<ReportFile>();
private readonly List<ReportFile> _reportFiles = new List<ReportFile>();
private readonly List<string> _evalFiles = new List<string>();
private StreamWriter _watchWriter;
private readonly Dictionary<string, string> _controllers = new Dictionary<string, string>();
private readonly SharedContainer _sharedVars = SharedContainer.Instance;
Expand Down Expand Up @@ -100,16 +101,22 @@ public void AddReport(string template, string output)
_reportFiles.Add(new ReportFile() {Template = template, Output = output});
}

public void AddEval(string file)
{
_evalFiles.Add(file);
}

#endregion Options

public void Simulate()
{
int nActors = _inputActors.Count + _outputActors.Count + 5; // 5 is number of explicit classes set below
int nActors = _inputActors.Count + _outputActors.Count + 6; // 6 is number of explicit classes set below
int iA = 0;
var actors = new IActor[nActors];

_inputActors.ForEach(i => actors[iA++] = new NextData(i.Filename, StartTime, i.Recycle));
// add extra simulation actors here. Order is important:
actors[iA++] = new RunTimeExtentions(_evalFiles);
actors[iA++] = new Load();
actors[iA++] = new Station();
actors[iA++] = new SheddableLoadMgr();
Expand Down
3 changes: 3 additions & 0 deletions PWC.Asim/PWC.Asim.ExcelTools/Interface/ConfigSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public class ConfigSettings

public List<TemplateInformation> Reports { get; set; }

public List<String> EvalFiles { get; set; }

public LogFileInformation LogInformation { get; set; }

private DateTime _dateSimulatorRun;
Expand All @@ -93,6 +95,7 @@ public ConfigSettings()
Reports = new List<TemplateInformation>();
ExtraArgList = new List<List<string>>();
BatchCommands = new List<List<string>>();
EvalFiles = new List<string>();
RunSimulator = true;
_dateSimulatorRun = DateTime.Now;
Simulator = Environment.GetEnvironmentVariable("ProgramFiles") + @"\Power Water Corporation\Asim\Asim.exe";
Expand Down
9 changes: 9 additions & 0 deletions PWC.Asim/PWC.Asim.ExcelTools/Logic/AutomateWorksheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,15 @@ private void ParseConfigData(MyWorksheet worksheet)
});
break;

case "eval":
for (int j = 2; j <= data.GetLength(1); j++)
{
var cell = data[i, j];
if (cell != null)
_settings.EvalFiles.Add(cell.ToString());
}
break;

default:
Console.WriteLine("unknown option: '" + s + "'");
break;
Expand Down
6 changes: 6 additions & 0 deletions PWC.Asim/PWC.Asim.ExcelTools/Logic/Simulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ private string GenerateArguments(ConfigSettings settings)
}
});

settings.EvalFiles.ForEach(file =>
{
args.Append(" --eval ");
args.Append(Helper.Quote + file + Helper.Quote);
});

if (!string.IsNullOrWhiteSpace(settings.WatchFile) && settings.WatchGlobs.Any())
{
args.Append(" --watch ");
Expand Down
4 changes: 2 additions & 2 deletions PWC.Asim/PWC.Asim.ExcelTools/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.1.*")]
[assembly: AssemblyFileVersion("2.1.*")]
[assembly: AssemblyVersion("2.2.*")]
[assembly: AssemblyFileVersion("2.2.*")]
Loading

0 comments on commit d716212

Please sign in to comment.