Does anyone have a MATLAB code example for a staggered grid (for 1D/2D problems)?

49 visualizzazioni (ultimi 30 giorni)
I’m working on solving the shallow water equations using a staggered grid in MATLAB, and I’m looking for example codes or guidance for a staggered grid. Specifically, I want to understand how to set up the grid (interfaces and centers), apply boundary/initial conditions. These are my equations,
  2 Commenti
Torsten
Torsten il 26 Set 2025 alle 20:26
Modificato: Torsten il 26 Set 2025 alle 20:27
I know the use of staggered grids to compute velocity and pressure for the Navier-Stokes equations.
Why do you think this is necessary for the shallow water equations ?
I suggest working with CLAWPACK because the equations are quite difficult to handle numerically:
Sajani
Sajani il 26 Set 2025 alle 21:06
@Torsten Thank you for your response. I’m using the staggered grid because many references have applied it previously, and I’m trying to implement a simple version in MATLAB before moving to more advanced. If you have a MATLAB code for staggered grids applied to the Navier–Stokes equations, could you please share it? I’ll also check out CLAWPACK as you suggested.

Accedi per commentare.

Risposte (1)

William Rose
William Rose il 28 Set 2025 alle 1:23
Here is a start, just to show how the grids set up in 2D:
dx=0.1; Lx=1;
dy=0.1; Ly=1;
[PX,PY]=meshgrid(0:dx:Lx,0:dy:Ly);
[UX,UY]=meshgrid(dx/2:dx:Lx-dx/2,0:dy:Ly);
[VX,VY]=meshgrid(0:dx:Lx,dy/2:dy:Ly-dy/2);
Display the grids
figure;
plot(PX(:),PY(:),'r.',UX(:),UY(:),'g.',VX(:),VY(:),'b.')
legend('Pressure','U','V'); xlabel('X'); ylabel('Y'); title('Staggered Grids')
For 3D, add a grid for W (z-component of velocity) which is offset by dz/2.
Depending on your boundary conditions, you might want the U and V grid opoints to lie exactly on both boundaries. For example, in a tank, U and V are zero at the edges, so place the U and V grid points on the tank edges, where the respective velocities must be zero. The boundary conditions will include: {U=0 at X=0 and at X=Lx}; {V=0 at Y=0 and at Y=Ly}. One way to make such a grid is shown below.
[UX,UY]=meshgrid(0:dx:Lx,dy/2:dy:Ly-dy/2);
[VX,VY]=meshgrid(dx/2:dx:Lx-dx/2,0:dy:Ly);
[PX,PY]=meshgrid(dx/2:dx:Lx-dx/2,dy/2:dy:Ly-dy/2);
Display the grids
figure;
plot(PX(:),PY(:),'r.',UX(:),UY(:),'g.',VX(:),VY(:),'b.')
legend('Pressure','U','V'); xlabel('X'); ylabel('Y'); title('Staggered Grids')
For the specific equations on the staggered grids, consult the journal articles to which you referred.
  9 Commenti
Torsten
Torsten circa 2 ore fa
Modificato: Torsten circa un'ora fa
I didn't check your code carefully, but what immediately strikes me is that you never reference eta(Nx+1,:) . This means that the boundary condition for eta at x=L never "progresses" into the domain.
Further, you compute eta(i,n+1) using Q(:,n+1). You should use an explicit time step here, thus use Q(:,n) instead. Otherwise, your time integration scheme is some mixture of explicit and implicit scheme - hard to say whether it will produce something useful.
And in all sources I searched the non-conservative form of the equations reads
dh/dt + u*dh/dx + h*du/dx = 0
du/dx + u*du/dx = -g*dh/dx + some sink term
Maybe you could explain how this transforms to the system you wrote in your original question.

Accedi per commentare.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by