User:Thierry Dugnolle/Python/Diffusion in one dimension

Diffusion in one dimension

main.py

edit
# 2023, October 18. Version231018

# The drawer paints and draws an animation of the diffusion of anything
# (molecules, heat...) in an elongated compartment.


from Scalar1DfieldDrawer import aScalar1DfieldDrawer
from Scalar1Dfield import TheGaussian1Dfield
from Vector2D import The2Dvector
from time import process_time


print ("Time:", process_time(), "Mathematical painter")

# The drawer
TheDrawer = aScalar1DfieldDrawer()

# The canvas:
TheCanvasStyle= "black on white" # "color", "black on white" or "white on black"
TheWidth = 500 # number of pixels
TheHeight = 250 # number of pixels
TheLengthUnit = 10 # number of pixels
TheRealWidth = TheWidth/TheLengthUnit
TheRealHeight = TheHeight/TheLengthUnit
TheCanvasColor = (0, 0, 0) # (red, green, blue) here black
TheDrawer.takesAnewCanvas(TheWidth, TheHeight, TheLengthUnit, TheCanvasStyle, TheCanvasColor)
TheDrawer.canvas.imageCenter = The2Dvector( 0.5*TheRealWidth, 0.5*TheRealHeight)

# The paintbrush:
TheDrawer.paintbrush.lineHalfWidth = 0.4 # (The line half width * the length unit) is the
# number of pixels in the half width of the line.

# The palette:
def ThePalette(shade): # heatColors : black, red, yellow, white.
    return TheDrawer.palette.heatColors(shade)

# The field:
ThePixelNumberOfPoints = 2 # number of points in the field in the width of a pixel
TheNumberOfPoints = TheWidth*ThePixelNumberOfPoints
sigma = 1.0
mu = -0.5*TheRealWidth
TheField = TheGaussian1Dfield(TheNumberOfPoints, TheRealWidth, sigma, mu, TheRealHeight)
TheField.limitCondition = "constant"
# Its flow:
def TheFieldFlow(Afield):
    return Afield.diffusion1Dflow(sigma)

# The animation:
TheNumberOfImages = 3
TheNumberOf_dts = 30000
The_dt= 0.00001
for i in range(TheNumberOfImages):
    print("Time:", process_time(), "The drawer paints the image", i)
    TheDrawer.paintsAndDrawsAreal1Dfield(TheField, TheRealHeight, ThePalette)
    TheDrawer.givesApainting("diffusion1D/im" + str(1000 + i) + ".png")
    print("Time:", process_time(), "The drawer calculates the next state of the field.")
    for j in range(TheNumberOf_dts):
        if j % 5000 == 0:
            print("Time:", process_time(), "Time of the field:", j*The_dt)
        TheField = TheField.nextField(TheFieldFlow, The_dt)

print("Time:", process_time(), "Good bye")

Other files

edit

This program requires the following additional files: Painter.py, Palette.py, Line2Ddrawer.py, Scalar1DfieldDrawer.py, Scalar1Dfield.py and Vector2D.py. They are all on this page : User:Thierry Dugnolle/Python/Mathematical painter.