(CFD) Problem with C code for 1D linear convection equation

Click For Summary
The C program aims to solve a 1D linear advection equation using a numerical scheme, but it encounters issues with incorrect output values during the second for loop. The initial conditions are set up in an array, where values are defined as 2 between 0.5 and 1, and 1 elsewhere. The program uses forward difference in time and backward difference in space for the numerical update. However, there are errors in the implementation, particularly in how the arrays are updated and printed. The output of `u0_array` shows bizarre numbers due to incorrect indexing and logic in the loop. A working Python script is provided for comparison, which correctly implements the same logic and outputs expected results. The Python version highlights the correct handling of array indices and copying values between iterations, which may help debug the C code.
mcgrane5
Messages
1
Reaction score
0
TL;DR
Hi im writing a c program on linear convection in 1D and im having problems with my final for loop. It returns bizarre results and i dont know why

I have the same problem coded in python and it works perfectly. Could anyone offer a fix to my code. Its only the last for loop where im having problems. I have attached my C code below as well as the working python script to help anyone debug
This is my c program below. Again everything works as expected up until the highlighted lines or the second for loop. When i run my program to
output the arrays contents i get bizarre numbers that are not correct.

[CODE lang="c" title="1D linear convection C code" highlight="63-68"]# include <math.h>
# include <stdlib.h>
# include <stdio.h>
# include <time.h>int main ( )
{
//eqyation to be solved 1D linear advection -- du/dt + c(du/dx) = 0
//initial conditions u(x,0) = u0(x)

//after discretisation using forward difference time and backward differemce space
//update equation becomes u = u0 - c * dt/dx * (u0 - u[i - 1]);
FILE *fpointer = NULL;
fpointer = fopen("bbb.txt", "w");

int nx = 41; //num of grid points

double dx = 2.0 / (nx - 1.0); //magnitude of the spacing between grid points

int nt = 25;//nt is the number of timesteps

double dt = 0.25; //the amount of time each timestep covers

int c = 1; //assume wavespeed
//double u0_array;

//set up our initial conditions. The initial velocity u_0
//is 2 across the interval of 0.5 <x < 1 and u_0 = 1 everywhere else.
//we will define an array of ones
double* u0_array = (double*)calloc(nx, sizeof(double));

for (int i = 0; i < nx-1; i++)
{
if (i * dx >= 0.5 && i * dx <= 1)
{
u0_array = 2;
}else
{
u0_array = 1;
}

}

//test code to make sure array is populated correctly
for (int i = 0; i < nx; i++)
{
//u0_array = 2;
//printf("%f, ", u0_array);

}

//make a temporary array that allows us to store the solution
double* usol_array = (double*)calloc(nx, sizeof(double));

//apply numerical scheme forward difference in
//time an backward difference in space
for (int i = 0; i < nt; i++)
{
usol_array = u0_array;

for (int j = 0; j < nx-1; j++)
{
//usol_array = u0_array;
u0_array[j] = usol_array[j] - c * dt/dx * (usol_array[j] - usol_array[j - 1]);
printf("%f\n", u0_array);
}
//first loop iterates through each time step
}


return EXIT_SUCCESS;
}

[/CODE]

BELOW IS THE WORKING PYTHON SCRIPT TO HELP ANYONE DEBUG MY C CODE[CODE lang="python" title="Working python script for same problem"]import numpy
import matplotlib.pyplot as plt
import time, sys

nx = 41 # try changing this number from 41 to 81 and Run All ... what happens?
dx = 2 / (nx-1)
nt = 25 #nt is the number of timesteps we want to calculate
dt = .025 #dt is the amount of time each timestep covers (delta t)
c = 1

u = numpy.ones(nx) #numpy function ones()
u[int(.5 / dx):int(1 / dx + 1)] = 2 #setting u = 2 between 0.5 and 1 as per our I.C.s
print(u)

un = numpy.ones(nx) #initialize a temporary array

for n in range(nt): #loop for values of n from 0 to nt, so it will run nt times
un = u.copy()
print(un)##copy the existing values of u into un
for i in range(1, nx): ## you can try commenting this line and...
#for i in range(nx): ## ... uncommenting this line and see what happens!
u = un - c * dt / dx * (un - un[i-1])
#print(u)

plt.plot(numpy.linspace(0, 2, nx), u);
plt.show()[/CODE]
 
Technology news on Phys.org
In your c file, manually compute what the value is that you get for u0_array when j=0 and you should see the problem.
 
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...

Similar threads

Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
5
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 25 ·
Replies
25
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K