Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in equation for calculating u in linear convection #38

Closed
Anirudh257 opened this issue Jan 3, 2018 · 8 comments
Closed

Error in equation for calculating u in linear convection #38

Anirudh257 opened this issue Jan 3, 2018 · 8 comments

Comments

@Anirudh257
Copy link

In linear convection's equation:

`u = numpy.ones(nx)

u[int(.5/dx):int(1 / dx + 1)] = 2`

Line 2 will give a zero error.

@PKHG
Copy link

PKHG commented Jan 3, 2018

Hallo, you always should give a precise context ;-)
[code]
import numpy
nx = 14
u = numpy.ones(nx)
dx =0.2
u[int(.5/dx):int(1 / dx + 1)] = 2
u
[/code]
gives: array([ 1., 1., 2., 2., 2., 2., 1., 1., 1., 1., 1., 1., 1., 1.])

@mesnardo
Copy link
Member

mesnardo commented Jan 3, 2018

Hi @Anirudh257

I suspect you are using Python 2, which has a different behavior than Python 3 when it comes to division.
When using Python 2, the problem comes from the cell above the one you mention where we compute the grid cell width dx.
With Python 2, division between two integers returns an integer and thus dx = 2 / (nx - 1) will return 0.
With Python 3, it will return a float, i.e. 0.05.

@mesnardo
Copy link
Member

mesnardo commented Jan 3, 2018

Python 3.6.1 |Anaconda 4.4.0 (64-bit)| (default, May 11 2017, 13:09:58) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> nx = 41
>>> dx = 2 / (nx - 1)
>>> dx
0.05
>>> u = numpy.ones(nx)
>>> u[int(0.5 / dx): int(1 / dx + 1)] = 2
>>> u
array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  2.,  2.,  2.,
        2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  1.,  1.,  1.,  1.,  1.,
        1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,
        1.,  1.])

@PKHG
Copy link

PKHG commented Jan 3, 2018 via email

@mesnardo
Copy link
Member

mesnardo commented Jan 3, 2018

Python 2.7.12 (default, Nov 20 2017, 18:23:56) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> nx = 41
>>> dx = 2 / (nx - 1)
>>> dx
0
>>> u = numpy.ones(nx)
>>> u[int(0.5 / dx): int(1 / dx + 1)] = 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: float division by zero

@PKHG
Copy link

PKHG commented Jan 3, 2018

with Python 2 you need e.g. use this dx = 2.0/(nx-1) to force a decimal ;-
Not needed with Python 3 (if not changed the division elsewhere)

@gforsyth
Copy link
Member

gforsyth commented Jan 3, 2018 via email

@Anirudh257
Copy link
Author

Sorry, @mesnardo I am a beginner on Github and this will never repeat again. The issue will be resolved on importing the future division method from Python 3. It has been pointed out above. I am closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants