From 553237e3674ee09e9584c79503300598fbdffa52 Mon Sep 17 00:00:00 2001 From: David Anthoff Date: Wed, 25 Nov 2015 17:23:26 -0800 Subject: [PATCH] Add julia comparison code --- Esmf/Field/FieldVariable0Dimensional.cs | 8 ++- Esmf/Field/FieldVariable1Dimensional.cs | 11 ++- Esmf/Field/FieldVariable1DimensionalTime.cs | 11 ++- Esmf/Field/FieldVariable2Dimensional.cs | 19 +++++- Esmf/Field/FieldVariable2DimensionalTime.cs | 19 +++++- Esmf/VariableInterfaces.cs | 5 ++ Fund/Fund.csproj | 1 + Fund/JuliaComparison.cs | 75 +++++++++++++++++++++ Fund/Program.cs | 11 +++ 9 files changed, 155 insertions(+), 5 deletions(-) create mode 100644 Fund/JuliaComparison.cs diff --git a/Esmf/Field/FieldVariable0Dimensional.cs b/Esmf/Field/FieldVariable0Dimensional.cs index 717af3bb..94f73a2a 100644 --- a/Esmf/Field/FieldVariable0Dimensional.cs +++ b/Esmf/Field/FieldVariable0Dimensional.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Text; using System.Diagnostics; +using System.IO; namespace Esmf { @@ -16,7 +17,7 @@ public abstract class FieldVariable0DimensionalTypeless public abstract object GetFieldSetter(); } - public class FieldVariable0Dimensional : FieldVariable0DimensionalTypeless + public class FieldVariable0Dimensional : FieldVariable0DimensionalTypeless, IVariableWriter where T : struct { private T _value; @@ -96,5 +97,10 @@ public override object GetFieldSetter() { return _setter; } + + void IVariableWriter.WriteData(StreamWriter file) + { + file.WriteLine(_value); + } } } diff --git a/Esmf/Field/FieldVariable1Dimensional.cs b/Esmf/Field/FieldVariable1Dimensional.cs index 66948204..8ff8c934 100644 --- a/Esmf/Field/FieldVariable1Dimensional.cs +++ b/Esmf/Field/FieldVariable1Dimensional.cs @@ -7,10 +7,11 @@ using System.Linq; using System.Text; using System.Diagnostics; +using System.IO; namespace Esmf { - public class FieldVariable1Dimensional : IParameter1DimensionalTypeless, IVariable1Dimensional, IParameter1Dimensional + public class FieldVariable1Dimensional : IParameter1DimensionalTypeless, IVariable1Dimensional, IParameter1Dimensional, IVariableWriter where D1 : IDimension { private JaggedArrayWrapper _values; @@ -122,5 +123,13 @@ public IEnumerable EnumerateValues() yield return _values[i]; } } + + void IVariableWriter.WriteData(StreamWriter file) + { + for(int i=0;i<_values.Length;i++) + { + file.WriteLine(_values[i]); + } + } } } diff --git a/Esmf/Field/FieldVariable1DimensionalTime.cs b/Esmf/Field/FieldVariable1DimensionalTime.cs index c03d827f..93838032 100644 --- a/Esmf/Field/FieldVariable1DimensionalTime.cs +++ b/Esmf/Field/FieldVariable1DimensionalTime.cs @@ -7,10 +7,11 @@ using System.Linq; using System.Text; using System.Diagnostics; +using System.IO; namespace Esmf { - public class FieldVariable1DimensionalTime : IParameter1DimensionalTypeless, IVariable1Dimensional, IParameter1Dimensional + public class FieldVariable1DimensionalTime : IParameter1DimensionalTypeless, IVariable1Dimensional, IParameter1Dimensional, IVariableWriter { private JaggedArrayWrapper _values; private bool[] _valuesHasBeenSet; @@ -128,5 +129,13 @@ public IEnumerable EnumerateValues() yield return _values[i]; } } + + void IVariableWriter.WriteData(StreamWriter file) + { + for (int i = 0; i < _values.Length; i++) + { + file.WriteLine(_values[i]); + } + } } } diff --git a/Esmf/Field/FieldVariable2Dimensional.cs b/Esmf/Field/FieldVariable2Dimensional.cs index 4e52fae1..2c1427fb 100644 --- a/Esmf/Field/FieldVariable2Dimensional.cs +++ b/Esmf/Field/FieldVariable2Dimensional.cs @@ -7,10 +7,11 @@ using System.Linq; using System.Text; using System.Diagnostics; +using System.IO; namespace Esmf { - public class FieldVariable2Dimensional : IParameter2DimensionalTypeless, IVariable2Dimensional, IParameter2Dimensional + public class FieldVariable2Dimensional : IParameter2DimensionalTypeless, IVariable2Dimensional, IParameter2Dimensional, IVariableWriter where D1 : IDimension where D2 : IDimension { @@ -125,5 +126,21 @@ IEnumerable> IParameter2DimensionalTypeless.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(); + } + } } } diff --git a/Esmf/Field/FieldVariable2DimensionalTime.cs b/Esmf/Field/FieldVariable2DimensionalTime.cs index cdf1a39a..faf4ee58 100644 --- a/Esmf/Field/FieldVariable2DimensionalTime.cs +++ b/Esmf/Field/FieldVariable2DimensionalTime.cs @@ -7,10 +7,11 @@ using System.Linq; using System.Text; using System.Diagnostics; +using System.IO; namespace Esmf { - public class FieldVariable2DimensionalTime : IParameter2DimensionalTypeless, IVariable2Dimensional, IParameter2Dimensional, IFieldInternal + public class FieldVariable2DimensionalTime : IParameter2DimensionalTypeless, IVariable2Dimensional, IParameter2Dimensional, IFieldInternal, IVariableWriter where D2 : IDimension { private JaggedArrayWrapper2 _values; @@ -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(); + } + } } } diff --git a/Esmf/VariableInterfaces.cs b/Esmf/VariableInterfaces.cs index f491db7b..67b3cca8 100644 --- a/Esmf/VariableInterfaces.cs +++ b/Esmf/VariableInterfaces.cs @@ -65,6 +65,11 @@ public interface IParameter1Dimensional : IParameter1DimensionalTypeless< T this[D1 index] { get; } } + public interface IVariableWriter + { + void WriteData(System.IO.StreamWriter file); + } + public interface IVariable1Dimensional { T this[D1 index] { get; set; } diff --git a/Fund/Fund.csproj b/Fund/Fund.csproj index 8fc77431..a20aa1f9 100644 --- a/Fund/Fund.csproj +++ b/Fund/Fund.csproj @@ -88,6 +88,7 @@ Properties\CommonAssemblyInfo.cs + diff --git a/Fund/JuliaComparison.cs b/Fund/JuliaComparison.cs new file mode 100644 index 00000000..881348eb --- /dev/null +++ b/Fund/JuliaComparison.cs @@ -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(); + tax_series.AddRange(Enumerable.Range(0, 1050).Select(asdf => 25.0)); + + model2["emissions"].Parameters["currtax"].SetValue((i_t, i_r) => tax_series[i_t.Value]); + model2["emissions"].Parameters["currtaxch4"].SetValue((i_t, i_r) => tax_series[i_t.Value]); + model2["emissions"].Parameters["currtaxn2o"].SetValue((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); + } + } + } + } +} diff --git a/Fund/Program.cs b/Fund/Program.cs index eb56eed6..f9e96892 100644 --- a/Fund/Program.cs +++ b/Fund/Program.cs @@ -24,6 +24,9 @@ class FundArguments [Argument(ArgumentType.AtMostOnce)] public bool MPI; + + [Argument(ArgumentType.AtMostOnce)] + public bool JuliaComparison; #pragma warning restore 0649 } @@ -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();