C/C++ [C++] Crank-Nicolson advection of gaussian pulse.

  • Thread starter Thread starter randombill
  • Start date Start date
  • Tags Tags
    Gaussian Pulse
AI Thread Summary
The discussion revolves around implementing the Crank-Nicolson method to solve the advection equation for a Gaussian pulse in C++. The initial condition for the Gaussian pulse is specified as u(x,0) = exp[-100(x-0.5)^2], with parameters such as velocity and timestep defined. The user initially struggles with correctly initializing the Gaussian pulse in the code, particularly in how to incorporate the timestep into the Gaussian equation. After making adjustments, they successfully create a Gaussian function and output both the original and advected Gaussian data for analysis. There is also a mention of concerns regarding the efficiency of the tridiagonal matrix setup used in the code.
randombill
Messages
81
Reaction score
0
I tried to used the Crank-Nicolson method to solve the advection equation for a Gaussian pulse using the code on this page and I'm lost at trying to setup the initial values for the Gaussian which is passed as an array into the advection matrix.

The Gaussian pulse is defined

initial condition: u(x,0) = exp[-100(x-0.5)^2] as on this page written for the Lax method.

The initial velocity is v = 1, timestep = 9.95x10^(-3), N = 200 and C = 2.

In the following code the matrix u is initialized to 202 elements and I try to initialize the matrix u
with the gaussian pulse for each index between 1 and 200 but something is very wrong. For one I'm not sure how to put the timestep into the gaussian equation. I suppose if x/t = v then tv = x and (9.95x10^(-3))*(velocity = 1) = distance then each timestep solved with advect1D would move the Gaussian to the right a small amount.

The thing I'm not sure about is this part in my code

Code:
for (int index = 1; index < 201; index++)
    {
        u(index) = exp(-100*(distance - 0.5)*(distance-0.5));
    }

I initialize every element of u(index) with the same gaussian? Well hopefully someone can help me here's the complete code.

Code:
// Advect1D.cpp

// Function to evolve advection equation in 1-d:

//  du / dt + v du / dx = 0  for  xl <= x <= xh

//  u = 0  at x=xl and x=xh

// Array u assumed to be of extent N+2.

// Now, ith element of array corresponds to

//  x_i = xl + i * dx    i=0,N+1

// Here, dx = (xh - xl) / (N+1) is grid spacing.

// Function evolves u by single time-step.

// C = v dt / dx, where dt is time-step.

// Uses Crank-Nicholson scheme.

#include <blitz/array.h>
#include <iostream>
#include <cmath>

using namespace blitz;

void Tridiagonal (Array<double,1> a, Array<double,1> b, Array<double,1> c, Array<double,1> w, Array<double,1>& u);
void Advect1D (Array<double,1>& u, double C);

int main()
{
    Array<double,1> u(202);
    u = 0;

    double distance = 9.95e-3;

    for (int index = 1; index < 201; index++)
    {
        u(index) = exp(-100*(distance - 0.5)*(distance-0.5));
    }

    double C = 2;

    Advect1D ( u,  C);
    std::cout<<u<<std::endl;

    return 0;
}
void Advect1D (Array<double,1>& u, double C)
{
  // Find N. Declare local arrays.
  int N = u.extent(0) - 2;
  std::cout<<"N = "<<N<<std::endl;

  Array<double,1> a(N+2), b(N+2), c(N+2), w(N+2);

  a = 0;
  b = 0;
  c = 0;
  w = 0;

  // Initialize tridiagonal matrix
  for (int i = 2; i <= N;   i++)
    a(i) = - 0.25 * C;

  for (int i = 1; i <= N; i++)
    b(i) = 1.;

  for (int i = 1; i <= N-1; i++)
    c(i) = + 0.25 * C;

  // Initialize right-hand side vector
  for (int i = 1; i <= N; i++)
    w(i) = u(i) - 0.25 * C * (u(i+1) - u(i-1));

  std::cout<<a<<std::endl;
  std::cout<<b<<std::endl;
  std::cout<<c<<std::endl;
  std::cout<<w<<std::endl;
  // Invert tridiagonal matrix equation
  Tridiagonal (a, b, c, w, u);

  // Calculate i=0 and i=N+1 values
  u(0) = 0.;
  u(N+1) = 0.;
}
void Tridiagonal (Array<double,1> a, Array<double,1> b, Array<double,1> c,  Array<double,1> w, Array<double,1>& u)
{
  // Find N. Declare local arrays.
  int N = a.extent(0) - 2;
  Array<double,1> x(N), y(N);

  // Scan up diagonal from i = N to 1
  x(N-1) = - a(N) / b(N);
  y(N-1) = w(N) / b(N);

  for (int i = N-2; i > 0; i--)
  {
      x(i) = - a(i+1) / (b(i+1) + c(i+1) * x(i+1));
      y(i) = (w(i+1) - c(i+1) * y(i+1)) / (b(i+1) + c(i+1) * x(i+1));
  }

  x(0) = 0.;
  y(0) = (w(1) - c(1) * y(1)) / (b(1) + c(1) * x(1));

  // Scan down diagonal from i = 0 to N-1
  u(1) = y(0);
  for (int i = 1; i < N; i++)
    u(i+1) = x(i) * u(i) + y(i);
}
 
Technology news on Phys.org
I made some changes to the code and I think I solved it but I'm not sure the solution makes sense. Basically I create a Gaussian pulse function with

width (standard deviation) = 0.247
height = 1;
position = .78

the actual width of the Gaussian is 1.55 from tail to tail and I divide that number by 200 to get the space discretization which is used to find the value of the Gaussian at specific intervals between 0 and 1.55. I output the original Gaussian and the advection equation into a dat file and use gnuplot. Now I'm not sure if the solution I found is correct. I think it makes sense, any ideas?

Heres the source and the gnuplot .plt file below it plus a photo of the beast.

Code:
// Advect1D.cpp

// Function to evolve advection equation in 1-d:

//  du / dt + v du / dx = 0  for  xl <= x <= xh

//  u = 0  at x=xl and x=xh

// Array u assumed to be of extent N+2.

// Now, ith element of array corresponds to

//  x_i = xl + i * dx    i=0,N+1

// Here, dx = (xh - xl) / (N+1) is grid spacing.

// Function evolves u by single time-step.

// C = v dt / dx, where dt is time-step.

// Uses Crank-Nicholson scheme.

#include <blitz/array.h>
#include <iostream>
#include <cmath>
#include <fstream>

using namespace blitz;

void Tridiagonal (Array<double,1> a, Array<double,1> b, Array<double,1> c, Array<double,1> w, Array<double,1>& u);
void Advect1D (Array<double,1>& u, double C);

int main()
{
    Array<double,1> u(202);
    std::ofstream gaussian;

    gaussian.open("gaussian.dat");

    u = 0;
    //double distance = 9.95e-3;

    double element = 1.55/200;//1.55 is the width of the gaussian
    std::cout<<"element: "<<element<<std::endl;

    double dx = 0;

    double term = 0;
    double numerator = 0;
    double denominator = 0;

    for (int index = 1; index < 201; index++)
    {
        numerator = -1*pow(dx - .78,2);
        denominator = 2*pow(0.247,2);
        term = numerator/denominator;
        u(index) = exp(term);

        gaussian<<u(index)<<"   "<<dx<<"\n";

        dx += element;
    }


    double C = 2;

    Advect1D ( u,  C);
    std::cout<<u<<std::endl;
    dx = 0;

    std::ofstream gaussian_pde;
    gaussian_pde.open("gaussian_pde.dat");

    for (int index = 1; index < 201; index++)
    {
        gaussian_pde<<u(index)<<"   "<<dx<<"\n";
        dx += element;
    }

    gaussian.close();
    gaussian_pde.close();

    return 0;
}
void Advect1D (Array<double,1>& u, double C)
{
  // Find N. Declare local arrays.
  int N = u.extent(0) - 2;
  std::cout<<"N = "<<N<<std::endl;

  Array<double,1> a(N+2), b(N+2), c(N+2), w(N+2);

  a = 0;
  b = 0;
  c = 0;
  w = 0;

  // Initialize tridiagonal matrix
  for (int i = 2; i <= N;   i++)
    a(i) = - 0.25 * C;

  for (int i = 1; i <= N; i++)
    b(i) = 1.;

  for (int i = 1; i <= N-1; i++)
    c(i) = + 0.25 * C;

  // Initialize right-hand side vector
  for (int i = 1; i <= N; i++)
    w(i) = u(i) - 0.25 * C * (u(i+1) - u(i-1));

  std::cout<<a<<std::endl;
  std::cout<<b<<std::endl;
  std::cout<<c<<std::endl;
  std::cout<<w<<std::endl;
  // Invert tridiagonal matrix equation
  Tridiagonal (a, b, c, w, u);

  // Calculate i=0 and i=N+1 values
  u(0) = 0.;
  u(N+1) = 0.;
}
void Tridiagonal (Array<double,1> a, Array<double,1> b, Array<double,1> c,  Array<double,1> w, Array<double,1>& u)
{
  // Find N. Declare local arrays.
  int N = a.extent(0) - 2;
  Array<double,1> x(N), y(N);

  // Scan up diagonal from i = N to 1
  x(N-1) = - a(N) / b(N);
  y(N-1) = w(N) / b(N);

  for (int i = N-2; i > 0; i--)
  {
      x(i) = - a(i+1) / (b(i+1) + c(i+1) * x(i+1));
      y(i) = (w(i+1) - c(i+1) * y(i+1)) / (b(i+1) + c(i+1) * x(i+1));
  }

  x(0) = 0.;
  y(0) = (w(1) - c(1) * y(1)) / (b(1) + c(1) * x(1));

  // Scan down diagonal from i = 0 to N-1
  u(1) = y(0);
  for (int i = 1; i < N; i++)
    u(i+1) = x(i) * u(i) + y(i);
}


Code:
set output 'gaussian.png'
set key bmargin left horizontal Right noreverse enhanced autotitles box linetype -1 linewidth 1.000
plot 'gaussian.dat' using 2:1 with lines ,'gaussian_pde.dat' using 2:1 with lines

attachment.php?attachmentid=59182&stc=1&d=1370049870.png
 

Attachments

  • gaussian.png
    gaussian.png
    4.3 KB · Views: 1,066
What I find irritating at a first glance is that the arrays for the a,b,c diags are of equal size, thus two of them are padded; I'd doubt this improves efficiency or simplifies usage significantly.

---

Furthermore:

Let T be your Matrix with a,b,c diags.

From http://farside.ph.utexas.edu/teaching/329/lectures/node92.html
Code:
// Invert tridiagonal matrix equation
  Tridiagonal (a, b, c, w, u);

i take it one should solve

T * u = w

for u by inverting T in the Tridiagonal function.

boost::ublas has an adaptor for banded matrices
http://www.boost.org/doc/libs/1_53_0/libs/numeric/ublas/doc/banded.htm#banded_matrix

and LAPACK of course has this as well
http://www.netlib.org/lapack/lug/node125.html

Have you checked the correctness of your own Tridiagonal function with either of them?

Regards, Solkar
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...

Similar threads

Back
Top