forked from amaggiulli/QLNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Cranck-Nicholson, Method of Lines & TrBDF2 schemes (amaggiulli#244)
* Update step method & add new schemes - Update step method by adding theta parameter. - Add Cranck-Nicholson, Method of Lines & TrBDF2 schemes * Add new schemes to FdmBackwardSolver * Update IMixedScheme interface and dependencies * Update QLNet Old project file * Delete FdmBackwardSolver.cs * Move to Solvers folder
- Loading branch information
tournierjc
authored
Jan 29, 2020
1 parent
3b313ec
commit 46075c8
Showing
14 changed files
with
399 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/QLNet/Methods/Finitedifferences/Schemes/CrankNicolsonScheme.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
Copyright (C) 2020 Jean-Camille Tournier ([email protected]) | ||
This file is part of QLNet Project https://github.com/amaggiulli/qlnet | ||
QLNet is free software: you can redistribute it and/or modify it | ||
under the terms of the QLNet license. You should have received a | ||
copy of the license along with this program; if not, license is | ||
available online at <http://qlnet.sourceforge.net/License.html>. | ||
QLNet is a based on QuantLib, a free-software/open-source library | ||
for financial quantitative analysts and developers - http://quantlib.org/ | ||
The QuantLib license is available online at http://quantlib.org/license.shtml. | ||
This program is distributed in the hope that it will be useful, but WITHOUT | ||
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
FOR A PARTICULAR PURPOSE. See the license for more details. | ||
*/ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace QLNet | ||
{ | ||
/*! In one dimension the Crank-Nicolson scheme is equivalent to the | ||
Douglas scheme and in higher dimensions it is usually inferior to | ||
operator splitting methods like Craig-Sneyd or Hundsdorfer-Verwer. | ||
*/ | ||
public class CrankNicolsonScheme : IMixedScheme, ISchemeFactory | ||
{ | ||
public CrankNicolsonScheme() | ||
{ } | ||
|
||
public CrankNicolsonScheme(double theta, | ||
FdmLinearOpComposite map, | ||
List<BoundaryCondition<FdmLinearOp>> bcSet = null, | ||
double relTol = 1E-8, | ||
ImplicitEulerScheme.SolverType solverType = ImplicitEulerScheme.SolverType.BiCGstab) | ||
{ | ||
dt_ = null; | ||
theta_ = theta; | ||
explicit_ = new ExplicitEulerScheme(map, bcSet); | ||
implicit_ = new ImplicitEulerScheme(map, bcSet, relTol, solverType); | ||
} | ||
|
||
#region ISchemeFactory | ||
|
||
public IMixedScheme factory(object L, object bcs, object[] additionalInputs = null) | ||
{ | ||
double? theta = additionalInputs[0] as double?; | ||
double? relTol = additionalInputs[1] as double?; | ||
ImplicitEulerScheme.SolverType? solverType = additionalInputs[2] as ImplicitEulerScheme.SolverType?; | ||
return new CrankNicolsonScheme(theta.Value, L as FdmLinearOpComposite, | ||
bcs as List<BoundaryCondition<FdmLinearOp>>, relTol.Value, solverType.Value); | ||
} | ||
|
||
#endregion | ||
|
||
public void step(ref object a, double t, double theta = 1.0) | ||
{ | ||
Utils.QL_REQUIRE(t - dt_ > -1e-8, () => "a step towards negative time given"); | ||
if (theta_ != 1.0) | ||
explicit_.step(ref a, t, 1.0 - theta_); | ||
|
||
if (theta_ != 0.0) | ||
implicit_.step(ref a, t, theta_); | ||
} | ||
|
||
public void setStep(double dt) | ||
{ | ||
dt_ = dt; | ||
explicit_.setStep(dt_.Value); | ||
implicit_.setStep(dt_.Value); | ||
} | ||
|
||
public int numberOfIterations() | ||
{ | ||
return implicit_.numberOfIterations(); | ||
} | ||
protected double? dt_; | ||
protected double theta_; | ||
protected ExplicitEulerScheme explicit_; | ||
protected ImplicitEulerScheme implicit_; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/QLNet/Methods/Finitedifferences/Schemes/MethodOfLinesScheme.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
Copyright (C) 2020 Jean-Camille Tournier ([email protected]) | ||
This file is part of QLNet Project https://github.com/amaggiulli/qlnet | ||
QLNet is free software: you can redistribute it and/or modify it | ||
under the terms of the QLNet license. You should have received a | ||
copy of the license along with this program; if not, license is | ||
available online at <http://qlnet.sourceforge.net/License.html>. | ||
QLNet is a based on QuantLib, a free-software/open-source library | ||
for financial quantitative analysts and developers - http://quantlib.org/ | ||
The QuantLib license is available online at http://quantlib.org/license.shtml. | ||
This program is distributed in the hope that it will be useful, but WITHOUT | ||
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
FOR A PARTICULAR PURPOSE. See the license for more details. | ||
*/ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace QLNet | ||
{ | ||
/*! In one dimension the Crank-Nicolson scheme is equivalent to the | ||
Douglas scheme and in higher dimensions it is usually inferior to | ||
operator splitting methods like Craig-Sneyd or Hundsdorfer-Verwer. | ||
*/ | ||
public class MethodOfLinesScheme : IMixedScheme, ISchemeFactory | ||
{ | ||
public MethodOfLinesScheme() | ||
{ } | ||
|
||
public MethodOfLinesScheme(double eps, | ||
double relInitStepSize, | ||
FdmLinearOpComposite map, | ||
List<BoundaryCondition<FdmLinearOp>> bcSet = null) | ||
{ | ||
dt_ = null; | ||
eps_ = eps; | ||
relInitStepSize_ = relInitStepSize; | ||
map_ = map; | ||
bcSet_ = new BoundaryConditionSchemeHelper(bcSet); | ||
} | ||
|
||
#region ISchemeFactory | ||
|
||
public IMixedScheme factory(object L, object bcs, object[] additionalInputs = null) | ||
{ | ||
double? eps = additionalInputs[0] as double?; | ||
double? relInitStepSize = additionalInputs[1] as double?; | ||
return new MethodOfLinesScheme(eps.Value, relInitStepSize.Value, | ||
L as FdmLinearOpComposite, bcs as List<BoundaryCondition<FdmLinearOp>>); | ||
} | ||
|
||
#endregion | ||
|
||
protected List<double> apply(double t, List<double> r) | ||
{ | ||
map_.setTime(t, t + 0.0001); | ||
bcSet_.applyBeforeApplying(map_); | ||
|
||
Vector dxdt = -1.0 * map_.apply(new Vector(r)); | ||
|
||
return dxdt; | ||
} | ||
|
||
public void step(ref object a, double t, double theta = 1.0) | ||
{ | ||
Utils.QL_REQUIRE(t - dt_ > -1e-8, () => "a step towards negative time given"); | ||
List<double> v = new AdaptiveRungeKutta(eps_, relInitStepSize_ * dt_.Value).value(this.apply, a as Vector, t, Math.Max(0.0, t - dt_.Value)); | ||
Vector y = new Vector(v); | ||
bcSet_.applyAfterSolving(y); | ||
a = y; | ||
} | ||
|
||
public void setStep(double dt) | ||
{ | ||
dt_ = dt; | ||
} | ||
|
||
protected double? dt_; | ||
protected double eps_, relInitStepSize_; | ||
protected FdmLinearOpComposite map_; | ||
protected BoundaryConditionSchemeHelper bcSet_; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.