0

I want to solve u(x,t)=7.5-sin(x) numerically over the domain [0,2pi]. The problem has periodic boundary conditions u(0)=u(2pi)=7.5

I want to create a 1-D grid consisting of N grid cells where each grid cell contains some information X and U. For each cell, I've created pointers that point to the neighbour cells to connect the grid up, and since we have periodic boundary conditions I'd like to connect the first and last cell together. A for loop is used to traverse the grid one cell at a time, and store the grid cells in a vector cells.

I have a couple of problems here:

  1. interp_u and interp_x both return 0, it seems like they are using the initialised vectors with all values 0. How can I input X and U for cell[i] into the linear interpolated x and u?

  2. It's giving me a segmentation fault, I'm not too sure what the problem is.

Cheers!

class Grid {
  public:
    // Pointers to neighbour cells
  Grid * LeftNeighbour;
  Grid * RightNeighbour;

  std::vector < double > X;
  std::vector < double > U;

  Grid() {
    // Initialises vectors
    std::vector < double > X(2);
    std::vector < double > U(2);
  }

  double interp_x(double t) {
    return X[0] * (1 / 2) * (1 - t) + X[1] * (1 / 2) * (1 + t);
  }

  double interp_u(double t) {
    return U[0] * (1 / 2) * (1 - t) + U[1] * (1 / 2) * (1 + t);
  }
};

int main() {
  const long double pi = 3.1415926535897932384626433832795;
  std::cout.precision(10);
  int N = 5; //number of cells
  std::vector < Grid > cells(N); //initialises vector of cells

  Grid * RightNeighbour, * LeftNeighbour;

  for (int i = 0; i < N; i++) {
    cells[i].X[0] = 0 + ((2 * pi) / N) * i;
    cells[i].X[1] = cells[i].X[0] + ((2 * pi) / N); //SEGMENTATION FAULT

    cells[i].U[0] = 7.5 - sin(cells[i].X[0]);
    cells[i].U[1] = 7.5 - sin(cells[i].X[1]);

    std::cout << "u(t=0) = " << cells[i].interp_u(0) << std::endl; //linear interpolation

    if (i == 0) { //first cell
      LeftNeighbour = & cells[N - 1];
      RightNeighbour = & cells[i + 1];
    }

    if ((i % (N - 1)) != 0) {
      LeftNeighbour = & cells[i - 1];
      RightNeighbour = & cells[i + 1];
    }

    if (i == N - 1) { //last cell
      LeftNeighbour = & cells[i - 1];
      RightNeighbour = & cells[0];
    }
  }
  return 0;
}
kiner_shah
  • 3,939
  • 7
  • 23
  • 37
  • `std::vector X(2);` does not initialize the member `X`, but creates a new local variable in the constructor with the same name. The member `X` is still empty. – mch Jan 02 '23 at 08:43
  • How would I pass `X` and `U` for each grid cell in the for loop into the class so I get the correct interpolated values? Thank you! – Achuan Chen Jan 02 '23 at 08:49

1 Answers1

2

This is wrong

Grid()
{
// Initialises vectors
std::vector<double> X(2);
std::vector<double> U(2);
}

should be

Grid() : X(2), U(2) // Initialises vectors
{
}

Your version create two local variables called X and U and initialises those. The two variables have the same names as your class variables but they are different variables.

Instead use an initialiser list to initialise your class variables.

EDIT

So with the pointers, you have this in main

Grid * RightNeighbour, * LeftNeighbour;

and this

  LeftNeighbour = & cells[N - 1];
  RightNeighbour = & cells[i + 1];

etc. etc.

I'm guessing that what you really meant to do is this

  cells[i].LeftNeighbour = & cells[N - 1];
  cells[i].RightNeighbour = & cells[i + 1];

etc. etc.

Now you can delete this line in main

Grid * RightNeighbour, * LeftNeighbour;

This is basically the same error as before, you have class variables RightNeighbour and LeftNeighbour but instead of using those, you declared some different variables with exactly the same name in main.

john
  • 85,011
  • 4
  • 57
  • 81
  • How could I pass the ```X``` and ```U``` for ```cell[i]``` into the class? Also, how could I resolve the segfault error? Could this error be that I haven't initialised the pointers? Thanks John! – Achuan Chen Jan 02 '23 at 09:52
  • @AchuanChen `cell[i]` is a `Grid` with `X` and `U`, so I don't understand this question, you can't pass something into a class if it is already in the class. With this change I didn't get any segfaults. – john Jan 02 '23 at 10:28
  • @AchuanChen All your pointers are initialised, but you never use any of your pointers. So they cannot be the cause of the segfault, but what you are expecting these pointers to do I don't know. Also you have the same error with the pointers as you did with `X` and `U`. You have pointers in your `Grid` class, but you also have pointers **with exactly the same names** in your `main` function. I guessing that you didn;t mean to do that. – john Jan 02 '23 at 10:29
  • @AchuanChen I've edited the question with what I think are some more errors in your code. – john Jan 02 '23 at 10:38
  • Cheers appreciate it John! I'm new to c++ and misunderstood the concept of classes and pointers. This is exactly what I needed. Thank you! – Achuan Chen Jan 03 '23 at 15:51