/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { bool _active = true; DA.GetData(0, ref _active); Active active = (_active) ? Active.YES : Active.NO; SOR sor = new SOR(active); DA.SetData(0, sor); }
/// <summary>Gets the implied Black volatility of a specific european call option. /// </summary> /// <param name="strike">The strike.</param> /// <param name="forward">The forward.</param> /// <param name="timeToExpiry">The time span between valuation date and expiry date in its <see cref="System.Double"/> representation.</param> /// <param name="c0">The dimensionless option price, i.e. option price divided by forward and discount factor.</param> /// <param name="value">The implied Black volatility (output).</param> /// <returns>A value indicating whether <paramref name="value"/> contains valid data.</returns> private ImpliedCalculationResultState TryGetImpliedCallVolatility(double strike, double forward, double timeToExpiry, double c0, out double value) { var x = Math.Log(forward / strike); var expOfMinusx = strike / forward; if (x > 0) // "in-out" duality { x = -x; expOfMinusx = forward / strike; c0 = expOfMinusx * c0 + 1 - expOfMinusx; // expOfMinusx = exp( [original] x ) } var v0 = SOR.GetInitialCallOptionTotalVolatility(x, c0); var w0 = (v0 * v0 - 2 * Math.Abs(x)) / (v0 * v0 + 2 * Math.Abs(x)); var oneOverOnePlusW = 1.0 / (1 + w0); var v = v0; var w = w0; for (int j = 0; j < m_MaxNumberOfIterations; j++) { var nPlus = StandardNormalDistribution.GetCdfValue(x / v + 0.5 * v); var nMinus = expOfMinusx * StandardNormalDistribution.GetCdfValue(x / v - 0.5 * v); double c = nPlus - nMinus; // undiscounted normalized option value var temp = StandardNormalDistribution.GetInverseCdfValue((c0 + nMinus + w * nPlus) / (1.0 + w)); v = temp + Math.Sqrt(temp * temp + 2.0 * Math.Abs(x)); w = (v * v - 2 * Math.Abs(x)) / (v * v + 2 * Math.Abs(x)); if (Math.Abs(c - c0) < m_Tolerance) { value = v / Math.Sqrt(timeToExpiry); // here, we assume that the 'new' total volatility estimation is better than the one used for the calculation of 'c' return(ImpliedCalculationResultState.ProperResult); } } value = v / Math.Sqrt(timeToExpiry); return(ImpliedCalculationResultState.NoProperResult); }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { MainSettings _mainSettings = null; List <Configuration> otherSettings_ = new List <Configuration> (); bool _runIt = false; DA.GetData(0, ref _mainSettings); DA.GetDataList(1, otherSettings_); DA.GetData(2, ref _runIt); SimpleForcingSettings simpleForcingSettings = null; TThread tThread = null; TimeSteps timeSteps = null; ModelTiming modelTiming = null; SoilConfig soilConfig = null; Sources sources = null; Turbulence turbulence = null; OutputSettings outputSettings = null; Cloud cloud = null; Background background = null; SolarAdjust solarAdjust = null; BuildingSettings buildingSettings = null; IVS ivs = null; ParallelCPU parallelCPU = null; SOR sor = null; InflowAvg inflowAvg = null; Facades facades = null; PlantSetting plantSetting = null; LBC lbc = null; FullForcing fullForcing = null; if (_runIt) { try { foreach (Configuration o in otherSettings_) { Type obj = o.GetType(); if (obj == typeof(SimpleForcingSettings)) { simpleForcingSettings = o as SimpleForcingSettings; } else if (obj == typeof(TThread)) { tThread = o as TThread; } else if (obj == typeof(TimeSteps)) { timeSteps = o as TimeSteps; } else if (obj == typeof(ModelTiming)) { modelTiming = o as ModelTiming; } else if (obj == typeof(SoilConfig)) { soilConfig = o as SoilConfig; } else if (obj == typeof(Sources)) { sources = o as Sources; } else if (obj == typeof(Turbulence)) { turbulence = o as Turbulence; } else if (obj == typeof(OutputSettings)) { outputSettings = o as OutputSettings; } else if (obj == typeof(Cloud)) { cloud = o as Cloud; } else if (obj == typeof(Background)) { background = o as Background; } else if (obj == typeof(SolarAdjust)) { solarAdjust = o as SolarAdjust; } else if (obj == typeof(BuildingSettings)) { buildingSettings = o as BuildingSettings; } else if (obj == typeof(IVS)) { ivs = o as IVS; } else if (obj == typeof(ParallelCPU)) { parallelCPU = o as ParallelCPU; } else if (obj == typeof(SOR)) { sor = o as SOR; } else if (obj == typeof(InflowAvg)) { inflowAvg = o as InflowAvg; } else if (obj == typeof(Facades)) { facades = o as Facades; } else if (obj == typeof(PlantSetting)) { plantSetting = o as PlantSetting; } else if (obj == typeof(LBC)) { lbc = o as LBC; } else if (obj == typeof(FullForcing)) { fullForcing = o as FullForcing; } } Simx simx = new Simx(_mainSettings) { SimpleForcing = simpleForcingSettings, TThread = tThread, TimeSteps = timeSteps, ModelTiming = modelTiming, SoilSettings = soilConfig, Sources = sources, Turbulence = turbulence, OutputSettings = outputSettings, Cloud = cloud, Background = background, SolarAdjust = solarAdjust, BuildingSettings = buildingSettings, IVS = ivs, ParallelCPU = parallelCPU, SOR = sor, InflowAvg = inflowAvg, Facades = facades, PlantSetting = plantSetting, LBC = lbc, FullForcing = fullForcing }; simx.WriteSimx(); DA.SetData(0, Path.Combine(Path.GetDirectoryName(_mainSettings.Inx), _mainSettings.Name + ".simx")); } catch { this.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Please provide a valid mainSettings."); } } }
public void TestSolvers() { //LU Factorization double[,] matrix4 = { { 1, 2, 4 }, { 3, 8, 14 }, { 2, 6, 13 } }; double[,] sym_posMatrix = { { 3, 4, 3 }, { 4, 8, 6 }, { 3, 6, 9 } }; double[] b = { 3, 13, 4 }; LUfactorization lufactorization = new LUfactorization(); double[,] L = lufactorization.LMatrix(matrix4); double[,] U = lufactorization.UMatrix(matrix4); double[] solution1 = lufactorization.SolveSystem(L, U, b); double[,] matrix3 = { { 1, 2, 4 }, { 3, 8, 14 }, { 2, 6, 13 } }; //SOR SOR sor = new SOR(); (double[] x, _, _) = sor.SORMethod(matrix3, b, 50, 10e-06, 1.25); //check double[] actualSolution1 = { 3, 4, -2 }; double tol = 10E-6; Assert.Equal(solution1, actualSolution1); for (int i = 0; i < x.Length; i++) { Debug.Assert(Math.Abs(x[i] - actualSolution1[i]) < tol); } ////Jacobi ////Jacobi jacobi = new Jacobi(); ////(double[] solution2, _, _) = jacobi.JacobiMethod(matrix4, b, 4, 1e-6); ////Cholesky Factorization //CholeskyFactorization choleskyFactorization = new CholeskyFactorization(); //var (matrixL, matrixLT) = choleskyFactorization.ComposeLLTMatrix(sym_posMatrix); //double[] solution3 = choleskyFactorization.SolveEquationSystem(matrixL, matrixLT, b); //// //GaussSiedel //// GaussSiedel gaussSiedel = new GaussSiedel(); //// (var solution4,_,_) = gaussSiedel.GaussSiedelMethod(matrix4, b, 30, 1e-6); //double[] actualSolution2 = { -3.5, 4.333, -1.2778 }; ////for (int i = 0; i < solution2.Length; i++) ////{ //// Debug.Assert(Math.Abs(solution2[i] - actualSolution1[i]) < tol); ////} //for (int i = 0; i < solution3.Length; i++) //{ // Debug.Assert(Math.Abs(solution3[i] - actualSolution2[i]) < tol); //} //// //for (int i = 0; i < solution3.Length; i++) //// //{ //// // Debug.Assert(Math.Abs(solution4[i] - actualSolution1[i]) < tol); //// //} }