sandbox/Antoonvh/lineintegral.c
#Evaluate a line integral on a 2D grid.
#include "grid/quadtree.h"
#define func(a,b) (sin(a)*cos(b))
#define funcint(a,b) (sin(a)*(sin(Y0+L0)-sin(y-(Delta/2))))
scalar s[],ints[];
int startlevel = 2;
int main(){
=2.*M_PI;
L0=Y0=-L0/2;
X0for (int i=1;i<7;i++){
// Test to function on several grids
(1<<(startlevel+i));
init_gridrefine(level<=startlevel+i && (x+y)<-1.);
unrefine(level>=startlevel+i-1 && (x+y)>1.);
// Define the function (2nd-order accurate)
foreach()
[]=func(x,y);
s
boundary({s}); //<- This is important
// Integrate to the top
foreach(){
//Store height of this cell;
double yt=y;
double yh=y;
double integral=0.;
while(yh<(Y0+L0)){
= locate(x,yh);
Point point +=interpolate(s,x,y)*Delta;
integral +=Delta;
yh}
= locate(x,yt);
Point point []=integral;
ints}
double err=0;
foreach()
+=fabs(ints[]-funcint(x,y))*sq(Delta);
errstatic FILE * fp = NULL; if (!fp) fp = fopen("err.dat","w");
fprintf(fp,"%d\t%g\n",i,err);
}
}
Results
The function appears to be first-order accurate:
set xr [ 0.5:6.5]
set logscale y
plot 'err.dat' u 1:2 ,\
**(-x) 2