<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div class="elementToProof" style="font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Dear all,</div>
<div class="elementToProof" style="font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
I am struggling to understand how I should set up a SNES solver where the Newton Raphson iterations can be computed using a matrix-free method.</div>
<div class="elementToProof" style="font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
This is what I would like to do :</div>
<div class="elementToProof" style="font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<ul style="margin-top: 0px; margin-bottom: 0px;" data-editing-info="{"applyListStyleFromLevel":false,"unorderedStyleType":2}">
<li style="font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); margin-top: 0px; margin-bottom: 0px; list-style-type: "- ";">
<div class="elementToProof" role="presentation">I want to solve F(x) = 0</div>
</li><li style="font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); margin-top: 0px; margin-bottom: 0px; list-style-type: "- ";">
<div class="elementToProof" role="presentation">The Newton Raphson iteration J(x) dx = -F(x) should be computed using a matrix-free method</div>
</li><li style="font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); margin-top: 0px; margin-bottom: 0px; list-style-type: "- ";">
<div class="elementToProof" role="presentation">Then classic Newton Raphson update x = x + dx</div>
</li></ul>
<div style="margin-top: 0px; margin-bottom: 0px; font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="margin-top: 0px; margin-bottom: 0px; font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
The first question I would have is whether this can be done using SNES ?</div>
<div style="margin-top: 0px; margin-bottom: 0px; font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
If not, I can always this done using a classic matrix-free product and implementing the Newton Raphson iterations myself.</div>
<div style="margin-top: 0px; margin-bottom: 0px; font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="margin-top: 0px; margin-bottom: 0px; font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
What I have tried, without success is the following :</div>
<div style="margin-top: 0px; margin-bottom: 0px; font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="margin-top: 0px; margin-bottom: 0px; font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
 SNESCreate(PETSC_COMM_WORLD, &snes);<br>
SNESSetApplicationContext(snes, (void *)(&userCtx));</div>
<div style="margin-top: 0px; margin-bottom: 0px; font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt;" class="elementToProof">
<span style="color: rgb(0, 0, 0);"> SNESSetFunction(snes, r, FormFunction1, (void *)&</span><span style="color: rgb(0, 0, 0);">userCtx</span><span style="color: rgb(0, 0, 0);">);</span></div>
<div class="elementToProof" style="font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
MatCreateSNESMF(snes, &J);</div>
<div class="elementToProof" style="font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
MatShellSetOperation(J, MATOP_MULT, (void(*)(void))mult);</div>
<div class="elementToProof" style="font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
MatSetFromOptions(J);</div>
<div style="font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
MatSetUp(J);</div>
<div style="font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt;">
<span style="color: rgb(0, 0, 0);"> SNESSetJacobian(snes, J, J, MFJacobianVectorProduct, (void *)&</span><span style="color: rgb(0, 0, 0);">userCtx</span><span style="color: rgb(0, 0, 0);">);</span></div>
<div style="font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
 SNESSetFromOptions(snes);<br>
SNESSetUseMatrixFree(snes, PETSC_FALSE, PETSC_TRUE);</div>
<div class="elementToProof" style="font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
...</div>
<div class="elementToProof" style="font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<ul style="margin-top: 0px; margin-bottom: 0px;" data-editing-info="{"applyListStyleFromLevel":false,"unorderedStyleType":2}">
<li style="font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); margin-top: 0px; margin-bottom: 0px; list-style-type: "- ";">
<div style="margin-top: 0px; margin-bottom: 0px;" class="elementToProof" role="presentation">
The function 'mult' implements the matrix-free multiplication, say A y = b</div>
</li><ul style="margin-top: 0px; margin-bottom: 0px; list-style-type: circle;" data-editing-info="{"applyListStyleFromLevel":true}">
<li style="font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); margin-top: 0px; margin-bottom: 0px;">
<div style="margin-top: 0px; margin-bottom: 0px;" class="elementToProof" role="presentation">
But how do I get the point at which A is computed ? Since A is supposed to be the Jacobian it is something like A(x)</div>
</li><li style="font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); margin-top: 0px; margin-bottom: 0px;">
<div style="margin-top: 0px; margin-bottom: 0px;" role="presentation">To try to do that, I added a method '<span style="color: rgb(0, 0, 0);">MFJacobianVectorProduct</span>' where I store in the SNES Application Context the point 'x' at which the jacobian should
be computed, then in the 'mult' function I try to get the context of 'J' and use it to compute the matrix-free product</div>
</li></ul>
</ul>
<div style="margin-top: 0px; margin-bottom: 0px; font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="margin-top: 0px; margin-bottom: 0px; font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
I am probably misunderstanding something since the code runs, but the Jacobian is simply computed using finite differences and my "matrix-free" Jacobian product implementation is not called.</div>
<div style="margin-top: 0px; margin-bottom: 0px; font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="margin-top: 0px; margin-bottom: 0px; font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
If you have an example, I would be happy to look into it.</div>
<div style="margin-top: 0px; margin-bottom: 0px; font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="margin-top: 0px; margin-bottom: 0px; font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Thank you in advance,</div>
<div style="margin-top: 0px; margin-bottom: 0px; font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="margin-top: 0px; margin-bottom: 0px; font-family: Aptos, "Aptos_EmbeddedFont", "Aptos_MSFontService", Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Daniel</div>
</body>
</html>