void u_init ( int n, double x[], double t, double u[] ) /******************************************************************************/ /* Purpose: U_INIT sets the initial condition for U. Licensing: This code is distributed under the GNU LGPL license. Modified: 19 August 2010 Author: John Burkardt Parameters: Input, int N, the number of nodes. Input, double X[N], the coordinates of the nodes. Input, double T, the current time. Output, double U[N], the initial values U(X,T). */ { int i; double pi = 3.141592653589793; double q; double r; double s; double ua; double ub; ua = u_a ( x[0], t ); ub = u_b ( x[n-1], t ); q = 2.0 * ( ua - ub ) / pi; r = ( ua + ub ) / 2.0; /* S can be varied. It is the slope of the initial condition at the midpoint. */ s = 1.0; for ( i = 0; i < n; i++ ) { u[i] = - q * atan ( s * ( 2.0 * x[i] - x[0] - x[n-1] ) / ( x[n-1] - x[0] ) ) + r; } return; }
// interpolate displacements in triangle with given (s,t) coords, // where (0,0) is node a, (1,0) is node b and (0,1) is node c. inline Eigen::Vector3d interpInTri( double s, double t, double u_a_x, double u_a_y, double u_a_z, double u_b_x, double u_b_y, double u_b_z, double u_c_x, double u_c_y, double u_c_z ) const{ Eigen::Vector3d u_a(u_a_x, u_a_y, u_a_z), u_b(u_b_x, u_b_y, u_b_z), u_c(u_c_x, u_c_y, u_c_z); return (1.0-s-t)*u_a + s*u_b + t*u_c; }
int main ( void ) /******************************************************************************/ /* Purpose: FD1D_BURGERS_LEAP solves the nonviscous Burgers equation using leapfrogging. Licensing: This code is distributed under the GNU LGPL license. Modified: 19 August 2010 Author: John Burkardt Parameters: None */ { double a; double b; double dt; double dx; int i; int ihi; int ilo; int n; int step; int step_num; double t; double t_init; double t_last; double *uc; double *un; double *uo; double *x; timestamp ( ); printf ( "\n" ); printf ( "FD1D_BURGERS_LEAP:\n" ); printf ( " C version\n" ); printf ( " Solve the non-viscous time-dependent Burgers equation,\n" ); printf ( " using the leap-frog method.\n" ); printf ( "\n" ); printf ( " Equation to be solved:\n" ); printf ( "\n" ); printf ( " du/dt + u * du/dx = 0\n" ); printf ( "\n" ); printf ( " for x in [ a, b ], for t in [t_init, t_last]\n" ); printf ( "\n" ); printf ( " with initial conditions:\n" ); printf ( "\n" ); printf ( " u(x,o) = u_init\n" ); printf ( "\n" ); printf ( " and boundary conditions:\n" ); printf ( "\n" ); printf ( " u(a,t) = u_a(t), u(b,t) = u_b(t)\n" ); /* Set and report the problem parameters. */ n = 21; a = -1.0; b = +1.0; dx = ( b - a ) / ( double ) ( n - 1 ); step_num = 30; t_init = 0.0; t_last = 3.0; dt = ( t_last - t_init ) / ( double ) ( step_num ); printf ( "\n" ); printf ( " %f <= X <= %f\n", a, b ); printf ( " Number of nodes = %d\n", n ); printf ( " DX = %f\n", dx ); printf ( "\n" ); printf ( " %f <= T <= %f\n", t_init, t_last ); printf ( " Number of time steps = %d\n", step_num ); printf ( " DT = %f\n", dt ); uc = ( double * ) malloc ( n * sizeof ( double ) ); un = ( double * ) malloc ( n * sizeof ( double ) ); uo = ( double * ) malloc ( n * sizeof ( double ) ); x = r8vec_even ( n, a, b ); printf ( "\n" ); printf ( " X:\n" ); printf ( "\n" ); for ( ilo = 0; ilo < n; ilo = ilo + 5 ) { ihi = i4_min ( ilo + 5, n - 1 ); for ( i = ilo; i <= ihi; i++ ) { printf ( " %14f", x[i] ); } printf ( "\n" ); } /* Set the initial condition, and apply boundary conditions to first and last entries. */ step = 0; t = t_init; u_init ( n, x, t, un ); un[0] = u_a ( x[0], t ); un[n-1] = u_b ( x[n-1], t ); report ( step, step_num, n, x, t, un ); /* Use Euler's method to get the first step. */ step = 1; t = ( ( double ) ( step_num - step ) * t_init + ( double ) ( step ) * t_last ) / ( double ) ( step_num ); for ( i = 0; i < n; i++ ) { uc[i] = un[i]; } for ( i = 1; i < n - 1; i++ ) { un[i] = uc[i] - dt * uc[i] * ( uc[i+1] - uc[i-1] ) / 2.0 / dx; } un[0] = u_a ( x[0], t ); un[n-1] = u_b ( x[n-1], t ); report ( step, step_num, n, x, t, un ); /* Subsequent steps use the leapfrog method. */ for ( step = 2; step <= step_num; step++ ) { t = ( ( double ) ( step_num - step ) * t_init + ( double ) ( step ) * t_last ) / ( double ) ( step_num ); for ( i = 0; i < n; i++ ) { uo[i] = uc[i]; uc[i] = un[i]; } for ( i = 1; i < n - 1; i++ ) { un[i] = uo[i] - dt * uc[i] * ( uc[i+1] - uc[i-1] ) / dx; } un[0] = u_a ( x[0], t ); un[n-1] = u_b ( x[n-1], t ); report ( step, step_num, n, x, t, un ); } free ( uc ); free ( un ); free ( uo ); free ( x ); /* Terminate. */ printf ( "\n" ); printf ( "FD1D_BURGERS_LEAP:\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; }