Help with 2 array function

Hi all,
Would you please help me with this code. Just mention this code is not still complete and I will add more.
Until here in this code. I defined particles numbers as np then in x[np][2] I defined them in (x,y) direction. then in particle velocity function I put those particles in fluid_u which itself is in two direction to get the particles velocity in fluid. But I want to call the particle velocity function between two for loops in line 40 to do calculations (based on euler methods) would you please just tell me how to call the two array particle velocity function in line 40?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  #include <iostream>
#include <cmath>
using namespace std;

int np=100;
int nx=200, ny=200;
int dt=0.1;
double dist;

double euler(double u, double u_previous_time_step){
double a;
	a=(dt/2)*(u_previous_time_step + u);

	return a;
}

void particle_velocity(double **x, double **u, double ***fluid_u){
	int px, py;
    int j,n;

	px=int(x[n][0]);
	py=int(x[n][1]);

	for (j=0; j<2; j++){
	u[n][j] = fluid_u[px][py][j];
	}
}

int main(){
	int n; int i;
	int tmax, t;
	int n1,n2;
	double x[np][2], u[np][2];
	double u_previous_time_step[n][2];

	double fluid_u[nx][ny][2];

	while(t<tmax){
		for (n=0; n<np; n++){
        
			for (i=0; i<2; i++){
				x[n][i]+=euler(u[n][i], u_previous_time_step[n][i]);
				u_previous_time_step[n][i]=u[n][i];
			}
		}
		t=t+dt;
		for(n1=0; n1<np; n1++)
			for(n2=0; n2<np; n2++)
				for (i=0; i<2; i++){
					dist= (x[n2][i])-(x[n1][i]);
			}
	}

	return 0;
}


can this be true?

1
2
3
4
5
6
7
8
  for (n=0; n<np; n++){
        particle_velocity (x, u, fluid_u);
			for (i=0; i<2; i++){
				x[n][i]+=euler(u[n][i], u_previous_time_step[n][i]);
				u_previous_time_step[n][i]=u[n][i];
			}
		}
		t=t+dt;
Last edited on
To me it seems that you have points in 2D coordinate system. In other words, the position of a point is a 2D (math) vector that has x and y components.

It would be more intuitive to not store position as array of two elements, but as a struct that has members. The latter expresses the intent of the code more clearly too. Furthermore, you should avoid raw arrays and pointers and prefer standard library containers and algorithms.

Can you do that?
Thanks for your comment. Yes I have 2D coordinate system, But to be honest I am not that much professional to apply your comment.
As I have too short time to hand in this assignment and develop this code, would you please let us focus on the what I have written?

is the particle_velocity (x, u, fluid_u); expression at line 2 of the second code true?

should I change or add something?
Last edited on
any suggestion?
You say that the code is not complete and that you will add more. How could we know which bits simply wait for you to add and which bits you don't know to add/change? Write the code as complete as possible so that we can focus on real issues.

Is a call to that function correct? There are two ways to be correct. The first is syntax, and the compiler can tell if you have that right. The second is logical correctness. You can write syntactically correct code that makes nothing sensible.

Incidentally, some compiler says:
error: cannot convert 'double (*)[2]' to 'double**' for argument '1'
       to 'void particle_velocity(double**, double**, double***)'

Therefore, you fail already on the syntax. I cannot even comment the logic.

Trivial way around that is to write:
void particle_velocity( const double x[][2], double u[][2], const double fluid_u[][ny][2] )


The snippets below would avoid that particular error and lead to other benefits too, but if you really have no time for that, then these won't help you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <vector>
struct Point {
  double x;
  double y;

  Point& operator+=( const Point& rhs ) {
    x += rhs.x;
    y += rhs.y;
    return *this;
  }
  // other overloaded member functions
};

void particle_velocity( const std::vector<Point> & foo, double **u, double ***fluid_u );

// used:
std::vector<Point> x;

particle_velocity( x, u, fluid_u );
Topic archived. No new replies allowed.