<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
span.apple-converted-space
{mso-style-name:apple-converted-space;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;
mso-ligatures:none;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=DE link=blue vlink=purple style='word-wrap:break-word'><div class=WordSection1><p class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'>Thanks fort he clarification. And it makes sense that x is read-only. I solved my issue by actually creating a new, empty vector which is used in snes.solve and can update my FEniCS vector from this, see: <a href="https://github.com/sblauth/cashocs/pull/533">https://github.com/sblauth/cashocs/pull/533</a><o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'>What I did not get at first is that PETSc.VEC.setArray seems to create a shallow copy, whereas PETSc.VEC.aypx(0.0, …) seems to create something like a deep copy. Using aypx this is not an issue anymore.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'>Best,<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'>Sebastian<o:p></o:p></span></p><p class=MsoNormal><span style='mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal><span style='mso-fareast-language:EN-US'><o:p> </o:p></span></p><div><p class=MsoNormal>--<o:p></o:p></p><p class=MsoNormal>Dr. Sebastian Blauth<o:p></o:p></p><p class=MsoNormal>Fraunhofer-Institut für<o:p></o:p></p><p class=MsoNormal>Techno- und Wirtschaftsmathematik ITWM<o:p></o:p></p><p class=MsoNormal>Abteilung Transportvorgänge<o:p></o:p></p><p class=MsoNormal>Fraunhofer-Platz 1, 67663 Kaiserslautern<o:p></o:p></p><p class=MsoNormal>Telefon: +49 631 31600-4968<o:p></o:p></p><p class=MsoNormal><a href="mailto:sebastian.blauth@itwm.fraunhofer.de">sebastian.blauth@itwm.fraunhofer.de</a><o:p></o:p></p><p class=MsoNormal><a href="https://www.itwm.fraunhofer.de">https://www.itwm.fraunhofer.de</a><o:p></o:p></p></div><p class=MsoNormal><span style='mso-fareast-language:EN-US'><o:p> </o:p></span></p><div style='border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt'><div><div style='border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm'><p class=MsoNormal><b><span lang=EN-US>From:</span></b><span lang=EN-US> Barry Smith <bsmith@petsc.dev> <br><b>Sent:</b> Tuesday, December 10, 2024 4:32 PM<br><b>To:</b> Blauth, Sebastian <sebastian.blauth@itwm.fraunhofer.de><br><b>Cc:</b> petsc-users@mcs.anl.gov<br><b>Subject:</b> Re: [petsc-users] Issue with SNES with petsc4py 3.22<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p> </o:p></p><div><p class=MsoNormal><o:p> </o:p></p></div><p class=MsoNormal> The callbacks for SNES, such as SNESSetFunction(), are not allowed to change the value of the solution x. The updates to the solution are controlled by the nonlinear solver being used by SNES; for example, with Newton's method with line search, the solution is updated by the SNES Newton solver after a successful linear search is computed. <o:p></o:p></p><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal> Could you please fully explain the context in which you wish to change the value of x during the SNES solve directly and what callback the issue is occurring in?<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal> Barry<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p><div><p class=MsoNormal><br><br><o:p></o:p></p><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><div><p class=MsoNormal>On Dec 10, 2024, at 3:27 AM, Blauth, Sebastian <<a href="mailto:sebastian.blauth@itwm.fraunhofer.de">sebastian.blauth@itwm.fraunhofer.de</a>> wrote:<o:p></o:p></p></div><p class=MsoNormal><o:p> </o:p></p><div><div><p class=MsoNormal>Hi everyone,<o:p></o:p></p></div><div><p class=MsoNormal> <o:p></o:p></p></div><div><p class=MsoNormal><span lang=EN-US>I am having trouble with using SNES from petsc4py in PETSc 3.22. The problem is similar to<span class=apple-converted-space> </span><a href="https://gitlab.com/petsc/petsc/-/issues/1611"><span style='color:#0563C1'>https://gitlab.com/petsc/petsc/-/issues/1611</span></a><span class=apple-converted-space> </span>and related to the readonly access of “x”. Particularly, in my application, based on FEniCS, I want to solve a nonlinear PDE. Therefore, in my callbacks, I want to set the vector of DoFs of my FEniCS function to “x” with a line like</span><o:p></o:p></p></div><div><p class=MsoNormal><span lang=EN-US> </span><o:p></o:p></p></div><div><p class=MsoNormal><span lang=EN-US>self.u.vector().vec().setArray(x)</span><o:p></o:p></p></div><div><p class=MsoNormal><span lang=EN-US> </span><o:p></o:p></p></div><div><p class=MsoNormal><span lang=EN-US>However, this now raises the error</span><o:p></o:p></p></div><div><p class=MsoNormal><span lang=EN-US> </span><o:p></o:p></p></div><div><p class=MsoNormal><span lang=EN-US>[0] Vector 'Vec_0x84000003_0' (argument #1) was locked for read-only access in unknown_function() at unknown file:0 (line numbers only accurate to function begin)</span><o:p></o:p></p></div><div><p class=MsoNormal><span lang=EN-US> </span><o:p></o:p></p></div><div><p class=MsoNormal><span lang=EN-US>I believe that this is a consequence of<span class=apple-converted-space> </span><a href="https://gitlab.com/petsc/petsc/-/merge_requests/7567#22c226fc29b6fc9dc5781b0aff4dcc70ac6c3489"><span style='color:#0563C1'>https://gitlab.com/petsc/petsc/-/merge_requests/7567#22c226fc29b6fc9dc5781b0aff4dcc70ac6c3489</span></a>, but I did not find any information on how to remove the lock from the vector or how I can set use “x” to update the vector of my function. Using</span><o:p></o:p></p></div><div><p class=MsoNormal><span lang=EN-US> </span><o:p></o:p></p></div><div><p class=MsoNormal><span lang=EN-US>xx = x.getArray(readonly=True)</span><o:p></o:p></p></div><div><p class=MsoNormal><span lang=EN-US> </span><o:p></o:p></p></div><div><p class=MsoNormal><span lang=EN-US>as described in<span class=apple-converted-space> </span><a href="https://gitlab.com/petsc/petsc/-/issues/1611"><span style='color:#0563C1'>https://gitlab.com/petsc/petsc/-/issues/1611</span></a><span class=apple-converted-space> </span>does not work either, as calling self.u.vector().vec().setArray(xx) then fails with the same error message.</span><o:p></o:p></p></div><div><p class=MsoNormal><span lang=EN-US> </span><o:p></o:p></p></div><div><p class=MsoNormal><span lang=EN-US>Does anyone have an idea how to fix this?<span class=apple-converted-space> </span></span><o:p></o:p></p></div><div><p class=MsoNormal><span lang=EN-US>Thanks a lot in advance,</span><o:p></o:p></p></div><div><p class=MsoNormal><span lang=EN-US>Sebastian</span><o:p></o:p></p></div><div><p class=MsoNormal><span lang=EN-US> </span><o:p></o:p></p></div><div><p class=MsoNormal><span lang=EN-US> </span><o:p></o:p></p></div><div><p class=MsoNormal><span lang=EN-US> </span><o:p></o:p></p></div><div><p class=MsoNormal>--<o:p></o:p></p></div><div><p class=MsoNormal>Dr. Sebastian Blauth<o:p></o:p></p></div><div><p class=MsoNormal>Fraunhofer-Institut für<o:p></o:p></p></div><div><p class=MsoNormal>Techno- und Wirtschaftsmathematik ITWM<o:p></o:p></p></div><div><p class=MsoNormal>Abteilung Transportvorgänge<o:p></o:p></p></div><div><p class=MsoNormal>Fraunhofer-Platz 1, 67663 Kaiserslautern<o:p></o:p></p></div><div><p class=MsoNormal>Telefon: +49 631 31600-4968<o:p></o:p></p></div><div><p class=MsoNormal><a href="mailto:sebastian.blauth@itwm.fraunhofer.de">sebastian.blauth@itwm.fraunhofer.de</a><o:p></o:p></p></div><div><p class=MsoNormal><a href="https://www.itwm.fraunhofer.de/">https://www.itwm.fraunhofer.de</a><o:p></o:p></p></div></div></blockquote></div><p class=MsoNormal><o:p> </o:p></p></div></div></div></body></html>