I have the following code for my program
int main(void)
{
int i,size;
float vel_x[size],vel_y[size],vel_x0[size],vel_y0[size],rho[size],rho0[size];
float *ux, *vy, *ux0, *vy0;
float *r, *r0;
struct fdparam fdparam_1;
printf("Enter the number of grid points: \t");
scanf("%d", &fdparam_1.N);
printf("Enter the maximum number of iterations: \t");
scanf("%d", &fdparam_1.MAXIT);
printf("Enter the value for time domain: \t");
scanf("%f", &fdparam_1.t_domain);
printf("Enter the time step and density of the fluid: \t \t");
scanf("%f\t%f", &fdparam_1.Dt, &fdparam_1.dens);
printf("Enter the diffusion coefficient and viscosity: \t \t");
scanf("%f\t%f",&fdparam_1.diff, &fdparam_1.mu);
printf("The parameters are N=%d, MAXIT=%d, t_domain=%f, Dt=%f, diff=%e, mu=%e, dens=%f \n",fdparam_1.N, fdparam_1.MAXIT, fdparam_1.t_domain, fdparam_1.Dt, fdparam_1.diff, fdparam_1.mu, fdparam_1.dens);
size=(fdparam_1.N+2)*(fdparam_1.N+2);
printf("The size is %d \n",size );
r = (float*) calloc (size,sizeof(float));
r0 = (float*) calloc (size,sizeof(float));
ux = (float*) calloc (size,sizeof(float));
vy = (float*) calloc (size,sizeof(float));
ux0 = (float*)calloc (size,sizeof(float));
vy0 = (float*)calloc (size,sizeof(float));
var_init(fdparam_1.N,r0,ux0,vy0,fdparam_1.dens);
// t=0;
// Solver functions
// while (t<fdparam_1.t_domain){
velocity_solve(fdparam_1.N,ux,vy,ux0,vy0,fdparam_1.Dt,fdparam_1.mu,fdparam_1.MAXIT); //calculates ux and vy to be used in the density solver
density_solve(fdparam_1.N,r,r0,ux,vy,fdparam_1.Dt,fdparam_1.diff,fdparam_1.MAXIT); //uses ux and vy calculated from Navier Stokes in the velocity solver to calculate density r
// t+=fdparam_1.Dt
// }
}
//velocity solver function
void velocity_solve(int n, float *u, float *v, float *u0, float *v0, float dt, float m, int MAXITER)
{
int i,j;
add_source(n,u,u0,dt); add_source(n,v,v0,dt);
swap(u0,u); swap(v0,v);
diffusion(n,u,u0,dt,m,MAXITER); diffusion(n,v,v0,dt,m,MAXITER);
projection(n,u,v,u0,v0,MAXITER);
swap(u0,u); swap(v0,v);
advection(n,u,u0,u0,v0,dt); advection(n,v,v0,u0,v0,dt);
projection(n,u,v,u0,v0,MAXITER);
printf("Printing velocities now \n");
for (i=0; i<=n+1; i++){
for (j=0;j<=n+1;j++){
printf("%f \t %f \n",u[ix(i,j)],v[ix(i,j)]);
}
}
}
// density solver function
void density_solve(int n, float *x, float *x0, float *u, float *v, float dt, float diff, int MAXITER)
{
int i,j;
add_source(n,x,x0,dt);
swap(x0,x);
diffusion(n,x,x0,dt,diff,MAXITER);
swap(x0,x);
advection(n,x,x0,u,v,dt);
printf("Printing density now \n");
for (i=0;i<=n+1;i++){
for (j=0; j<=n+1;j++){
printf("%f \t",x[ix(i,j)]);
}
}
printf("\n");
}
The main problem that I am facing while executing this code is that I get a segmentation fault when I try to run both the functions velocity_solve
and density_solve
together. The two functions are executed properly when they are run individually i.e. when I comment velocity_solve
, density_solve
runs fine and vice-versa.
I am pretty sure there is nothing wrong with the functions being used in the velocity_solve
and density_solve
routines because neither of the two routines would give individual outputs. I suspect there is something going wrong when the two functions are supposed to interact, i.e the output of velocity_solve
needs to be used in density_solve
.
I tried initializing the size variable but then it doesn't make a difference at all. But then I realized that I do not use the arrays that are defined so I commented them out, instead I use the pointers for which I have already allocated memory, then why does it still give a segmentation fault when I try to call both the functions together in main function?
Can someone suggest what is going wrong in the code?