This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

How to calculate the dot product of two vectors with fenics (in parallel solver) without invoking numpy?

+1 vote

I can do it using the following numpy arrays:

v1 = Function(V)
v2 = Function(V)
a1 = 1.0 - numpy.dot(v1.vector().array(), v2.vector().array())

The above code works in series. However, when I run it in parallel solver (mpirun -np 2), it does not work as expected. That is, it produces two values rather than a single one.

The problem is: How can I calculate the dot product of two vectors with fenics (in parallel solver) without invoking numpy?

Related question: https://fenicsproject.org/qa/10976/mpirun-wrong-results?show=10976#q10976

Thank you!

jywu

Thanks a lot!

asked Apr 8, 2017 by jywu FEniCS Novice (510 points)
edited Apr 8, 2017 by jywu

If you're using lots of processes use VecDot. If you're not using many processes you could look into PETScVector.gather() and do the operation locally on each process.

Thanks, nate!

Would please give more details?

1 Answer

+1 vote
 
Best answer

Try something like this:

a1 = v1.vector().inner(v2.vector())
answered Apr 8, 2017 by hernan_mella FEniCS Expert (19,460 points)
selected Apr 8, 2017 by jywu

Mella, thank you for your targeting answer! It is really what I need.

Thanks again.

...