<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class=""><br class=""></div>  Could you be more specific in saying how it fails (can you share the actual prototype code?) <div class=""><br class=""></div><div class="">  Note here you seem to be missing a /dx term</div><div class=""><br class=""></div><div class=""><blockquote style="font-family: Calibri, Helvetica, sans-serif; margin-top: 0px; margin-bottom: 0px;" class=""><div class=""><font size="1" class=""><span style="font-family: "Courier New", monospace; color: rgb(237, 92, 87);" class=""><b class="">G[i] = (u[i+1] - u[i]) - q_A;           </b></span><span style="font-family: "Courier New", monospace; color: rgb(153, 153, 153);" class=""><b class="">//Neumann BC:   du/dx(x=0.0) = q_A;</b></span></font></div><div class=""><font size="1" class=""><span style="font-family: "Courier New", monospace; color: rgb(153, 153, 153);" class=""><b class=""><br class=""></b></span></font></div></blockquote><div><br class=""></div><div>   Barry</div><div><br class=""><blockquote type="cite" class=""><div class="">On Nov 26, 2021, at 4:02 AM, zhfreewill <<a href="mailto:zhfreewill@gmail.com" class="">zhfreewill@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_default" style="font-family:simsun,serif"><span style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt;" class="">Hi all,</span><br class=""></div><div class="gmail_quote"><div dir="ltr" class="">
<div class="">
<div dir="ltr" class="">
<div class="">
<div dir="ltr" class="">
<div class="">
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">I have a basic question on how to apply B.C. for a cell-centered structured FV discretization when using the SNES and Ts object.</span></div><div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class=""><span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class=""><span class="gmail_default" style="font-family:simsun,serif"></span></span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; background-color: rgba(0, 0, 0, 0);" class="">For instance, for a 1-D transient diffusion equation</span></div>
<blockquote style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; margin-top: 0px; margin-bottom: 0px;" class="">
<div class=""><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">u_t = u_xx, 0 < x < 1</b></span></div>
</blockquote>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; background-color: rgba(0, 0, 0, 0);" class="">with IC: </span></div>
<blockquote style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; margin-top: 0px; margin-bottom: 0px;" class="">
<div class=""><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">u(x,t=0) = 0;</b></span></div>
</blockquote>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; background-color: rgba(0, 0, 0, 0);" class="">and Dirichlet BC:</span></div>
<blockquote style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; margin-top: 0px; margin-bottom: 0px;" class="">
<div class=""><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">u(x=0) = u_A = 0.0;</b></span></div>
<div class=""><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">u(x=1) = u_B = 1.0;</b></span></div>
</blockquote>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; background-color: rgba(0, 0, 0, 0);" class="">or Neumann BC:</span></div>
<blockquote style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; margin-top: 0px; margin-bottom: 0px;" class="">
<div class=""><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">du/dx(x=0.0) = q_A = 0.0;</b></span></div>
<div class=""><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">du/dx(x=1.0) = q_B = 1.0;</b></span></div>
</blockquote>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Courier New",monospace;font-size:10pt" class="">    </span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; background-color: rgba(0, 0, 0, 0);" class="">When using TS of PETSc, the equation can be organized in form of</span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Courier New",monospace;font-size:10pt" class="">    </span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">U_t = G(t,u)</b></span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; background-color: rgba(0, 0, 0, 0);" class="">where
</span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87);background-color:rgba(0,0,0,0)" class=""><b class="">G</b></span><span style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; background-color: rgba(0, 0, 0, 0);" class="">
 is the right<span class="gmail_default" style="font-family:simsun,serif"></span>-hand-side (RHS) function regardless of the specific method of discretization.</span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Arial Black",Arial,sans-serif;font-size:10pt;color:rgb(222,106,25);background-color:rgba(0,0,0,0)" class=""><b class="">(1) For FD discretization</b></span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Courier New",monospace;font-size:10pt" class=""><br class="">
</span></div>
<blockquote style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; margin-top: 0px; margin-bottom: 0px;" class="">
<div class=""><b class=""><span style="font-family:"Courier New",monospace;font-size:10pt" class=""></span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class="">A               |---dx--|               B</span></b></div>
<div class=""><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">|-------|-------|-------|-------|-------| grid size = nx</b></span></div>
<div class=""><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">0              i-1      i      i+1     nx-1</b></span></div>
<div class=""><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class=""><br class="">
</b></span></div>
</blockquote>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Courier New",monospace;font-size:10pt" class=""></span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">the vertex-centered FD discretization of the domain is</span></div>
<blockquote style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; margin-top: 0px; margin-bottom: 0px;" class="">
<div class=""><span style="font-family:"Courier New",monospace;font-size:10pt" class=""><b class=""></b></span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">x(i) = i*dx (i = 0,..., nx)), where dx = 1/nx</b></span></div>
</blockquote>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">and the RHS function G of the equation is</span></div>
<blockquote style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; margin-top: 0px; margin-bottom: 0px;" class="">
<div class=""><span style="font-family:"Courier New",monospace;font-size:10pt" class=""><b class=""></b></span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">G[i] = (u[i+1]-2*u[i]+u[i-1])/dx^2</b></span></div>
<div class=""><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class=""><br class="">
</b></span></div>
</blockquote>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Courier New",monospace;font-size:10pt" class=""></span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">There have been dozens of examples demonstrating way of BC setting with a finite difference medthod, e.g.,</span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<a href="https://github.com/petsc/petsc/blob/386ebab930a846575609b49074a40c46e7f8ed75/src/ts/tutorials/ex26.c#L293-L343" id="m_3788707207907009609LPlnk737449" target="_blank" class="">https://github.com/petsc/petsc/blob/386ebab930a846575609b49074a40c46e7f8ed75/src/ts/tutorials/ex26.c#L293-L343</a><br class="">
</div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<div id="m_3788707207907009609LPBorder_GTaHR0cHM6Ly9naXRodWIuY29tL3BldHNjL3BldHNjL2Jsb2IvMzg2ZWJhYjkzMGE4NDY1NzU2MDliNDkwNzRhNDBjNDZlN2Y4ZWQ3NS9zcmMvdHMvdHV0b3JpYWxzL2V4MjYuYyNMMjkzLUwzNDM." style="width:100%;margin-top:16px;margin-bottom:16px;max-width:800px;min-width:424px" class="">
<div id="m_3788707207907009609LPCloseButtonContainer950726" title="删除链接预览" role="button" class="">
<span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">which I think I hav</span><span style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; background-color: rgba(0, 0, 0, 0);" class="">e underst</span><span style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; background-color: rgba(0, 0, 0, 0);" class="">ood
</span><span style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; background-color: rgba(0, 0, 0, 0);" class="">how it works and the following lines of code evaluate the RHS function
</span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(200,38,19);background-color:rgba(0,0,0,0)" class=""><b class="">G(t,u)</b></span><span style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; background-color: rgba(0, 0, 0, 0);" class="">
 in PETSc's </span><b class=""><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(200,38,19);background-color:rgba(0,0,0,0)" class="">TSSetRHSFun</span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(200,38,19);background-color:rgba(0,0,0,0)" class="">ction()</span></b><span style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; background-color: rgba(0, 0, 0, 0);" class="">
 routine</span></div>
</div>
</div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Courier New",monospace;font-size:10pt" class=""></span></div>
<div style="font-family: Calibri, Helvetica, sans-serif;" class="">
<font size="1" class=""><span style="font-family:"Courier New",monospace" class=""></span><span style="font-family:"Courier New",monospace;color:rgb(237,92,87);background-color:rgba(0,0,0,0)" class="">/*------------------FD discretization-------------------------*/</span></font></div>
<div style="font-family: Calibri, Helvetica, sans-serif;" class="">
<span style="font-family:"Courier New",monospace;color:rgb(237,92,87);background-color:rgba(0,0,0,0)" class=""><b class=""><font size="1" class="">for (i = xs; i < xs + xm; i++) {</font></b></span></div>
<blockquote style="font-family: Calibri, Helvetica, sans-serif; margin-top: 0px; margin-bottom: 0px;" class="">
<div class=""><span style="font-family:"Courier New",monospace;color:rgb(153,153,153)" class=""><b class=""><font size="1" class="">// left boundary</font></b></span></div>
<div class=""><span style="font-family:"Courier New",monospace;color:rgb(237,92,87)" class=""><b class=""><font size="1" class="">if (i == 0) {</font></b></span></div>
<blockquote style="margin-top:0;margin-bottom:0" class="">
<div class=""><font size="1" class=""><span style="font-family:"Courier New",monospace;color:rgb(237,92,87)" class=""><b class="">G[i] = u[i] - u_A;                </b></span><span style="font-family:"Courier New",monospace;color:rgb(153,153,153);background-color:rgba(0,0,0,0)" class=""><b class="">//
 Dirichlet BC: u(x=0) = u_A;</b></span></font></div>
<div class=""><font size="1" class=""><span style="font-family:"Courier New",monospace;color:rgb(237,92,87)" class=""><b class="">G[i] = (u[i] - u[i+1])/dx - q_A;  </b></span><span style="font-family:"Courier New",monospace;color:rgb(153,153,153);background-color:rgba(0,0,0,0)" class=""><b class="">//
 Neumann BC:   du/dx(x=0.0) = q_A;</b></span></font></div>
</blockquote>
<div class=""><span style="font-family:"Courier New",monospace;color:rgb(237,92,87)" class=""><b class=""><font size="1" class="">}
</font></b></span></div>
<div class=""><span style="font-family:"Courier New",monospace;color:rgb(153,153,153);background-color:rgba(0,0,0,0)" class=""><b class=""><font size="1" class="">// right boundary</font></b></span></div>
<div class=""><span style="font-family:"Courier New",monospace;color:rgb(237,92,87)" class=""><b class=""><font size="1" class="">else if (i == mx - 1) {</font></b></span></div>
<blockquote style="margin-top:0;margin-bottom:0" class="">
<div class=""><font size="1" class=""><span style="font-family:"Courier New",monospace;color:rgb(237,92,87)" class=""><b class="">G[i] = u[i] - u_B;                </b></span><span style="font-family:"Courier New",monospace;color:rgb(153,153,153);background-color:rgba(0,0,0,0)" class=""><b class="">//
 Dirichlet BC: u(x=1.0) = u_B;</b></span></font></div>
<div class=""><font size="1" class=""><span style="font-family:"Courier New",monospace;color:rgb(237,92,87)" class=""><b class="">G[i] = (u[i]-u[i-1])/dx - q_B;    </b></span><span style="font-family:"Courier New",monospace;color:rgb(153,153,153);background-color:rgba(0,0,0,0)" class=""><b class="">//
 Neumann BC:   du/dx(x=0.0) = q_A;</b></span></font></div>
</blockquote>
<div class=""><span style="font-family:"Courier New",monospace;color:rgb(237,92,87)" class=""><b class=""><font size="1" class="">}</font></b></span></div>
<div class=""><span style="font-family:"Courier New",monospace;color:rgb(153,153,153);background-color:rgba(0,0,0,0)" class=""><b class=""><font size="1" class="">// internal</font></b></span></div>
<div class=""><span style="font-family:"Courier New",monospace;color:rgb(237,92,87)" class=""><b class=""><font size="1" class="">else {</font></b></span></div>
<blockquote style="margin-top:0;margin-bottom:0" class="">
<div class=""><font size="1" class=""><span style="font-family:"Courier New",monospace;color:rgb(237,92,87)" class=""><b class="">G[i] = (u[i-1] - 2 * u[i] + u[i + 1])/dx^2;
</b></span><span style="font-family:"Courier New",monospace;color:rgb(153,153,153);background-color:rgba(0,0,0,0)" class=""><b class="">//u_xx</b></span></font></div>
</blockquote>
<div class=""><span style="font-family:"Courier New",monospace;color:rgb(237,92,87)" class=""><b class=""><font size="1" class="">}</font></b></span></div>
</blockquote>
<div style="font-family: Calibri, Helvetica, sans-serif;" class="">
<font size="1" class=""><span style="font-family:"Courier New",monospace;color:rgb(237,92,87)" class=""><b class="">}</b></span><span style="font-family:"Courier New",monospace;color:rgb(153,153,153);background-color:rgba(0,0,0,0)" class=""><span style="color:rgb(237,92,87);font-variant-ligatures:inherit;font-variant-caps:inherit" class=""><b style="" class="">/*------------------------------------------------------------*/</b></span></span></font></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Courier New",monospace;color:rgb(237,92,87);font-size:8pt" class=""><b class=""><br class="">
</b></span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Courier New",monospace;font-size:10pt" class=""></span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">The BCs, e.g., at the left boudnary, can be straightforwardly set as follows:</span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">Dirichlet BC:</span><span style="font-family:"Courier New",monospace;font-size:10pt" class="">
</span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Courier New",monospace;font-size:10pt" class="">    </span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87);background-color:rgba(0,0,0,0)" class=""><b class="">G[0] = u[0] - u_A</b></span><span style="font-family:"Courier New",monospace;font-size:10pt" class="">
              </span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Courier New",monospace;font-size:10pt" class="">    </span><span style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; background-color: rgba(0, 0, 0, 0);" class="">which indicates</span><span style="font-family:"Courier New",monospace;font-size:10pt" class="">
</span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87);background-color:rgba(0,0,0,0)" class=""><b class="">G[0] = u[0] - u_A = 0</b></span><span style="font-family:"Courier New",monospace;font-size:10pt" class="">
</span><span style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; background-color: rgba(0, 0, 0, 0);" class="">and Dirichlet BC</span><span style="font-family:"Courier New",monospace;font-size:10pt" class="">
</span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87);background-color:rgba(0,0,0,0)" class=""><b class="">u[0] = u_A</b></span><span style="font-family:"Courier New",monospace;font-size:10pt" class="">
</span><span style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; background-color: rgba(0, 0, 0, 0);" class="">is thus applied</span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">Neumann BC:</span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Courier New",monospace;font-size:10pt" class="">    </span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87);background-color:rgba(0,0,0,0)" class=""><b class="">G[0] = (u[0] - u[1])/dx - q_A</b></span><span style="font-family:"Courier New",monospace;font-size:10pt" class="">
</span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Courier New",monospace;font-size:10pt" class="">    </span><span style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; background-color: rgba(0, 0, 0, 0);" class="">which indicates</span><span style="font-family:"Courier New",monospace;font-size:10pt" class="">
</span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">G[0] = (u[0] - u[1])/dx - q_A = 0</b></span><span style="font-family:"Courier New",monospace;font-size:10pt" class="">
</span><span style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; background-color: rgba(0, 0, 0, 0);" class="">and Neumann BC</span><span style="font-family:"Courier New",monospace;font-size:10pt" class="">
</span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87);background-color:rgba(0,0,0,0)" class=""><b class="">du/dx = (u[0] - u[1])/dx = q_A</b></span><span style="font-family:"Courier New",monospace;font-size:10pt" class="">
</span><span style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; background-color: rgba(0, 0, 0, 0);" class="">is applied</span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Courier New",monospace;font-size:10pt" class=""></span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">This is the way the above examples apply BCs.</span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<br class="">
</div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">For a structured, cell-centered FVM discretization, however, I tried and found the BCs can not be applied in a similar way. I will explain how I tried this as follows.</span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Arial Black",Arial,sans-serif;font-size:10pt;color:rgb(222,106,25)" class=""><b class="">(2) FV discretization</b></span></div>
<blockquote style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; margin-top: 0px; margin-bottom: 0px;" class="">
<div class=""><b class=""><span style="font-family:"Courier New",monospace;font-size:10pt" class=""></span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class="">A               |---dx--|               B</span></b></div>
<div class=""><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">|---o---|---o---|---o---|---o---|---o---| cell size = nx</b></span></div>
<div class=""><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">W   w   P   e   E          
</b></span></div>
<div class=""><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">0      i-1      i      i+1     nx-1</b></span></div>
<div class=""><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class=""><br class="">
</b></span></div>
</blockquote>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">(NOTE: the center cell is denoted as P and its neighbouring cells as W, E; </span><span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">cell faces are denoted as w, e; the left
 and right boundary faces are A and B, respectively.)</span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<br class="">
</div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">The cell-centered FV discretization of the domain is</span></div>
<blockquote style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; margin-top: 0px; margin-bottom: 0px;" class="">
<div class=""><span style="font-family:"Courier New",monospace;font-size:10pt" class=""><b class=""></b></span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">x(i) = dx/2 + i*dx (i = 0,..., nx-1)), where dx = 1/nx</b></span></div>
</blockquote>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Courier New",monospace;font-size:10pt" class=""></span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">the diffusion term u_xx can be reorganized by using divergence theorem</span></div>
<blockquote style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; margin-top: 0px; margin-bottom: 0px;" class="">
<div class=""><b class=""><span style="font-family:"Courier New",monospace;font-size:10pt" class=""></span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class="">(u_E-u_P)   (u_P-u_W)</span></b></div>
<div class=""><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">--------- - ---------</b></span></div>
<div class=""><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">    dx         dx</b></span></div>
</blockquote>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">and the RHS function G of the equation is</span></div>
<blockquote style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; margin-top: 0px; margin-bottom: 0px;" class="">
<div class=""><span style="font-family:"Courier New",monospace;font-size:10pt" class=""><b class=""></b></span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">G[i] = (u_E-u_P)/dx - (u_P-u_W)/dx = (u[i+1]-2*u[i]+u[i-1])/dx</b></span></div>
</blockquote>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Courier New",monospace;font-size:10pt" class=""><span style="margin:0px;font-size:10pt;background-color:rgb(255,255,255);font-family:Arial,Helvetica,sans-serif" class="">the key lines of code in RHS function G(t,u) in PETSc's</span><span style="margin:0px;font-size:10pt;background-color:rgb(255,255,255)" class=""><span style="font-family:Arial,Helvetica,sans-serif" class=""> </span></span><span style="margin:0px;font-size:10pt;color:rgb(237,92,87);background-color:rgb(255,255,255);font-family:Arial,Helvetica,sans-serif" class=""><b class="">TSSetRHSFunction()</b></span><span style="margin:0px;font-size:10pt;background-color:rgb(255,255,255)" class=""><span style="font-family:Arial,Helvetica,sans-serif" class=""> </span></span><span style="margin:0px;font-size:10pt;background-color:rgb(255,255,255);font-family:Arial,Helvetica,sans-serif" class="">routine
 can be</span></span></div><div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class=""><span style="font-family:"Courier New",monospace;font-size:10pt" class=""><span style="margin:0px;font-size:10pt;background-color:rgb(255,255,255);font-family:Arial,Helvetica,sans-serif" class=""><br class=""></span></span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Courier New",monospace;font-size:10pt" class=""></span></div>
<div style="font-family: Calibri, Helvetica, sans-serif;" class="">
<b style="" class=""><font size="1" class=""><span style="font-family:"Courier New",monospace" class=""></span><span style="font-family:"Courier New",monospace;color:rgb(153,153,153);background-color:rgba(0,0,0,0)" class="">/*------------------FV discretization-------------------------*/</span></font></b></div>
<div style="font-family: Calibri, Helvetica, sans-serif;" class="">
<span style="font-family:"Courier New",monospace;color:rgb(237,92,87)" class=""><b class=""><font size="1" class="">for (i = xs; i < xs + xm; i++) {</font></b></span></div>
<blockquote style="font-family: Calibri, Helvetica, sans-serif; margin-top: 0px; margin-bottom: 0px;" class="">
<div class=""><span style="font-family:"Courier New",monospace;color:rgb(153,153,153);background-color:rgba(0,0,0,0)" class=""><b class=""><font size="1" class="">// left boundary</font></b></span></div>
<div class=""><span style="font-family:"Courier New",monospace;color:rgb(237,92,87)" class=""><b class=""><font size="1" class="">if (i == 0) {</font></b></span></div>
<blockquote style="margin-top:0;margin-bottom:0" class="">
<div class=""><font size="1" class=""><span style="font-family:"Courier New",monospace;color:rgb(237,92,87)" class=""><b class="">G[i] = 3.0 * u[i] - u[i+1] - 2.0 * u_A;
</b></span><span style="font-family:"Courier New",monospace;color:rgb(153,153,153);background-color:rgba(0,0,0,0)" class=""><b class="">//Dirichlet BC: u(x=0.0) = u_A, does not work</b></span></font></div>
<div class=""><font size="1" class=""><span style="font-family:"Courier New",monospace;color:rgb(237,92,87)" class=""><b class="">G[i] = (u[i+1] - u[i]) - q_A;          
</b></span><span style="font-family:"Courier New",monospace;color:rgb(153,153,153);background-color:rgba(0,0,0,0)" class=""><b class="">//Neumann BC:   du/dx(x=0.0) = q_A;</b></span></font></div>
</blockquote>
<div class=""><span style="font-family:"Courier New",monospace;color:rgb(237,92,87)" class=""><b class=""><font size="1" class="">}</font></b></span></div>
<div class=""><span style="font-family:"Courier New",monospace;color:rgb(153,153,153);background-color:rgba(0,0,0,0)" class=""><b class=""><font size="1" class="">// right boundary</font></b></span></div>
<div class=""><span style="font-family:"Courier New",monospace;color:rgb(237,92,87)" class=""><b class=""><font size="1" class="">else if (i == mx - 1) {</font></b></span></div>
<blockquote style="margin-top:0;margin-bottom:0" class="">
<div class=""><font size="1" class=""><span style="font-family:"Courier New",monospace;color:rgb(237,92,87)" class=""><b class="">G[i] = 3.0 * u[i] - u[i-1] - 2.0 * u_B;
</b></span><span style="font-family:"Courier New",monospace;color:rgb(153,153,153);background-color:rgba(0,0,0,0)" class=""><b class="">//Dirichlet BC: u(x=1.0) = u_B, does not work</b></span></font></div>
<div class=""><font size="1" class=""><span style="font-family:"Courier New",monospace;color:rgb(237,92,87)" class=""><b class="">G[i] = (u[i]-u[i-1])/dx - q_B;          </b></span><span style="font-family:"Courier New",monospace;color:rgb(153,153,153);background-color:rgba(0,0,0,0)" class=""><b class="">//Neumann
 BC:   du/dx(x=1.0) = q_B, does not work</b></span></font></div>
</blockquote>
<div class=""><span style="font-family:"Courier New",monospace;color:rgb(237,92,87)" class=""><b class=""><font size="1" class="">}</font></b></span></div>
<div class=""><span style="font-family:"Courier New",monospace;color:rgb(153,153,153);background-color:rgba(0,0,0,0)" class=""><b class=""><font size="1" class="">// internal</font></b></span></div>
<div class=""><span style="font-family:"Courier New",monospace;color:rgb(237,92,87)" class=""><b class=""><font size="1" class="">else {</font></b></span></div>
<blockquote style="margin-top:0;margin-bottom:0" class="">
<div class=""><font size="1" class=""><span style="font-family:"Courier New",monospace;color:rgb(237,92,87)" class=""><b class="">G[i] = (u[i-1] - 2*u[i] + u[i+1])/dx;   </b></span><span style="font-family:"Courier New",monospace;color:rgb(153,153,153);background-color:rgba(0,0,0,0)" class=""><b class="">//u_x</b></span></font></div>
</blockquote>
<div class=""><span style="font-family:"Courier New",monospace;color:rgb(237,92,87)" class=""><b class=""><font size="1" class="">}</font></b></span></div>
</blockquote>
<div style="font-family: Calibri, Helvetica, sans-serif;" class="">
<font size="1" class=""><span style="font-family:"Courier New",monospace;color:rgb(237,92,87)" class=""><b class="">}</b></span><span style="font-family:"Courier New",monospace;color:rgb(153,153,153);background-color:rgba(0,0,0,0)" class=""><span style="color:rgb(237,92,87);font-variant-ligatures:inherit;font-variant-caps:inherit" class=""><b style="" class="">/*------------------------------------------------------------*/</b></span></span></font></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<br class="">
</div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">I tried to apply BCs in a way similar to that of the FD discretization.</span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">For the Dirichlet BC on the left boundary, because of the cell-centered arrangement of u[i], the left BC is given as
</span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87);background-color:rgba(0,0,0,0)" class=""><b class="">u_A</b></span><span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class=""> at cell face A instead of cell center.</span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">So, I first applied a linear interpolation to get the cell-centered value of
</span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87);background-color:rgba(0,0,0,0)" class=""><b class="">u[0]</b></span><span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class=""> from the left boundary value
</span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87);background-color:rgba(0,0,0,0)" class=""><b class="">u_A</b></span><span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class=""> and neighbouring
</span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87);background-color:rgba(0,0,0,0)" class=""><b class="">u[1]</b></span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<br class="">
</div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Courier New",monospace;font-size:10pt" class="">    </span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">u[0] = (u[1]+2*u_A)/3.0 (equivalent to 3*u[0] - u[1] - 2*u_A = 0.0)</b></span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<br class="">
</div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">    |-dx/2-|------dx-----|</b></span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">    |------o------|------o------| linear interpolation: u[0] = (u[1]+2*u_A)/3.0</b></span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">    A      P      e      E</b></span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">   u_A   u[0]          u[1]</b></span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<br class="">
</div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">Dirichlet BC:</span><span style="font-family:"Courier New",monospace;font-size:10pt" class="">
</span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Courier New",monospace;font-size:10pt" class="">    </span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">G[0] = 3.0*u[0] - u[1] - 2.0*u_A</b></span><span style="font-family:"Courier New",monospace;font-size:10pt" class="">
      </span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Courier New",monospace;font-size:10pt" class="">    in which </span>
<span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">u[0] = (u[1]+2*u_A)/3.0</b></span><span style="font-family:"Courier New",monospace;font-size:10pt" class="">
</span><span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">is implicitly calculated and I expect the Dirichlet BC</span><span style="font-family:"Courier New",monospace;font-size:10pt" class="">
</span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">u(x=0) = u_A</b></span><span style="font-family:"Courier New",monospace;font-size:10pt" class="">
</span><span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">is applied</span><span style="font-family:"Courier New",monospace;font-size:10pt" class="">
</span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<br class="">
</div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">For the Neumann BC, left BC is given as a gradient of
</span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87);background-color:rgba(0,0,0,0)" class=""><b class="">u</b></span><span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">,
</span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87);background-color:rgba(0,0,0,0)" class=""><b class="">q_A</b></span><span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">, at cell face A</span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">Neumann BC:</span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Courier New",monospace;font-size:10pt" class="">    </span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">G[i] = (u[1] - u[0])/dx - q_A</b></span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:"Courier New",monospace;font-size:10pt" class="">    </span><span style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; background-color: rgba(0, 0, 0, 0);" class="">in which Neumann BC</span><span style="font-family:"Courier New",monospace;font-size:10pt" class="">
</span><span style="font-family:"Courier New",monospace;font-size:10pt;color:rgb(237,92,87)" class=""><b class="">du/dx = (u[1] - u[0])/dx = q_A</b></span><span style="font-family:"Courier New",monospace;font-size:10pt" class="">
</span><span style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt; background-color: rgba(0, 0, 0, 0);" class="">is expected to be applied.</span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<br class="">
</div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">However, this FV discretization failed to work. Is there anything wrong or c</span><span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">ould you give me a hint?</span></div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<br class="">
</div>
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;" class="">
<span style="font-family:Arial,Helvetica,sans-serif;font-size:10pt" class="">Best,</span></div>
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 10pt;" class="">Qw</span><br class="">
</div>
</div>
</div>
</div>
</div>
</div>

</div></div>
</div></blockquote></div><br class=""></div></body></html>