Why is my Hydrodynamics Simulation Code Producing a Noisy Signal?

Click For Summary

Discussion Overview

The discussion revolves around a user's hydrodynamics simulation code intended to model a sod shock tube. The user reports that the output plot of density versus distance results in a noisy signal, prompting inquiries into potential errors in the code.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • The user describes their simulation code and the unexpected noisy output when plotting density against distance.
  • Some participants suggest reposting the code with proper formatting to facilitate better understanding and troubleshooting.
  • There is a mention of a specific issue with the code where the use of brackets may cause formatting problems in the forum, which could hinder the readability of the code.
  • Another participant humorously notes their intention to point out the formatting issue privately, indicating a light-hearted approach to the discussion.

Areas of Agreement / Disagreement

Participants generally agree that the code needs to be reformatted for clarity, but there is no consensus on the specific cause of the noisy signal or how to resolve it.

Contextual Notes

There are indications that the code may contain errors or misbehaving sections, but these have not been identified or resolved in the discussion.

Silviu
Messages
612
Reaction score
11
Hi! I have this code which should simulate a sod shock tube like this: https://en.wikipedia.org/wiki/Sod_shock_tube But when i plot density versus distance I just get something like a noisy signal. Any idea where I am doing something wrong?

from matplotlib import pyplot as plt

import numpy as np

import copy

import random

import mathL=1.0

gamma = 1.4

N=1000

h=L/(N-1)

CFL=0.1v=[[0 for x in range(3)] for x in range(N)]

F=[[0 for x in range(3)] for x in range(N)]
# give the values to each cell
def initialise():

for i in range(N):

if i<N/2:

rho=1

p=1

u=0

else:

rho=0.125

p=0.1

u=0

e=p/((gamma-1)*rho)

v[0]=rho #create a matrix with first column for density, second for density times speed

v[1]=u*rho #and third for e, and a vector for the volume of each grid

v[2]=e
# calculate the maximum speed in the program

def cMax():

uMax=0

for i in range(N):

pressure = (gamma-1)*v[0]*v[2]

speed=v[1]/v[0]

c=np.sqrt(gamma*abs(pressure)/abs(v[0]))

if uMax< (c+abs(speed)):

uMax=c+abs(speed)

return uMax

def fluxCalculator():

for i in range(1,N-1):

#density flux

if v[0]< v[i-1][0]:

rho=v[i-1][0]

if v[0]> v[i-1][0]:

rho=v[0]

if v[0]==v[i-1][0]:

rho=0

F[0]=rho*v[1]/v[0]
#energy flux
if v[2]<v[i-1][2]:

e=v[i-1][2]

if v[2]>v[i-1][2]:

e=v[2]

if v[2]==v[i-1][2]:

e=0

F[2]=F[0]*e
#momentum flux
if v[1]/v[0]<v[i-1][1]/v[i-1][0]:

u=v[i-1][1]/v[i-1][0]

if v[1]/v[0]>v[i-1][1]/v[i-1][0]:

u=v[1]/v[0]

if v[1]/v[0]==v[i-1][1]/v[i-1][0]:

u=0

F[1]=0.5*u*(F[i+1][0]+F[0])initialise()
def update():

tau=CFL*h/cMax()

fluxCalculator()

for i in range(1,N-1):

rho=v[0]

m=v[1]

e=v[2]

pressure=(gamma-1)*rho*e

e=e-tau/h*pressure*(v[i+1][1]/v[i+1][0]-m/rho)

m=m-tau/h*(pressure-(gamma-1)*v[i-1][0]*v[i-1][2])

rho=rho-tau/h*(F[i+1][0]-F[0])

e=e-tau/h*(F[i+1][2]-F[2])

m=m-tau/h*(F[i+1][1]-F[1])

v[0]=rho

v[1]=m

v[2]=e

density=[]

position=np.arange(0,1+h/2,h)
for i in range(1000):

update()
for i in range(N):

density=density+[v[0]]

plt.plot(position,density)

plt.show()
 
Technology news on Phys.org
Please repost your code, using a
Python:
 tag at the top and a
tag at the bottom.

You might notice that what is displayed above switches to italics in the middle somewhere. This is because your code contains v[0]. The first pair of brackets is interpreted by browsers as a sign that the following text should be in italics. Each in the code is "gobbled up" and doesn't appear.
Silviu said:
But when i plot density versus distance I just get something like a noisy signal. Any idea where I am doing something wrong?
Given the amount of code, this is probably not enough for us to go on. If you can identify a section of code that you think is misbehaving, that would be very helpful.
 
Last edited:
  • Like
Likes   Reactions: BvU
Mark44 said:
Please repost your code, using a
Python:
 tag at the top and a
tag at the bottom.
Hehe, I wanted to point that out to Silviu in a private conversation, so his (her) thread would remain unanswered, but never mind. Out of curiosity: How can you let the tags
Python:
[code=python] and ##[\ /code]##  appear without the browser taking action
o0)?
 
BvU said:
Hehe, I wanted to point that out to Silviu in a private conversation, so his (her) thread would remain unanswered, but never mind. Out of curiosity: How can you let the tags
Python:
[code=python] and ##[\ /code]##  appear without the browser taking action
o0)?
I change the color of the first character in each tag. Black works fine -- this causes the browser to not interpret the tag as it usually would.
 
  • Like
Likes   Reactions: Ibix and BvU

Similar threads

  • · Replies 6 ·
Replies
6
Views
2K
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 34 ·
2
Replies
34
Views
6K