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

Click For Summary
SUMMARY

The forum discussion addresses a problem in a C program that implements a numerical solution for the 1D linear convection equation using a forward difference in time and a backward difference in space. The issue arises in the second for loop where the output of the array contains incorrect values. The user is advised to manually compute the values of the array to identify the source of the error. A working Python script is provided for comparison, which successfully implements the same numerical scheme and produces correct results.

PREREQUISITES
  • Understanding of numerical methods for solving partial differential equations
  • Familiarity with C programming, specifically dynamic memory allocation and array manipulation
  • Knowledge of the 1D linear convection equation and its discretization techniques
  • Experience with Python for numerical computing, particularly using NumPy
NEXT STEPS
  • Debug the C code by manually calculating the values of the arrays during execution
  • Learn about memory management in C to prevent issues with dynamic arrays
  • Explore the differences in array indexing between C and Python to avoid off-by-one errors
  • Investigate other numerical methods for solving the convection equation, such as Lax-Wendroff or MacCormack methods
USEFUL FOR

Software developers, computational scientists, and students working on numerical simulations of partial differential equations, particularly those transitioning from Python to C programming.

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.
 

Similar threads

Replies
1
Views
3K
  • · 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 7 ·
Replies
7
Views
2K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K