source: CIVL/examples/accuracy/upwindFirstOrder.cvl@ 7320499

main test-branch
Last change on this file since 7320499 was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 2.1 KB
Line 
1/* First-order "upwind" scheme for 1-dim. advection.
2 * Uses forward differencing in time and makes a choice of forward
3 * or backward differencing in space based on the direction of the fluid flow.
4 * First-order accurate in space and time.
5 * To verify with CIVL:
6 * civl verify upwindFirstOrder.cvl
7 */
8
9$input int n; // Number of points
10$assume(n >= 1);
11$input double h; // Distance between points
12$assume(0<h && h<1);
13$input double dt; // Duration of a time step
14$assume(0<dt && dt<1);
15$input double a; // Constant for wave velocity
16$assume(a != 0);
17$input double u1[n]; // initial values
18// u:R^2->R has 2 continuous derivatives in [-1,1]x[0,100]
19$abstract $differentiable(2, [-1,1],[0,100]) $real u($real x, $real t);
20$input int step; // some arbitrary time step
21$assume(step >= 0);
22
23double v[n];
24double v_new[n];
25
26void upwindForward() {
27 /*@ loop invariant $forall (int j:1..i-1) v_new[j] == v[j] - dt*a*(v[j+1]-v[j])/h;
28 @ loop invariant 1<=i && i<=n-1;
29 @ loop assigns i, v_new[1..n-2];
30 @*/
31 for (int i = 1; i < n-1; i++) {
32 v_new[i] = v[i] - dt*a*(v[i+1] - v[i])/h;
33 }
34}
35
36void upwindBackward() {
37 /*@ loop invariant $forall (int j:1..i-1) v_new[j] == v[j] - dt*a*(v[j]-v[j-1])/h;
38 @ loop invariant 1<=i && i<=n-1;
39 @ loop assigns i, v_new[1..n-2];
40 @*/
41 for (int i = 1; i < n-1; i++) {
42 v_new[i] = v[i] - dt*a*(v[i] - v[i-1])/h;
43 }
44}
45
46void upwind() {
47 if (a > 0)
48 upwindBackward();
49 else
50 upwindForward();
51 /*@ loop invariant 1<=i && i<=n-1;
52 @ loop invariant $forall (int j:1..i-1) v[j] == v_new[j];
53 @ loop assigns i, v[1..n-2];
54 @*/
55 for (int i = 1; i < n-1; i++) {
56 v[i] = v_new[i];
57 }
58}
59
60void main() {
61 $assume(n*h <= 1);
62 $assume(step*dt < 100);
63 $assume($forall (int i:0..n-1) v[i] == u(i*h, step*dt));
64 /*@ loop invariant 0<=i && i<=n;
65 @ loop invariant $forall (int j:0..i-1) v[j] == u1[j];
66 @ loop assigns i, v[0..n-1];
67 @*/
68 for (int i = 0; i < n; i++) {
69 v[i] = u1[i];
70 }
71 upwind();
72 $assert($uniform (int i:1..n-2)
73 (u(i*h, (step+1)*dt)-v[i])/dt - $D[u,{t,1}](i*h, step*dt) - a*$D[u,{x,1}](i*h, step*dt)
74 == $O(dt) + $O(h));
75}
Note: See TracBrowser for help on using the repository browser.