Skip to content

Commit

Permalink
Moved solve_system to generic ProblemDAE class
Browse files Browse the repository at this point in the history
  • Loading branch information
lisawim committed Oct 31, 2023
1 parent c325087 commit 76efa6a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 212 deletions.
61 changes: 50 additions & 11 deletions pySDC/projects/DAE/misc/ProblemDAE.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,64 @@
import numpy as np
from scipy.optimize import root

from pySDC.core.Problem import ptype
from pySDC.core.Problem import ptype, WorkCounter
from pySDC.implementations.datatype_classes.mesh import mesh


class ptype_dae(ptype):
"""
Interface class for DAE problems. Ensures that all parameters are passed that are needed by DAE sweepers
r"""
This class implements a generic DAE class and illustrates the interface class for DAE problems.
It ensures that all parameters are passed that are needed by DAE sweepers.
Parameters
----------
nvars : int
Number of unknowns of the problem class.
newton_tol : float
Tolerance for the nonlinear solver.
Attributes
----------
work_counters : WorkCounter
Counts the work, here the number of function calls during the nonlinear solve is logged.
"""

dtype_u = mesh
dtype_f = mesh

def __init__(self, nvars, newton_tol):
"""
Initialization routine
Args:
problem_params (dict): custom parameters for the example
dtype_u: mesh data type (will be passed parent class)
dtype_f: mesh data type (will be passed parent class)
"""
"""Initialization routine"""
super().__init__((nvars, None, np.dtype('float64')))
self._makeAttributeAndRegister('nvars', 'newton_tol', localVars=locals(), readOnly=True)

self.work_counters['newton'] = WorkCounter()

def solve_system(self, impl_sys, u0, t):
r"""
Solver for nonlinear implicit system (defined in sweeper).
Parameters
----------
impl_sys : callable
Implicit system to be solved.
u0 : dtype_u
Initial guess for solver.
t : float
Current time :math:`t`.
Returns
-------
me : dtype_u
Numerical solution.
"""

me = self.dtype_u(self.init)
opt = root(
impl_sys,
u0,
method='hybr',
tol=self.newton_tol,
)
me[:] = opt.x
self.work_counters['newton'].niter += opt.nfev
return me
103 changes: 3 additions & 100 deletions pySDC/projects/DAE/problems/simple_DAE.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import warnings
import numpy as np
from scipy.interpolate import interp1d
from scipy.optimize import root

from pySDC.projects.DAE.misc.ProblemDAE import ptype_dae
from pySDC.core.Problem import WorkCounter
Expand Down Expand Up @@ -38,8 +37,7 @@ class pendulum_2d(ptype_dae):
----------
work_counters : WorkCounter
Counts the work, i.e., number of function calls of right-hand side is called and stored in
``work_counters['rhs']``, the number of function calls of the Newton-like solver is stored in
``work_counters['newton']``.
``work_counters['rhs']``.
References
----------
Expand All @@ -57,7 +55,6 @@ def __init__(self, nvars, newton_tol):
# solution = data[:, 1:]
# self.u_ref = interp1d(t, solution, kind='cubic', axis=0, fill_value='extrapolate')
self.t_end = 0.0
self.work_counters['newton'] = WorkCounter()
self.work_counters['rhs'] = WorkCounter()

def eval_f(self, u, du, t):
Expand Down Expand Up @@ -86,36 +83,6 @@ def eval_f(self, u, du, t):
self.work_counters['rhs']()
return f

def solve_system(self, impl_sys, u0, t):
r"""
Solver for nonlinear implicit system (defined in sweeper).
Parameters
----------
impl_sys : callable
Implicit system to be solved.
u0 : dtype_u
Initial guess for solver.
t : float
Current time :math:`t`.
Returns
-------
me : dtype_u
Numerical solution.
"""

me = self.dtype_u(self.init)
opt = root(
impl_sys,
u0,
method='hybr',
tol=self.newton_tol,
)
me[:] = opt.x
self.work_counters['newton'].niter += opt.nfev
return me

def u_exact(self, t):
"""
Approximation of the exact solution generated by spline interpolation of an extremely accurate numerical reference solution.
Expand Down Expand Up @@ -177,8 +144,7 @@ class simple_dae_1(ptype_dae):
----------
work_counters : WorkCounter
Counts the work, i.e., number of function calls of right-hand side is called and stored in
``work_counters['rhs']``, the number of function calls of the Newton-like solver is stored in
``work_counters['newton']``.
``work_counters['rhs']``.
References
----------
Expand All @@ -190,7 +156,6 @@ def __init__(self, nvars, newton_tol):
"""Initialization routine"""
super().__init__(nvars, newton_tol)

self.work_counters['newton'] = WorkCounter()
self.work_counters['rhs'] = WorkCounter()

def eval_f(self, u, du, t):
Expand Down Expand Up @@ -222,36 +187,6 @@ def eval_f(self, u, du, t):
self.work_counters['rhs']()
return f

def solve_system(self, impl_sys, u0, t):
r"""
Solver for nonlinear implicit system (defined in sweeper).
Parameters
----------
impl_sys : callable
Implicit system to be solved.
u0 : dtype_u
Initial guess for solver.
t : float
Current time :math:`t`.
Returns
-------
me : dtype_u
Numerical solution.
"""

me = self.dtype_u(self.init)
opt = root(
impl_sys,
u0,
method='hybr',
tol=self.newton_tol,
)
me[:] = opt.x
self.work_counters['newton'].niter += opt.nfev
return me

def u_exact(self, t):
"""
Routine for the exact solution.
Expand Down Expand Up @@ -297,8 +232,7 @@ class problematic_f(ptype_dae):
Specific parameter of the problem.
work_counters : WorkCounter
Counts the work, i.e., number of function calls of right-hand side is called and stored in
``work_counters['rhs']``, the number of function calls of the Newton-like solver is stored in
``work_counters['newton']``.
``work_counters['rhs']``
References
----------
Expand All @@ -312,7 +246,6 @@ def __init__(self, nvars, newton_tol, eta=1):
self._makeAttributeAndRegister('eta', localVars=locals())

self.work_counters['rhs'] = WorkCounter()
self.work_counters['newton'] = WorkCounter()

def eval_f(self, u, du, t):
r"""
Expand Down Expand Up @@ -340,36 +273,6 @@ def eval_f(self, u, du, t):
self.work_counters['rhs']()
return f

def solve_system(self, impl_sys, u0, t):
r"""
Solver for nonlinear implicit system (defined in sweeper).
Parameters
----------
impl_sys : callable
Implicit system to be solved.
u0 : dtype_u
Initial guess for solver.
t : float
Current time :math:`t`.
Returns
-------
me : dtype_u
Numerical solution.
"""

me = self.dtype_u(self.init)
opt = root(
impl_sys,
u0,
method='hybr',
tol=self.newton_tol,
)
me[:] = opt.x
self.work_counters['newton'].niter += opt.nfev
return me

def u_exact(self, t):
"""
Routine for the exact solution.
Expand Down
35 changes: 1 addition & 34 deletions pySDC/projects/DAE/problems/synchronous_machine.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import numpy as np
import warnings
from scipy.interpolate import interp1d
from scipy.optimize import root

from pySDC.projects.DAE.misc.ProblemDAE import ptype_dae
from pySDC.core.Problem import WorkCounter
Expand Down Expand Up @@ -147,8 +146,7 @@ class synchronous_machine_infinite_bus(ptype_dae):
Defines the mechanical torque applied to the rotor shaft.
work_counters : WorkCounter
Counts the work, i.e., number of function calls of right-hand side is called and stored in
``work_counters['rhs']``, the number of function calls of the Newton-like solver is stored in
``work_counters['newton']``.
``work_counters['rhs']``.
References
----------
Expand Down Expand Up @@ -191,7 +189,6 @@ def __init__(self, nvars, newton_tol):
self.T_m = 0.854

self.work_counters['rhs'] = WorkCounter()
self.work_counters['newton'] = WorkCounter()

def eval_f(self, u, du, t):
r"""
Expand Down Expand Up @@ -269,36 +266,6 @@ def eval_f(self, u, du, t):
self.work_counters['rhs']()
return f

def solve_system(self, impl_sys, u0, t):
r"""
Solver for nonlinear implicit system (defined in sweeper).
Parameters
----------
impl_sys : callable
Implicit system to be solved.
u0 : dtype_u
Initial guess for solver.
t : float
Current time :math:`t`.
Returns
-------
me : dtype_u
Numerical solution.
"""

me = self.dtype_u(self.init)
opt = root(
impl_sys,
u0,
method='hybr',
tol=self.newton_tol,
)
me[:] = opt.x
self.work_counters['newton'].niter += opt.nfev
return me

def u_exact(self, t):
"""
Approximation of the exact solution generated by spline interpolation of an extremely accurate numerical reference solution.
Expand Down
Loading

0 comments on commit 76efa6a

Please sign in to comment.