<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=us-ascii">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:Consolas;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri","sans-serif";}
@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="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">Dear All,<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">I am having some issues upgrading to PETSc 3.7 due to some changes in the KSPMonitor routines.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">I need to configure my KSP solver to output its convergence log to an existing ASCII viewer through a custom monitor, printing only one every 10 iterations.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">The calling code is Fortran, we wrote a small C code that just calls the default monitor every 10 iterations, passing the viewer as last argument. If I understood
 correctly it is now necessary to set up a PetscViewerAndFormat structure and pass that as last argument to the monitor routine.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">I tried to create one with PetscViewerAndFormatCreate but I'm getting a runtime error (see below).<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">Could you please help me understand what I did wrong?<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">Thanks in advance,<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">Federico<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">Error:<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal" style="margin-left:36.0pt;text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">[1]PETSC ERROR: #1 PetscViewerPushFormat() line 149 in /PETSc/petsc-3.7.0/src/sys/classes/viewer/interface/viewa.c<span style="color:black;background:black;mso-highlight:black"><o:p></o:p></span></span></p>
<p class="MsoNormal" style="margin-left:36.0pt;text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">Error Message --------------------------------------------------------------<span style="color:black;background:black;mso-highlight:black"><o:p></o:p></span></span></p>
<p class="MsoNormal" style="margin-left:36.0pt;text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">[8]PETSC ERROR: Argument out of range<span style="color:black;background:black;mso-highlight:black"><o:p></o:p></span></span></p>
<p class="MsoNormal" style="margin-left:36.0pt;text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">[8]PETSC ERROR: Too many PetscViewerPushFormat(), perhaps you forgot PetscViewerPopFormat()?<span style="color:black;background:black;mso-highlight:black"><o:p></o:p></span></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">Fortran code:<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">  PetscViewer
<span style="color:#BBBB00">::</span> viewer<span style="color:black;background:black;mso-highlight:black"><o:p></o:p></span></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">  PetscViewerAndFormat
<span style="color:#BBBB00">::</span> vf<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas"> 
<span style="color:#00BB00">external</span> ShellKSPMonitor<span style="color:black;background:black;mso-highlight:black"><o:p></o:p></span></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">...<o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas"> 
<span style="color:#00BBBB">call</span> PetscViewerAndFormatCreate<span style="color:#BB00BB">(</span>viewer<span style="color:#BBBB00">,</span> PETSC_VIEWER_DEFAULT<span style="color:#BBBB00">,
</span>vf<span style="color:#BBBB00">,</span>ierr<span style="color:#BB00BB">)</span><span style="color:black;background:black;mso-highlight:black"><o:p></o:p></span></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas"> 
<span style="color:#00BBBB">call</span> KSPMonitorSet<span style="color:#BB00BB">(</span>ksp<span style="color:#BBBB00">,</span> ShellKSPMonitor<span style="color:#BBBB00">,</span> vf<span style="color:#BBBB00">,</span> PetscViewerAndFormatDestroy<span style="color:#BBBB00">,</span>
 ierr<span style="color:#BB00BB">)</span><span style="color:black;background:black;mso-highlight:black"><o:p></o:p></span></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:Consolas">C code:<o:p></o:p></span></p>
<p class="MsoNormal" style="margin-left:36.0pt;text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">PetscErrorCode  shellkspmonitor_<span style="color:#BB00BB">(</span><span style="color:#FF5555">KSP</span>
<span style="color:#BBBB00">*</span>ksp<span style="color:#BBBB00">,</span> PetscInt
<span style="color:#BBBB00">*</span>n<span style="color:#BBBB00">,</span> PetscReal
<span style="color:#BBBB00">*</span>rnorm<span style="color:#BBBB00">,</span> <span style="color:#00BB00">
void</span> <span style="color:#BBBB00">*</span>ptr<span style="color:#BB00BB">)</span><span style="color:black;background:black;mso-highlight:black"><o:p></o:p></span></span></p>
<p class="MsoNormal" style="margin-left:36.0pt;text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#BB00BB">{</span><span style="font-size:10.0pt;font-family:Consolas;color:black;background:black;mso-highlight:black"><o:p></o:p></span></p>
<p class="MsoNormal" style="margin-left:36.0pt;text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">    PetscErrorCode ierr<span style="color:#BBBB00">=</span>0<span style="color:#BBBB00">;</span><span style="color:black;background:black;mso-highlight:black"><o:p></o:p></span></span></p>
<p class="MsoNormal" style="margin-left:36.0pt;text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">   
<span style="color:#00BB00">if</span> <span style="color:#BB00BB">(</span><span style="color:#BBBB00">*</span>n
<span style="color:#BBBB00">%</span> 10 <span style="color:#BBBB00">==</span> 0<span style="color:#BB00BB">)</span>
<span style="color:#BB00BB">{</span><span style="color:black;background:black;mso-highlight:black"><o:p></o:p></span></span></p>
<p class="MsoNormal" style="margin-left:36.0pt;text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">       ierr
<span style="color:#BBBB00">=</span> KSPMonitorTrueResidualNorm<span style="color:#BB00BB">(</span><span style="color:#BBBB00">*</span>ksp<span style="color:#BBBB00">,*</span>n<span style="color:#BBBB00">,*</span>rnorm<span style="color:#BBBB00">,</span><span style="color:#BB00BB">(</span>PetscViewerAndFormat<span style="color:#BBBB00">*</span><span style="color:#BB00BB">)</span>ptr<span style="color:#BB00BB">)</span><span style="color:#BBBB00">;</span><span style="color:#FF5555">CHKERRQ</span><span style="color:#BB00BB">(</span>ierr<span style="color:#BB00BB">)</span><span style="color:#BBBB00">;</span><span style="color:black;background:black;mso-highlight:black"><o:p></o:p></span></span></p>
<p class="MsoNormal" style="margin-left:36.0pt;text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">   
<span style="color:#BB00BB">}</span><span style="color:black;background:black;mso-highlight:black"><o:p></o:p></span></span></p>
<p class="MsoNormal" style="margin-left:36.0pt;text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas">   
<span style="color:#FF55FF">return</span> ierr<span style="color:#BBBB00">;</span><span style="color:black;background:black;mso-highlight:black"><o:p></o:p></span></span></p>
<p class="MsoNormal" style="margin-left:36.0pt;text-autospace:none"><span style="font-size:10.0pt;font-family:Consolas;color:#BB00BB">}</span><span style="font-size:10.0pt;font-family:Consolas;color:black;background:black;mso-highlight:black"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal"><b><span lang="IT" style="font-size:18.5pt;font-family:"Arial","sans-serif";color:#0093D0;mso-fareast-language:IT">______</span></b><b><span lang="IT" style="font-size:18.5pt;font-family:"Arial","sans-serif";color:#6A737B;mso-fareast-language:IT">
</span></b><b><span lang="IT" style="font-size:18.5pt;font-family:"Arial","sans-serif";color:#8CC63F;mso-fareast-language:IT">______</span></b><b><span lang="IT" style="font-size:18.5pt;font-family:"Arial","sans-serif";color:#6A737B;mso-fareast-language:IT">
</span></b><b><span lang="IT" style="font-size:18.5pt;font-family:"Arial","sans-serif";color:#FCB034;mso-fareast-language:IT">______</span></b><b><span lang="IT" style="font-size:18.5pt;font-family:"Arial","sans-serif";color:#6A737B;mso-fareast-language:IT"><o:p></o:p></span></b></p>
<p class="MsoNormal"><span lang="IT" style="color:#404040;mso-fareast-language:IT">Federico Miorelli</span><span lang="IT" style="mso-fareast-language:IT"><o:p></o:p></span></p>
<p class="MsoNormal"><span lang="IT" style="color:#1F497D;mso-fareast-language:IT">Senior R&D Geophysicist<o:p></o:p></span></p>
<p class="MsoNormal"><b><span style="font-size:10.0pt;color:#1F497D;mso-fareast-language:EN-GB">Subsurface Imaging - General Geophysics
</span></b><b><span style="font-size:9.0pt;color:#1F497D;mso-fareast-language:EN-GB">Italy</span></b><span style="font-size:8.0pt;color:#404040;mso-fareast-language:EN-GB"><o:p></o:p></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:12.0pt;font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-family:Consolas"><o:p> </o:p></span></p>
</div>
<p class="MsoNormal"><i><span lang="EN-GB" style="font-size:7.0pt;font-family:"Arial","sans-serif";
color:#6A737B;mso-ansi-language:EN-GB">This email and any accompanying attachments are confidential. If you received this email by mistake, please delete<br>
it from your system. Any review, disclosure, copying, distribution, or use of the email by others is strictly prohibited.<o:p></o:p></span></i></p>
</body>
</html>