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

In summary,The conversation is about a C program that is supposed to output the contents of an array, but instead, it produces incorrect numbers. The program is a 1D linear advection equation and uses forward and backward difference methods. A Python script is also provided for debugging purposes. The issue is identified as a problem with the values of u0_array when j=0 in the second for loop.
  • #1
mcgrane5
1
0
TL;DR Summary
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.

1D linear convection C code:
# 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[i] = u0[i] - c * dt/dx * (u0[i] - 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[i] = 2;
        }else
        {
             u0_array[i] = 1;
        }

    }

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

    }

    //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[i] = u0_array[i];

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

    return EXIT_SUCCESS;
}

BELOW IS THE WORKING PYTHON SCRIPT TO HELP ANYONE DEBUG MY C CODE
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[i] = un[i] - c * dt / dx * (un[i] - un[i-1])
        #print(u[i])

plt.plot(numpy.linspace(0, 2, nx), u);
plt.show()
 
Technology news on Phys.org
  • #2
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.
 

1. What is the purpose of using CFD in solving the 1D linear convection equation?

The purpose of using CFD (Computational Fluid Dynamics) is to numerically simulate the behavior of fluid flow and heat transfer using mathematical equations and algorithms. This allows us to study and analyze complex fluid systems, such as the 1D linear convection equation, in a more efficient and cost-effective way compared to traditional experimental methods.

2. Why is there a problem with the C code for the 1D linear convection equation?

The problem with the C code could be due to various reasons, such as errors in the coding, incorrect boundary conditions, or convergence issues. It is important to carefully review and debug the code to identify the specific problem and find a solution.

3. How can the C code for the 1D linear convection equation be improved?

The C code can be improved by optimizing the algorithm used for solving the 1D linear convection equation, improving the accuracy of the numerical methods, and ensuring proper implementation of the boundary conditions. It is also important to validate the code with experimental or analytical results to ensure its accuracy.

4. What are some common challenges faced when solving the 1D linear convection equation using CFD?

Some common challenges include dealing with complex geometries, accurately modeling physical phenomena, choosing appropriate numerical methods and boundary conditions, and managing computational resources. It is important to have a thorough understanding of the problem and its limitations to overcome these challenges.

5. How is the accuracy of the CFD solution for the 1D linear convection equation validated?

The accuracy of the CFD solution can be validated by comparing it to experimental or analytical results, or by performing grid convergence studies to ensure that the solution converges as the grid is refined. It is also important to verify the results with physical principles and to conduct sensitivity analyses to assess the impact of different input parameters on the solution.

Similar threads

Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
619
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
12
Views
3K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
Back
Top