add new function to NewtonSolver (c++ & python)

Asked by Chaffra Affouda

I am trying to add the following function to NewtonSolver so that I can overload it in python. I also modified NewtonSolver::solve to call update_solution. Everthing builds.

void NewtonSolver::update_solution(GenericVector& x, GenericVector& dx)
{
 const double relaxation = parameters["relaxation_parameter"];
 if (std::abs(1.0 - relaxation) < DOLFIN_EPS)
  x -= dx;
 else
  x.axpy(-relaxation, dx);
}

However if I inherit from NewtonSolver in python then it seems that my overloaded "update_solution" is not being called by c++ NewtonSolver::solve. I used to have this working.

class DDSolver(NewtonSolver):

    def update_solution(self, x, dx):
        print 'updating solution'
        self.damp_potentials(x, dx)
        NewtonSolver.update_solution(self, x, dx)

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
Jan Blechta (blechta) said :
#1

Try using compile_module() to embedd DDSolver class implemented in C++.

Revision history for this message
Jan Blechta (blechta) said :
#2

Sorry, that would be compile_extension_module()

Revision history for this message
Chaffra Affouda (chaffra) said :
#3

I don't understand. My DDSolver class is implemented in python and I should be able to inherit from NewtonSolver in python, no?

Revision history for this message
Jan Blechta (blechta) said :
#4

On Tue, 19 Mar 2013 21:46:00 -0000
Chaffra Affouda <email address hidden> wrote:
> Question #224664 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/224664
>
> Status: Answered => Open
>
> Chaffra Affouda is still having a problem:
> I don't understand. My DDSolver class is implemented in python and I
> should be able to inherit from NewtonSolver in python, no?
>

Oh, I didn't read carefully before:) Try looking at classes which are
know that python overloads them (Expression, Subdomain). I guess that
they have to be reimplemented in python and/or some SWIG typemaps must
be introduced. Probably Johan or Garth would help you.

But consider that you can overload C++ class from python by
compile_extension_module() without dealing with these issues. Actually
you would be doing that by C++ code but it should work.

Jan

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

On 03/19/2013 11:16 PM, Jan Blechta wrote:
> Question #224664 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/224664
>
> Status: Open => Answered
>
> Jan Blechta proposed the following answer:
> On Tue, 19 Mar 2013 21:46:00 -0000
> Chaffra Affouda <email address hidden> wrote:
>> Question #224664 on DOLFIN changed:
>> https://answers.launchpad.net/dolfin/+question/224664
>>
>> Status: Answered => Open
>>
>> Chaffra Affouda is still having a problem:
>> I don't understand. My DDSolver class is implemented in python and I
>> should be able to inherit from NewtonSolver in python, no?
>>
>
> Oh, I didn't read carefully before:) Try looking at classes which are
> know that python overloads them (Expression, Subdomain). I guess that
> they have to be reimplemented in python and/or some SWIG typemaps must
> be introduced. Probably Johan or Garth would help you.

For SWIG to be able to pass a Python subclassed class to C++ it needs to
generate a C++ director class, which eventually wraps the Python class.
To tell SWIG to do this you put:

  %feature("director") dolfin::NewtonSolver;

into

  dolfin/swig/nls/pre.i

You should also make update_solution a virtual method.

> But consider that you can overload C++ class from python by
> compile_extension_module() without dealing with these issues. Actually
> you would be doing that by C++ code but it should work.

You can also perfectly fine subclass NewtonSolver by constructing your
C++ class yourself and then JIT compile it using
compile_extension_module as Jan says. Have a look at the doc string of
that function.

Johan

> Jan
>

Revision history for this message
Chaffra Affouda (chaffra) said :
#6

Awsome, guys,

that's what I was missing! Also thanks for the info with compile_extension_module. That will come in handy when I rewrite some of my code to C++.

Revision history for this message
Chaffra Affouda (chaffra) said :
#7

Thanks Johan Hake, that solved my question.