Skip to content

Commit

Permalink
Add julia comparison code
Browse files Browse the repository at this point in the history
  • Loading branch information
davidanthoff committed Nov 27, 2015
1 parent 02e5773 commit 553237e
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 5 deletions.
8 changes: 7 additions & 1 deletion Esmf/Field/FieldVariable0Dimensional.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace Esmf
{
Expand All @@ -16,7 +17,7 @@ public abstract class FieldVariable0DimensionalTypeless
public abstract object GetFieldSetter();
}

public class FieldVariable0Dimensional<T> : FieldVariable0DimensionalTypeless
public class FieldVariable0Dimensional<T> : FieldVariable0DimensionalTypeless, IVariableWriter
where T : struct
{
private T _value;
Expand Down Expand Up @@ -96,5 +97,10 @@ public override object GetFieldSetter()
{
return _setter;
}

void IVariableWriter.WriteData(StreamWriter file)
{
file.WriteLine(_value);
}
}
}
11 changes: 10 additions & 1 deletion Esmf/Field/FieldVariable1Dimensional.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace Esmf
{
public class FieldVariable1Dimensional<D1, T> : IParameter1DimensionalTypeless<T>, IVariable1Dimensional<D1, T>, IParameter1Dimensional<D1, T>
public class FieldVariable1Dimensional<D1, T> : IParameter1DimensionalTypeless<T>, IVariable1Dimensional<D1, T>, IParameter1Dimensional<D1, T>, IVariableWriter
where D1 : IDimension
{
private JaggedArrayWrapper<T> _values;
Expand Down Expand Up @@ -122,5 +123,13 @@ public IEnumerable<T> EnumerateValues()
yield return _values[i];
}
}

void IVariableWriter.WriteData(StreamWriter file)
{
for(int i=0;i<_values.Length;i++)
{
file.WriteLine(_values[i]);
}
}
}
}
11 changes: 10 additions & 1 deletion Esmf/Field/FieldVariable1DimensionalTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace Esmf
{
public class FieldVariable1DimensionalTime<T> : IParameter1DimensionalTypeless<T>, IVariable1Dimensional<Timestep, T>, IParameter1Dimensional<Timestep, T>
public class FieldVariable1DimensionalTime<T> : IParameter1DimensionalTypeless<T>, IVariable1Dimensional<Timestep, T>, IParameter1Dimensional<Timestep, T>, IVariableWriter
{
private JaggedArrayWrapper<T> _values;
private bool[] _valuesHasBeenSet;
Expand Down Expand Up @@ -128,5 +129,13 @@ public IEnumerable<T> EnumerateValues()
yield return _values[i];
}
}

void IVariableWriter.WriteData(StreamWriter file)
{
for (int i = 0; i < _values.Length; i++)
{
file.WriteLine(_values[i]);
}
}
}
}
19 changes: 18 additions & 1 deletion Esmf/Field/FieldVariable2Dimensional.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace Esmf
{
public class FieldVariable2Dimensional<D1, D2, T> : IParameter2DimensionalTypeless<T>, IVariable2Dimensional<D1, D2, T>, IParameter2Dimensional<D1, D2, T>
public class FieldVariable2Dimensional<D1, D2, T> : IParameter2DimensionalTypeless<T>, IVariable2Dimensional<D1, D2, T>, IParameter2Dimensional<D1, D2, T>, IVariableWriter
where D1 : IDimension
where D2 : IDimension
{
Expand Down Expand Up @@ -125,5 +126,21 @@ IEnumerable<Parameter2DimensionalMember<T>> IParameter2DimensionalTypeless<T>.Ge
}
}
}

void IVariableWriter.WriteData(StreamWriter file)
{
for (int i = 0; i < _values.Length0; i++)
{
for (int j = 0; j < _values.Length1; j++)
{
if(j>0)
{
file.Write(",");
}
file.Write(_values[i, j]);
}
file.WriteLine();
}
}
}
}
19 changes: 18 additions & 1 deletion Esmf/Field/FieldVariable2DimensionalTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace Esmf
{
public class FieldVariable2DimensionalTime<D2, T> : IParameter2DimensionalTypeless<T>, IVariable2Dimensional<Timestep, D2, T>, IParameter2Dimensional<Timestep, D2, T>, IFieldInternal
public class FieldVariable2DimensionalTime<D2, T> : IParameter2DimensionalTypeless<T>, IVariable2Dimensional<Timestep, D2, T>, IParameter2Dimensional<Timestep, D2, T>, IFieldInternal, IVariableWriter
where D2 : IDimension
{
private JaggedArrayWrapper2<T> _values;
Expand Down Expand Up @@ -145,5 +146,21 @@ void IFieldInternal.SwitchOffChecks()
{
_switchOffChecks = true;
}

void IVariableWriter.WriteData(StreamWriter file)
{
for (int i = 0; i < _values.Length0; i++)
{
for (int j = 0; j < _values.Length1; j++)
{
if (j > 0)
{
file.Write(",");
}
file.Write(_values[i, j]);
}
file.WriteLine();
}
}
}
}
5 changes: 5 additions & 0 deletions Esmf/VariableInterfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public interface IParameter1Dimensional<D1, T> : IParameter1DimensionalTypeless<
T this[D1 index] { get; }
}

public interface IVariableWriter
{
void WriteData(System.IO.StreamWriter file);
}

public interface IVariable1Dimensional<D1, T>
{
T this[D1 index] { get; set; }
Expand Down
1 change: 1 addition & 0 deletions Fund/Fund.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<Link>Properties\CommonAssemblyInfo.cs</Link>
</Compile>
<Compile Include="FundModel.cs" />
<Compile Include="JuliaComparison.cs" />
<Compile Include="LongtermDiagnosticOutput.cs" />
<Compile Include="MarginalDamage.cs" />
<Compile Include="MarginalDamage3.cs" />
Expand Down
75 changes: 75 additions & 0 deletions Fund/JuliaComparison.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Esmf;
using Fund.CommonDimensions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Fund
{
public static class JuliaComparison
{
public static void Run()
{
// Load parameters
var parameters = new Parameters();
parameters.ReadDirectory(@"Data\Base");

// Get best guess parameter values
var parameterValues = parameters.GetBestGuess();

// Create a new model that inits itself from the parameters just loaded
var model1 = FundModel.GetModel();

// Run the model
var rs1 = model1.Run(parameterValues);

var d1 = from p in rs1.GetDimensionalFieldsOperator()
where p.Values is IVariableWriter
orderby p.ComponentName, p.FieldName
select new { ComponentName = p.ComponentName, FieldName = p.FieldName };

// This will hold results from a BAU run
Directory.CreateDirectory("JuliaComp1");

foreach (var i in d1)
{
using (var file = new StreamWriter(@"JuliaComp1\" + i.ComponentName + "." + i.FieldName + ".csv"))
{
(rs1[i.ComponentName, i.FieldName] as IVariableWriter).WriteData(file);
}
}

// Create a new model that inits itself from the parameters just loaded
var model2 = FundModel.GetModel();

var tax_series = new List<double>();
tax_series.AddRange(Enumerable.Range(0, 1050).Select(asdf => 25.0));

model2["emissions"].Parameters["currtax"].SetValue<Timestep, Region, double>((i_t, i_r) => tax_series[i_t.Value]);
model2["emissions"].Parameters["currtaxch4"].SetValue<Timestep, Region, double>((i_t, i_r) => tax_series[i_t.Value]);
model2["emissions"].Parameters["currtaxn2o"].SetValue<Timestep, Region, double>((i_t, i_r) => tax_series[i_t.Value]);

// Run the model
var rs2 = model2.Run(parameterValues);

var d2 = from p in rs2.GetDimensionalFieldsOperator()
where p.Values is IVariableWriter
orderby p.ComponentName, p.FieldName
select new { ComponentName = p.ComponentName, FieldName = p.FieldName };

// This will hold results from a policy run
Directory.CreateDirectory("JuliaComp2");

foreach (var i in d2)
{
using (var file = new StreamWriter(@"JuliaComp2\" + i.ComponentName + "." + i.FieldName + ".csv"))
{
(rs2[i.ComponentName, i.FieldName] as IVariableWriter).WriteData(file);
}
}
}
}
}
11 changes: 11 additions & 0 deletions Fund/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class FundArguments

[Argument(ArgumentType.AtMostOnce)]
public bool MPI;

[Argument(ArgumentType.AtMostOnce)]
public bool JuliaComparison;
#pragma warning restore 0649
}

Expand Down Expand Up @@ -70,6 +73,14 @@ private static void Main2(FundArguments lParsedCmdArgs)

LongtermDiagnosticOutput.Run(lParsedCmdArgs.Diagnostic);
}
else if(lParsedCmdArgs.JuliaComparison)
{
Console.WriteLine();
Console.WriteLine("Julia comparison mode");
Console.WriteLine();

JuliaComparison.Run();
}
else
{
Console.WriteLine();
Expand Down

0 comments on commit 553237e

Please sign in to comment.