Exemplo n.º 1
0
mat Scheme::implicitScheme(int nSteps, double tSteps, double(*u_s)(double))
{
	//input: nSteps (# of interior points), time, tSteps, u_s(x)

	double deltaX = 1.0 / (nSteps + 1);
	double deltaT = 1.0 / tSteps;
	double alpha = deltaT / pow(deltaX,2);

	vec v(nSteps), vNext(nSteps), u(nSteps);
	int printIndex = tSteps / 10;
	mat M(printIndex, nSteps);
	int index = 0;

	for( int i = 0; i < nSteps; i++)
	{
		double x = (i + 1) * deltaX;
		vNext[i] = v[i] = -u_s(x);
	}
	// Boundary conditions
	v[0] = vNext[0] = v[nSteps] = vNext[nSteps] = 0;
	
	double a = -alpha;        // diagonal element
	double b = 1 + 2*alpha;   // off-diagonal element

	Solver solv = Solver();
	for(int t = 1; t <= tSteps; t++)
	{
		solv.tridiagonalSolver(a, b, v, vNext);
		
		// Initial conditions
		vNext[0] = vNext[nSteps] = 0;
		v = vNext;

		for (int i = 0; i < nSteps; i++)
		{
			double x = (i + 1) * deltaX;
			u[i] = v[i] + u_s(x);
		}

		// Print to file !
		if (t % 10 == 0)
		{
			for (int i = 0; i < nSteps; i++)
				M(index, i) = u[i];
			index++;
		}
	}

	for (int i = 0; i < printIndex; i++)
	{
		for (int j = 0; j < nSteps; j++)
			printf("%f \t", M(i, j));
	}

	return M;	
}
Exemplo n.º 2
0
//-----------------------------------------------------------------------------
// Purpose: 
// Input  : time - 
//			type - 
// Output : float
//-----------------------------------------------------------------------------
float CSentence::GetIntensity( float time, float endtime )
{
	float zeroValue = 0.5f;
	
	int c = GetNumSamples();
	
	if ( c <= 0 )
	{
		return zeroValue;
	}
	
	int i;
	for ( i = -1 ; i < c; i++ )
	{
		CEmphasisSample *s = GetBoundedSample( i, endtime );
		CEmphasisSample *n = GetBoundedSample( i + 1, endtime );
		if ( !s || !n )
			continue;

		if ( time >= s->time && time <= n->time )
		{
			break;
		}
	}

	int prev = i - 1;
	int start = i;
	int end = i + 1;
	int next = i + 2;

	prev = max( -1, prev );
	start = max( -1, start );
	end = min( end, GetNumSamples() );
	next = min( next, GetNumSamples() );

	CEmphasisSample *esPre = GetBoundedSample( prev, endtime );
	CEmphasisSample *esStart = GetBoundedSample( start, endtime );
	CEmphasisSample *esEnd = GetBoundedSample( end, endtime );
	CEmphasisSample *esNext = GetBoundedSample( next, endtime );

	float dt = esEnd->time - esStart->time;
	dt = clamp( dt, 0.01f, 1.0f );

	Vector vPre( esPre->time, esPre->value, 0 );
	Vector vStart( esStart->time, esStart->value, 0 );
	Vector vEnd( esEnd->time, esEnd->value, 0 );
	Vector vNext( esNext->time, esNext->value, 0 );

	float f2 = ( time - esStart->time ) / ( dt );
	f2 = clamp( f2, 0.0f, 1.0f );

	Vector vOut;
	Catmull_Rom_Spline( 
		vPre,
		vStart,
		vEnd,
		vNext,
		f2, 
		vOut );

	float retval = clamp( vOut.y, 0.0f, 1.0f );
	return retval;
}
Exemplo n.º 3
0
mat Scheme::explicitScheme(int nSteps, double tSteps, double(*u_s)(double))
{
	//input: nSteps , time, u_s(x)

	double deltaX = 1.0 / (float)(nSteps + 1);
	double alpha = 0.4;
	double deltaT = alpha * pow(deltaX, 2);
	//double 
	tSteps = 1.0 / deltaT;

	vec v(nSteps), vNext(nSteps), u(nSteps);
	int printIndex = tSteps / 10;
	mat M(printIndex, nSteps);
	int index = 0;

	//Initialization 
	for (int i = 1; i < nSteps; i++)
	{
		double x = (i + 1) * deltaX;
		v(i) = -u_s(x);
	}

	// Boundary conditions
	v[0] = vNext[0] = 0;
	v[nSteps] = vNext[nSteps] = 0;
	//
	for(int t = 1; t <= tSteps; t ++)
	{
		if ((t - 1) % 204 == 0)
		{
			for (int i = 0; i < nSteps; i++)
				printf("%f \t", v(i));
			printf("\n");
		}

		for(int i = 1; i < nSteps; i++ )
		{
			vNext[i] = (1 - 2 * alpha) * v[i];
			double truc = vNext[i]; // XX
			if( i > 0 ) 
				vNext[i] += alpha * v[i-1]; // else += 0
			if( i < nSteps ) 
				vNext[i] += alpha * v[i+1]; // else += 0
		}
		v = vNext;

		/*if ((t - 1) % 204 == 0)
		{
			for (int i = 0; i < nSteps; i++)
				printf("%f \t", v(i));
			printf("\n");
		}*/

		for (int i = 0; i < nSteps; i++)
		{
			double x = (i + 1) * deltaX;
			u[i] = v[i] + u_s(x);
		}


		// Print to file !
		if (t % 10 == 0)
		{
			for (int i = 0; i < nSteps; i++)
				M(index, i) = u[i];
			index++;
		}
	}
	
	for (int i = 0; i < printIndex; i++)
	{
		for (int j = 0; j < nSteps; j++)
			printf("%f \t", M(i, j));
	}

	return M;
}