<html><head></head><body><div style="font-family:garamond, new york, times, serif;font-size:13px;"><div>Hello PetSc users,</div><div><br></div><div>I am a beginner to PetSc and I am trying to write Jacobian matrix for my problem involving 2 nonlinear equations in 2 unknown variables, solving in a 1D domain.</div><div>Most of the petsc examples involve one variable and I am finding it difficult to understand how the Jacobian matrix is built when there are more than one variables.<br></div><div>For my problem the DOF is 2 (since 2 unknown variables). I am using finite difference and my stencil is 3 (i-1,i,i+1).</div><div>Jacobian matrix will have 2x2 submatrix elements and with stencil 3, we have to set 4x3=12 values for each grid point.<br></div><div>Let the unknown variables are "u,v" and (F1_u)_i-1 represent "partial derivative of residual of equation 1 at grid point 'i' with respect to variable 'u' at i-1".</div><div><br></div><div>Is the following pseudo-code the correct way of filling jacobian matrix?<br></div><div><br></div><div> FormJacobianLocal(DMDALocalInfo *info,Field *sol,Mat J,Mat Jpre, AppCtx *user)</div><div>{</div><div>  .......<br></div><div>    MatStencil     col[2][3],row;</div><div>    PetscReal       v[2][3];<br></div><div>.........</div><div>for(i=xs;i<xs+xm;i++){</div><div>    row.i = i;<b> row.c=0;</b></div><div>    { // Internal points<br></div><div>        col[0][0].i = i-1;     col[0][0].c = 0;     v[0][0] = jacobian value =  (F1_u)_i-1<br></div><div>        col[0][1].i = i;        col[0][1].c = 0;     v[0][1] = jacobian value = (F1_u)_i<br></div><div>        col[0][2].i = i+1;    col[0][2].c = 0;    v[0][1] =  jacobian value =  (F1_u)_i+1<br></div><div><div>       </div><div>        col[1][0].i = i-1;     col[1][0].c = 1;     v[1][0] = jacobian value =  (F1_v)_i-1</div><div>        col[1][1].i = i;        col[1][1].c = 1;     v[1][1] = jacobian value = (F1_v)_i<br></div>        col[1][2].i = i+1;    col[1][2].c = 1;    v[1][1] =  jacobian value =  (F1_v)_i+1<br></div><div>    }</div><div>  MatSetValuesStencil(Jpre,1,&row,6,col,v,INSERT_VALUES);</div><div>row.i = i;<b> row.c=1;</b><div>     { // Internal points<br><div>        col[0][0].i = i-1;     col[0][0].c = 0;     v[0][0] = jacobian value =  (F2_u)_i-1<br></div><div>        col[0][1].i = i;        col[0][1].c = 0;     v[0][1] = jacobian value = (F2_u)_i<br></div><div>        col[0][2].i = i+1;    col[0][2].c = 0;    v[0][1] =  jacobian value =  (F2_u)_i+1<br></div><div><div>       </div><div>        col[1][0].i = i-1;     col[1][0].c = 1;     v[1][0] = jacobian value =  (F2_v)_i-1</div><div>        col[1][1].i = i;        col[1][1].c = 1;     v[1][1] = jacobian value = (F2_v)_i<br></div>        col[1][2].i = i+1;    col[1][2].c = 1;    v[1][1] =  jacobian value =  (F2_v)_i+1<br></div><div>    }</div>  MatSetValuesStencil(Jpre,1,&row,6,col,v,INSERT_VALUES);<br></div></div><div>}</div><div>.......<br></div><div>}</div><div><br></div><div>Thank you,</div><div>Rahul.</div></div></body></html>