Python GUI: Darcy’s Law – calculate Flow, Specific Discharge and Average Linear Velocity

Darcy_Python

Hi all. Today I will demonstrate how to develop a simple GUI tool for calculating discharge, specific discharge and average linear velocity using Python. I will be using codeskulptor as my online tool. Make sure that you are NOT using internet explorer to run the program.

Darcy’s Law is the most basic groundwater equation that you will use over and over again in your career. In my GUI, I have created event handlers for K, A, dh, dl and n. Where:

  • K = Hydraulic conductivity
  • A= cross sectional area
  • dl=length
  • dh=change in head
  • n=porosity

In my program, I have mentioned ft and day as the units for length and time. You can use any consistent unit of your choice.

Developing the program was not too difficult once you understand some basics of GUI programming. I have defined input fields and wrote some codes to validate input parameters. Then I have defined a button handler for the “CALCULATE” button that does the main work for you.

If you have little bit of programming experience, you would probably notice how I have used global variables and called them inside various functions.

Here is the look of the GUI tool:

Darcy_Python

Darcy_Python

Click on the link below to run the program online.

http://www.codeskulptor.org/#user26_CXwOXTPAYr_7.py

Here is the source code:

# Python GUI to calculate Flow based on Darcy's Law
# Simple application of programming in geology
# Programmer: Ankan Basu, CPG
# Contact: admin@coalgeology.com
# email: admin@coalgeology.com
# Version: 4.0
# Date: 12/2/2013
import simplegui,math
message = "Welcome!"
K=0 
A=0 
dh=0
dl=0
Q=0
q=0
n=0
v=0
warning=''

# Handler for mouse click
def click():
    global message
    message = "Test Message"

def get_data_K(d):
    global K, warning
    if d.count("\.")<=1:
        warning=''
        d1=d.replace('.', '')
        if d1.isdigit()==True:
            d_float=float(d)
            K=d_float
        else:
            warning='Must enter numbers only, clear window before entering new data'

    else:
        warning='Must enter numbers only, clear window before entering new data'
def get_data_dh(d):
    global dh, warning
    if d.count("\.")<=1:
        warning=''
        d1=d.replace('.', '')
        if d1.isdigit()==True:
            d_float=float(d)
            dh=d_float
        else:
            warning='Must enter numbers only, clear window before entering new data'

    else:
        warning='Must enter numbers only, clear window before entering new data'

def get_data_dl(d):
    global dl, warning
    if d.count("\.")<=1:
        warning=''
        d1=d.replace('.', '')
        if d1.isdigit()==True:
            d_float=float(d)
            dl=d_float
        else:
            warning='Must enter numbers only, clear window before entering new data'

    else:
        warning='Must enter numbers only, clear window before entering new data'

def get_data_A(d):
    global A, warning
    if d.count("\.")<=1:
        warning=''
        d1=d.replace('.', '')
        if d1.isdigit()==True:
            d_float=float(d)
            A=d_float
        else:
            warning='Must enter numbers only, clear window before entering new data'

    else:
        warning='Must enter numbers only, clear window before entering new data'   

def get_data_n(d):
    global n, warning
    if d.count("\.")<=1:
        warning=''
        d1=d.replace('.', '')
        if d1.isdigit()==True:
            d_float=float(d)
            n=d_float
        else:
            warning='Must enter numbers only, clear window before entering new data'

    else:
        warning='Must enter numbers only, clear window before entering new data'           

def calculate_Q():
    global Q,q,K,dl,dl,A,n,v,warning
    if 0 in [K,dh,dl,A]:
        warning='Did you enter all the required data?'
    else:
        Q=K*A*(dh/dl)
        q=Q/A
        v=q/n

def reset():
    global K,A,dh,dl,Q,warning,n,q,v
    K,A,dh,dl,Q,q,v,n=0,0,0,0,0,0,0,0
    warning=""        

# Handler to draw on canvas
def draw(canvas):
    canvas.draw_text('Calculate Flow, Specific discharge and average linear velocity', [20,32], 18, "Red")
    canvas.draw_text('Enter data to the left window, hit enter', [20,52], 20, "Black")
    canvas.draw_text(str(K)+" :K (Hydraulic Conductivity) in feet/day", [20,80], 18, "Black")
    canvas.draw_text(str(A)+" :A (crossectional area) in Square Feet", [20,100], 18, "Black")
    canvas.draw_text(str(dh)+" :dh (change in head) in Feet", [20,120], 18, "Black")
    canvas.draw_text(str(dl)+" :dl (length of test zone) in feet", [20,140], 18, "Black")
    canvas.draw_text(str(n)+" :n (porosity)", [20,160], 18, "Black")
    canvas.draw_text('Flow calculation Below:', [20,200], 20, "Red")
    canvas.draw_text(str(round(Q,2))+" :Q (Flow) Cubic Feet/day", [20,230], 18, "Blue")
    canvas.draw_text(str(round(q,2))+" :q (specific discharge/Darcy velocity) Feet/day", [20,250], 18, "Blue")
    canvas.draw_text(str(round(v,2))+" :v (average linear velocity) Feet/day", [20,270], 18, "Blue")

    canvas.draw_text(warning, [20,300], 18, "Red")

    canvas.draw_text('Coded by Ankan Basu, CPG', [20,290], 14, "Green")
    canvas.draw_text('website www.coalgeology.com', [20,310], 14, "Orange")
    canvas.draw_text('contact admin@coalgeology.com', [20,330], 14, "Orange")

    image = simplegui.load_image('http://www.interpore.org/reference_material/mgfc-course/f411.gif')
    #print image.get_width()
    #print image.get_height()
    canvas.draw_image(image, (430 / 2, 291 / 2), (400, 291), (300,600), (430/1, 291/1))    
# Create a frame and assign callbacks to event handlers
frame = simplegui.create_frame("Home", 600, 800,200)
frame.set_canvas_background('Aquamarine ')
frame.add_input("Enter K, hit enter", get_data_K, 100)
frame.add_input("Enter A, hit enter", get_data_A, 100)
frame.add_input("Enter dh, hit enter", get_data_dh, 100)
frame.add_input("Enter dl, hit enter", get_data_dl, 100)
frame.add_input("Enter n, hit enter", get_data_n, 100)
frame.add_button("Calculate", calculate_Q,200)
frame.add_button("Reset all", reset,200)
frame.set_draw_handler(draw)

# Start the frame animation
frame.start()

 

Leave a Comment

Your email address will not be published. Required fields are marked *