# [petsc-users] How to apply boundary conditions when using TS?

zhfreewill zhfreewill at gmail.com
Mon Nov 15 20:51:56 CST 2021

```Hi,
I'm new to PETSc. I have been confused about how to apply boundary
conditions (BCs) (e.g., the Dirichlet and the Neumann type) when using the
time-stepping (TS) object of PETSc.

I have read the example codes (
https://petsc.org/release/src/ts/tutorials/index.html) and vaguely found
that BCs seems to be set  as follows:

1. when evaluating a matrix of the right hand side function g(x) (e.g.,
using a function like RHSMatrixHeat)
2. when evaluating a matrix of the Jacobian matrix of g'(x) (e.g., using a
function like FormJacobian)

For instance, in the ex1.4 (
the beginning states

17: /*
------------------------------------------------------------------------
19:    This program solves the one-dimensional heat equation (also called
the
20:    diffusion equation),
21:        u_t = u_xx,
22:    on the domain 0 <= x <= 1, with the boundary conditions
23:        u(t,0) = 0, u(t,1) = 0,
24:    and the initial condition
25:        u(0,x) = sin(6*pi*x) + 3*sin(2*pi*x).
26:    This is a linear, second-order, parabolic equation.

28:    We discretize the right-hand side using finite differences with
29:    uniform grid spacing h:
30:        u_xx = (u_{i+1} - 2u_{i} + u_{i-1})/(h^2)
...
47:
------------------------------------------------------------------------- */

and Dirichlet BCs, u(t,0) = 0 and u(t,1) = 0, are to set on the both side
of the discretized domain, the corresponding codes can be found on the
following lines in a function RHSMatrixHeat:

/*
------------------------------------------------------------------------- */
463: RHSMatrixHeat - User-provided routine to compute the right-hand-side
matrix for the heat equation. */
491: PetscErrorCode RHSMatrixHeat(TS ts,PetscReal t,Vec X,Mat AA,Mat
BB,void *ctx)
492: {
...
505:   /* Set matrix rows corresponding to boundary data */
509:   if (mstart == 0) {  /* first processor only */
510:     v[0] = 1.0;
511:     MatSetValues(A,1,&mstart,1,&mstart,v,INSERT_VALUES);
512:     mstart++;
513:   }
515:   if (mend == appctx->m) { /* last processor only */
516:     mend--;
517:     v[0] = 1.0;
518:     MatSetValues(A,1,&mend,1,&mend,v,INSERT_VALUES);
519:   }

521:   /* Set matrix rows corresponding to interior data.  We construct the
matrix one row at a time. */
525:   v[0] = sone; v[1] = stwo; v[2] = sone;
526:   for (i=mstart; i<mend; i++) {
527:     idx[0] = i-1; idx[1] = i; idx[2] = i+1;
528:     MatSetValues(A,1,&i,3,idx,v,INSERT_VALUES);
529:   }
...
550: }
/*
------------------------------------------------------------------------- */

My questions are:

1. How do these lines of code embody the Dirichlet BCs u(t,0) = 0 and
u(t,1) = 0.

2. Similarly, if I want to apply a Neumann type BCs in these lines, How can
I do?

Any suggestions or documents?

Best,
Qw
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20211116/7314bfa3/attachment.html>
```