11.1 Functions for initialization

This section contain functions for input data for most of further samples.

MGL code:

func 'prepare1d'
new y 50 3
modify y '0.7*sin(2*pi*x)+0.5*cos(3*pi*x)+0.2*sin(pi*x)'
modify y 'sin(2*pi*x)' 1
modify y 'cos(2*pi*x)' 2
new x1 50 'x'
new x2 50 '0.05-0.03*cos(pi*x)'
new y1 50 '0.5-0.3*cos(pi*x)'
new y2 50 '-0.3*sin(pi*x)'
return

func 'prepare2d'
new a 50 40 '0.6*sin(pi*(x+1))*sin(1.5*pi*(y+1))+0.4*cos(0.75*pi*(x+1)*(y+1))'
new b 50 40 '0.6*cos(pi*(x+1))*cos(1.5*pi*(y+1))+0.4*cos(0.75*pi*(x+1)*(y+1))'
return

func 'prepare3d'
new c 61 50 40 '-2*(x^2+y^2+z^4-z^2)+0.2'
new d 61 50 40 '1-2*tanh((x+y)*(x+y))'
return

func 'prepare2v'
new a 20 30 '0.6*sin(pi*(x+1))*sin(1.5*pi*(y+1))+0.4*cos(0.75*pi*(x+1)*(y+1))'
new b 20 30 '0.6*cos(pi*(x+1))*cos(1.5*pi*(y+1))+0.4*cos(0.75*pi*(x+1)*(y+1))'
return

func 'prepare3v'
define $1 pow(x*x+y*y+(z-0.3)*(z-0.3)+0.03,1.5)
define $2 pow(x*x+y*y+(z+0.3)*(z+0.3)+0.03,1.5)
new ex 10 10 10 '0.2*x/$1-0.2*x/$2'
new ey 10 10 10 '0.2*y/$1-0.2*y/$2'
new ez 10 10 10 '0.2*(z-0.3)/$1-0.2*(z+0.3)/$2'
return

C++ code:

void mgls_prepare1d(mglData *y, mglData *y1, mglData *y2, mglData *x1, mglData *x2)
{
	long n=50;
	if(y)	y->Create(n,3);
	if(x1)	x1->Create(n);
	if(x2)	x2->Create(n);
	if(y1)	y1->Create(n);
	if(y2)	y2->Create(n);
	for(long i=0;i<n;i++)
	{
		double xx = i/(n-1.);
		if(y)
		{
			y->a[i] = 0.7*sin(2*M_PI*xx) + 0.5*cos(3*M_PI*xx) + 0.2*sin(M_PI*xx);
			y->a[i+n] = sin(2*M_PI*xx);
			y->a[i+2*n] = cos(2*M_PI*xx);
		}
		if(y1)	y1->a[i] = 0.5+0.3*cos(2*M_PI*xx);
		if(y2)	y2->a[i] = 0.3*sin(2*M_PI*xx);
		if(x1)	x1->a[i] = xx*2-1;
		if(x2)	x2->a[i] = 0.05+0.03*cos(2*M_PI*xx);
	}
}
//-----------------------------------------------------------------------------
void mgls_prepare2d(mglData *a, mglData *b, mglData *v)
{
	long n=50,m=40;
	if(a)	a->Create(n,m);
	if(b)	b->Create(n,m);
	if(v)	{	v->Create(9);	v->Fill(-1,1);	}
	for(long j=0;j<m;j++)	for(long i=0;i<n;i++)
	{
		double x = i/(n-1.), y = j/(m-1.);
		long i0 = i+n*j;
		if(a)	a->a[i0] = 0.6*sin(2*M_PI*x)*sin(3*M_PI*y)+0.4*cos(3*M_PI*x*y);
		if(b)	b->a[i0] = 0.6*cos(2*M_PI*x)*cos(3*M_PI*y)+0.4*cos(3*M_PI*x*y);
	}
}
//-----------------------------------------------------------------------------
void mgls_prepare3d(mglData *a, mglData *b)
{
	long n=61,m=50,l=40;
	if(a)	a->Create(n,m,l);
	if(b)	b->Create(n,m,l);
	for(long k=0;k<l;k++)	for(long j=0;j<m;j++)	for(long i=0;i<n;i++)
	{
		double x=2*i/(n-1.)-1, y=2*j/(m-1.)-1, z=2*k/(l-1.)-1;
		long i0 = i+n*(j+m*k);
		if(a)	a->a[i0] = -2*(x*x + y*y + z*z*z*z - z*z - 0.1);
		if(b)	b->a[i0] = 1-2*tanh((x+y)*(x+y));
	}
}
//-----------------------------------------------------------------------------
void mgls_prepare2v(mglData *a, mglData *b)
{
	long n=20,m=30;
	if(a)	a->Create(n,m);
	if(b)	b->Create(n,m);
	for(long j=0;j<m;j++)	for(long i=0;i<n;i++)
	{
		double x=i/(n-1.), y=j/(m-1.);
		long i0 = i+n*j;
		if(a)	a->a[i0] = 0.6*sin(2*M_PI*x)*sin(3*M_PI*y)+0.4*cos(3*M_PI*x*y);
		if(b)	b->a[i0] = 0.6*cos(2*M_PI*x)*cos(3*M_PI*y)+0.4*cos(3*M_PI*x*y);
	}
}
//-----------------------------------------------------------------------------
void mgls_prepare3v(mglData *ex, mglData *ey, mglData *ez)
{
	long n=10;
	double z0=0.3;
	if(!ex || !ey || !ez)	return;
	ex->Create(n,n,n);	ey->Create(n,n,n);	ez->Create(n,n,n);
	for(long k=0;k<n;k++)	for(long j=0;j<n;j++)	for(long i=0;i<n;i++)
	{
		double x=2*i/(n-1.)-1, y=2*j/(n-1.)-1, z=2*k/(n-1.)-1;
		long i0 = i+n*(j+k*n);
		double r1 = pow(x*x+y*y+(z-z0)*(z-z0)+0.03,1.5);
		double r2 = pow(x*x+y*y+(z+z0)*(z+z0)+0.03,1.5);
		ex->a[i0]=0.2*x/r1 - 0.2*x/r2;
		ey->a[i0]=0.2*y/r1 - 0.2*y/r2;
		ez->a[i0]=0.2*(z-z0)/r1 - 0.2*(z+z0)/r2;
	}
}
//-----------------------------------------------------------------------------