Solving equations with self-defined linear_solver

Asked by Gerd Wachsmuth

Is it possible to solve an equation (a==L or F==0) by a self-defined linear solver?

Assume we have

Equation eq = (a==L);
LinearSolver *solver = ...;

Is there an easy (e.g. high-level) possibility to solve "eq" with "solver"? The same question applies for nonlinear equations and NewtonSolver.

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Johan Hake
Solved:
Last query:
Last reply:
Revision history for this message
Johan Hake (johan-hake) said :
#1

Sure, you just need to assemble the linear algebra objects, using
assemble. Then you pass the Matrix, A from the bilinear form and the
Vector, b from the linear form, to you linear solver.

johan

On 05/04/2012 02:45 PM, Gerd Wachsmuth wrote:
> New question #195998 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/195998
>
> Is it possible to solve an equation (a==L or F==0) by a self-defined linear solver?
>
> Assume we have
>
> Equation eq = (a==L);
> LinearSolver *solver = ...;
>
> Is there an easy (e.g. high-level) possibility to solve "eq" with "solver"? The same question applies for nonlinear equations and NewtonSolver.
>

Revision history for this message
Gerd Wachsmuth (gerd-wachsmuth) said :
#2

On 05/05/2012 03:40 PM, Johan Hake wrote:
> Sure, you just need to assemble the linear algebra objects, using
> assemble. Then you pass the Matrix, A from the bilinear form and the
> Vector, b from the linear form, to you linear solver.

Sure, but this is a rather low-level implementation. I was thinking of a
_high-level_ method like solve( a==L, u, solver ). Maybe a constructor
like LinearVariationalSolver(problem, lin_solver) would be helpful.

But things are more difficult for nonlinear problems:
One can define a custom NewtonSolver by
      NewtonSolver(lin_solver, la_factory),
but this is more or less useless, since it cannot be passed to
NonlinearVariationalSolver. Instead, one has to _copy_ the class
NonlinearDiscreteProblem (because it is a private subclass). Here I
would propose to make NonlinearDiscreteProblem a sovereign class (such
that one could customizing it by inherited classes).

Revision history for this message
Johan Hake (johan-hake) said :
#3

You can subclass NonlinearProblem, which NonlinearDiscreteProblem
subclasses, and pass that to a NewtonSolver. Is you point that the
present implementation of J and F in the NonlinearDiscreteProblem is so
general that it would be usefull to expose that class, so one can
subclass it and reuse J and F in one way or the other, within the
NonlinearVariationalSolver?

Johan

On 05/07/2012 08:35 AM, Gerd Wachsmuth wrote:
> Question #195998 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/195998
>
> Status: Answered => Open
>
> Gerd Wachsmuth is still having a problem:
> On 05/05/2012 03:40 PM, Johan Hake wrote:
>> Sure, you just need to assemble the linear algebra objects, using
>> assemble. Then you pass the Matrix, A from the bilinear form and the
>> Vector, b from the linear form, to you linear solver.
>
> Sure, but this is a rather low-level implementation. I was thinking of a
> _high-level_ method like solve( a==L, u, solver ). Maybe a constructor
> like LinearVariationalSolver(problem, lin_solver) would be helpful.
>
>
> But things are more difficult for nonlinear problems:
> One can define a custom NewtonSolver by
> NewtonSolver(lin_solver, la_factory),
> but this is more or less useless, since it cannot be passed to
> NonlinearVariationalSolver. Instead, one has to _copy_ the class
> NonlinearDiscreteProblem (because it is a private subclass). Here I
> would propose to make NonlinearDiscreteProblem a sovereign class (such
> that one could customizing it by inherited classes).
>

Revision history for this message
Gerd Wachsmuth (gerd-wachsmuth) said :
#4

On 05/07/2012 09:05 AM, Johan Hake wrote:
> You can subclass NonlinearProblem, which NonlinearDiscreteProblem
> subclasses, and pass that to a NewtonSolver.
Do you mean something like

class MyNonlinearSolver : public Nonlinear Solver
{
 ...
 class MyNonlinearDiscreteProblem : public NonlinearDiscreteProblem
 {};
};

? Is it possible to inherit from a private subclass?

> Is you point that the
> present implementation of J and F in the NonlinearDiscreteProblem is so
> general that it would be usefull to expose that class, so one can
> subclass it and reuse J and F in one way or the other, within the
> NonlinearVariationalSolver?
I think it would be convenient in some situations. E.g. if you would
like to add "A.ident_zeros()" to F. Moreover, I have a situation, where
I have two possible initial guesses for the nonlinear problem - there it
would be convenient to have access to F in order to identify the best
initial guess (in terms of smallest initial residual).

However, for my problem concerning custom linear solvers it would also
be sufficient to add a new constructor
NonlinearVariationalSolver(problem, newton_solver).

Revision history for this message
Best Johan Hake (johan-hake) said :
#5

On 05/07/2012 09:35 AM, Gerd Wachsmuth wrote:
> Question #195998 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/195998
>
> Status: Answered => Open
>
> Gerd Wachsmuth is still having a problem:
> On 05/07/2012 09:05 AM, Johan Hake wrote:
>> You can subclass NonlinearProblem, which NonlinearDiscreteProblem
>> subclasses, and pass that to a NewtonSolver.
> Do you mean something like
>
> class MyNonlinearSolver : public Nonlinear Solver
> {
> ...
> class MyNonlinearDiscreteProblem : public NonlinearDiscreteProblem
> {};
> };
>
> ? Is it possible to inherit from a private subclass?

No, have alook at the CahnHilliard demo in DOLFIN:

   http://bit.ly/IxH9Co

>> Is you point that the
>> present implementation of J and F in the NonlinearDiscreteProblem is so
>> general that it would be usefull to expose that class, so one can
>> subclass it and reuse J and F in one way or the other, within the
>> NonlinearVariationalSolver?
> I think it would be convenient in some situations. E.g. if you would
> like to add "A.ident_zeros()" to F. Moreover, I have a situation, where
> I have two possible initial guesses for the nonlinear problem - there it
> would be convenient to have access to F in order to identify the best
> initial guess (in terms of smallest initial residual).

This can be done by subclassing NonlinearProblem, see above.

> However, for my problem concerning custom linear solvers it would also
> be sufficient to add a new constructor
> NonlinearVariationalSolver(problem, newton_solver).

I am confident you can solve this by using NonlinearProblem directly
with the NewtonSolver. You might argue that this is too low level, but
the NonlinearVariation{Solver,Problem} classes are there for easy
prototyping. For more demanding problems you need to code these
structures your self. However, there might be some clever way to make
these classes more customizable, but that will add complexity, which
might not be needed at the end of the day.

Johan

Revision history for this message
Gerd Wachsmuth (gerd-wachsmuth) said :
#6

Thanks Johan Hake, that solved my question.