Coder Social home page Coder Social logo

Comments (8)

PKHG avatar PKHG commented on May 16, 2024 1

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)

from cfdpython.

PKHG avatar PKHG commented on May 16, 2024

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.])

from cfdpython.

mesnardo avatar mesnardo commented on May 16, 2024

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.

from cfdpython.

mesnardo avatar mesnardo commented on May 16, 2024
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.])

from cfdpython.

PKHG avatar PKHG commented on May 16, 2024

from cfdpython.

mesnardo avatar mesnardo commented on May 16, 2024
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

from cfdpython.

gforsyth avatar gforsyth commented on May 16, 2024

from cfdpython.

Anirudh257 avatar Anirudh257 commented on May 16, 2024

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.

from cfdpython.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.