[petsc-users] Requesting help with SNES non-linear example

Jose E. Roman jroman at dsic.upv.es
Sun Oct 27 14:03:11 CDT 2024


bratu2dnpy.py does not "return f", instead it modifies the argument f by manipulating its internal array.

If you want to use a function that "return f" you have to receive this returned variable and then set it to the Vec object, i.e.:

        f = self.compute(expected_soln_vec_npy, x, f)
        F.setArray(f)

Jose


> El 26 oct 2024, a las 12:39, Vaishak Prasad <vaishakprasad at gmail.com> escribió:
> 
> Dear Jose,
> 
> Thankyou very much for your response. 
> 
> On Sat, Oct 26, 2024 at 3:32 PM Jose E. Roman <jroman at dsic.upv.es> wrote:
> Your residue() function is computing something (f) that is not returned to the calling function. So evalFunction() returns a zero residual and the solver stops in one iteration.
> Apologies, I am a little confused. The example bratu2d.py imports the residue function from bratu2dnpy.py and it also does not return anything (and it works). I was under the impression that
> a pointer to `f` is passed to petsc, that overwrites it at each iteration. Please correct me if I am wrong.
> 
> I also tried returning `f` in my residue function but it does not change anything. Is this what you suggested?
>    Mixing numpy arrays and petsc4py vectors is going to be tricky unless you know what you are doing. It is simpler to manipulate petsc4py vectors directly, see for instance bratu3d.py or other examples.
> I have also tried this. I created a petsc vector for the input expected solution and then converted it into a numpy array using the `getArray` method. But noting changes.
> 
> I also printed some information about the convergence itself. I find 
> ConvergedReason is 2  (absolute tolerance)
> getTolerances is [1e-8, 1e-50, 1e-8, 50]  (default)
> getErrorIfNotConverged is False
> getIterationNumber is 0
> 
> In contrast, the convergence reason for the bratu2d example was 3 (relative tolerance).
> 
> I also tried increasing the `max_it` but to no avail.
> 
> I appreciate your valuable suggestion and advice in this regard.
> 
> Thanks and Regards
> 
> Jose
> 
> 
> > El 26 oct 2024, a las 11:11, Vaishak Prasad <vaishakprasad at gmail.com> escribió:
> > 
> > Hi,
> > 
> > Thanks for pointing me to the updated example. The example itself works fine.
> > 
> > However, on adapting the example to a simple problem ( recovering a given 1D vector), I find that there is only one function call happening and the solution has not converged. I am attaching the modified example here. Is there something silly I am doing here?
> > 
> > Any help would be appreciated.
> > 
> > Thanks and Regards
> > 
> > 
> > On Sat, Oct 26, 2024 at 12:42 PM Jose E. Roman <jroman at dsic.upv.es> wrote:
> > The examples in those old slides might be outdated. Try with the examples that come with the PETSc repo or tarball. In particular, that example can be found in src/binding/petsc4py/demo/legacy/bratu2d/bratu2d.py.
> > 
> > Jose
> > 
> > > El 25 oct 2024, a las 19:58, Vaishak Prasad <vaishakprasad at gmail.com> escribió:
> > > 
> > > Dear All,
> > > Greetings from India.
> > > 
> > > I am new to petsc4py. I was trying out the example on slides 18, and 19 at
> > > 
> > > https://urldefense.us/v3/__https://www.bu.edu/pasi/files/2011/01/Lisandro-Dalcin-petsc4py.pdf__;!!G_uCfscf7eWS!ZnQ15XH0wdJTgAhkpejZ0TFFnvrfoV8_YNfEnMmNV9HbN5jMiA0BcE2ru-OzhnPQxY5rps05o84vIoA9rvUd4pK3$ 
> > > 
> > > However, I get a zero vector for the solution `x`.
> > > 
> > > I am using petsc4py 3.21.3 on Linux.
> > > 
> > > Any help would be appreciated.
> > > 
> > > With regards
> > > 
> > > -- 
> > > Vaishak Prasad
> > > Post Doctoral Fellow
> > > Astrophysical Relativity Group
> > > International Center for Theoretical Sciences
> > > Tata Institute of Fundamental Research
> > > Survey No. 151, Shivakote, Hesaraghatta Hobli,
> > > Bengaluru, Karnataka, India, 
> > > Earth, Solar system, 
> > > Milky Way Galaxy, The Local Group
> > 
> > 
> > 
> > -- 
> > Vaishak Prasad
> > Post Doctoral Fellow
> > Astrophysical Relativity Group
> > International Center for Theoretical Sciences
> > Tata Institute of Fundamental Research
> > Survey No. 151, Shivakote, Hesaraghatta Hobli,
> > Bengaluru, Karnataka, India, 
> > Earth, Solar system, 
> > Milky Way Galaxy, The Local Group
> > <mwe.py>
> 
> 
> 
> -- 
> Vaishak Prasad
> Post Doctoral Fellow
> Astrophysical Relativity Group
> International Center for Theoretical Sciences
> Tata Institute of Fundamental Research
> Survey No. 151, Shivakote, Hesaraghatta Hobli,
> Bengaluru, Karnataka, India, 
> Earth, Solar system, 
> Milky Way Galaxy, The Local Group<mwe.py>




More information about the petsc-users mailing list