-2

The equations are here Hi! I tried to solve 3 pde equations by using the FD method in C++, but I do not know why I got half of the exact answer at all time. These equations are dependency variable. I have problem with g(x,y) and g(y,x). When I delete g(y,x) in eq 3, the result does not change. But when I delete g(x,y), I got zero, so I think we need to do something for dependency variables. I do not know. I hope to get help

if(g[i][k][j]!=g[k][i][j] && i!=k)

     u[i][k][j+1]=u[i][k][j]*(1.0 - 2.0*dt)
    +(dt/(dx*dx))*(g[i+1][k][j]-  2.0*g[i][k][j]+g[i-1][k][j])
    +(dt/(dx*dx))*(g[k+1][i][j]-  2.0*g[k][i][j]+g[k-1][i][j])
      +(dt/(dy*dy))*(g[i][k+1][j]-2.0*g[i][k][j]+g[i][k-1][j])
     +(dt/(dy*dy))*(g[k][i+1][j]-2.0*g[k][i][j]+g[k][i-1][j]); //(eq 1)



      g[i][k][j+1]=g[i][k][j]*(1 - dt)
       +dt*u[i][k][j]
       +(dt/(dy*dy))*(v[i][k+1][j]-2*v[i][k][j]+v[i][k-1][j])
       +(dt/(dx*dx))*(v[i+1][k][j]-2*v[i][k][j]+v[i-1][k][j]); //(eq 2)


     v[i][k][j+1]=v[i][k][j]+(g[k][i][j] + g[i][k][j])*dt; //(eq 3)
M.K
  • 1,464
  • 2
  • 24
  • 46
john
  • 15
  • 3
  • 7
    It is impossible for us to help you if you do not argument your question properly and give us a bit of context and describe well what is the issue that you are having. – Davide Spataro Nov 06 '18 at 10:49
  • You text asks about `g(x,y)` which looks like a function call, and your code has `g[i][k][j]` which looks like an array lookup. `x` and `y` are not even present. What are **each** of the names in your code things meant to represent? – Caleth Nov 06 '18 at 11:33
  • we have i represent x and k represent y and j represent time. g(x,y) and g(y,x) are symmetric – john Nov 06 '18 at 12:00
  • 1
    This code is unreadable and hard to understand. Create class that holds your multi dimensionnal arrays of datas. Define a function for each basic operation before combining them into a complicated formulas. Since you tagged C++, use `std::array` and `at()` instead of `[]`operator so you know you are not out of bounds. Last but not least, when you have complicated formulas like that , explicit them in the comments so anyone can verify you correclty implemented your formula. – Clonk Nov 06 '18 at 12:24
  • void H::allocate() { int i,j,k; for(i=0; i<2*nx+1; i++) { v.push_back(vector >()); u.push_back(vector >()); g.push_back(vector >()); for(k=0; k<2*nx+2; k++) { v[i].push_back(vector()); u[i].push_back(vector()); g[i].push_back(vector()); for(j=0; j – john Nov 06 '18 at 12:45
  • i use vector 2D – john Nov 06 '18 at 12:46

1 Answers1

2

Guessing from your picture and code, I think you may be missing a pair of braces

if(i!=k && g[i][k][j]!=g[k][i][j]) {
    u[i][k][j+1] = ...;
    g[i][k][j+1] = ...;
    v[i][k][j+1] = ...;
}

Note also that the comparison g[i][k][j]!=g[k][i][j] is likely to fail if the type is float or double (or complext<float>, complex<double> etc), see this post.

Walter
  • 44,150
  • 20
  • 113
  • 196