Python GUI for Molarity, Meq and Equivalent Weight: Calculator

I am going to talk about molarity (moles/L) , mili-equivalent/L and equivalent weight today. My approach is to get you what you need from common geochemical analysis report from various labs in the US.

In general, when you analyze for quality, depending on the lab and the parameters of interest, you may use units such as ppm, ppm or mg/L ug/L.

Although in theory, ppm is not exactly the same as mg/L, we will assure that they are for all practical mathematical calculations. Similarly, we will assume, ppb is same as ug/L.

If you look closely, you can tell that some of the units are actually dimensionless ratios: ppm, ppb, ppt (parts per million, billion, and trillion). Some units are weight per volume units, such as milligrams per liter (mg/L) and micrograms per liter (ug/L).

Now let’s look at molarity. Molarity is the number of moles of solute dissolved in one liter of solution (moles/L).

The units, therefore are moles per liter, specifically it’s moles of solute per liter of solution.

Example: NaCl = Na (Atomic Mass =22.991) + Cl (Atomic Msss= 35.457) = 58.448 gm.

Thus, a 1 molar solution of NaCl would be 58.5 grams of NaCl dissolved in 1 Liter of water.

 

Remember that molality (M) = moles solute / kg of solution; we are NOT going to discuss about this one today.

 

The equivalent weight of a dissolved ionic chemical species is the formula weight divided by the electrical charge. Or, one equivalent is the amount of ion required to cancel out the electrical charge of an oppositely charged monovalent ion. Generally, the amount of a substance in equivalents often has a very small magnitude, so milliequivalents (mEq or meq) is commonly used instead. In fact, in out python GUI you can calculate milliequivalents of solute per liter of solvent (mEq/L).

Now, here is the interface of my python GUI on codeskulptor:

molarity gui

 To run the program online, visit: http://www.codeskulptor.org/#user26_0jUxvmXsMZ_7.py

Just enter the data to the left window and hit calculate.

Source Code:

# Python GUI to calculate Molarity, Equivalent Weight and miliequivalent/L
# Simple application of programming in geology
# Programmer: Ankan Basu, CPG
# Note: Program will not work in INTERNET EXPLORER, use SAFARI, CHROME or FIREFOX.
# Contact: admin@coalgeology.com
# email: admin@coalgeology.com
# Version: 1.1
# Date: 12/3/2013
import simplegui,math
message = "Welcome!"
FW=0 #Molecular Weight/ Atomic Weight
C=0  #Concentration
CH=0 #Charge
molarity=0
meq_L=0
EW=0

warning=''

def get_data_formula_weight(d):
    global FW, warning
    if d.count("\.")<=1:
        warning=''
        d1=d.replace('.', '')
        if d1.isdigit()==True:
            d_float=float(d)
            FW=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_concentration(d):
    global C, warning
    if d.count("\.")<=1:
        warning=''
        d1=d.replace('.', '')
        if d1.isdigit()==True:
            d_float=float(d)
            C=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_charge(d):
    global CH, warning
    if d.count("\.")<=1:
        warning=''
        d1=d.replace('.', '')
        if d1.isdigit()==True:
            d_float=float(d)
            CH=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():
    global C,CH,FW,warning, molarity, meq_L,EW
    if 0 in [FW,C,CH]:
        warning='Did you enter all the required data?'
    else:
        molarity=C/(1000*FW)
        meq_L=C/(FW/CH)
        EW=FW/CH
        print molarity

def reset():
    global FW,C,CH,molarity,meq_L,warning,EW
    FW,C,CH,molarity,meq_L,EW=0,0,0,0,0,0
    warning=""        

# Handler to draw on canvas
def draw(canvas):
    canvas.draw_text('Calculate Molarity, Equivalent Weight and Meq/L', [20,32], 18, "Red")
    canvas.draw_text('Enter data to the left window, hit enter', [20,52], 20, "Black")
    canvas.draw_text(str(FW)+" : Formula Weight in grams", [20,80], 18, "Black")
    canvas.draw_text(str(C)+" : concentration in mg/L", [20,100], 18, "Black")
    canvas.draw_text(str(CH)+" : charge of the species", [20,120], 18, "Black")
    #canvas.draw_text(str(EW)+" : Equivalent weight is", [20,140], 18, "Black")

    canvas.draw_text('Calculations below:', [20,200], 20, "Red")
    canvas.draw_text(str("%.2e" %molarity)+" : is the molarity", [20,230], 18, "Blue")
    canvas.draw_text(str(round(EW,2))+" is the equivalent weight", [20,250], 18, "Blue")
    canvas.draw_text(str(round(meq_L,3))+" : is the meq/L", [20,270], 18, "Blue")

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

    canvas.draw_text('Coded by Ankan Basu, CPG, PG; Hydrogeologist/Geochemist', [20,320], 18, "Black")
    canvas.draw_text('website www.coalgeology.com', [20,340], 14, "Red")
    canvas.draw_text('contact admin@coalgeology.com', [20,360], 18, "Black")

    canvas.draw_text('Molarity= no. of moles of solute dissolved in 1 L of solution (moles/L)', [20,620], 18, "Black")
    canvas.draw_text('One Mole of a chemical species is its formula weight in grams', [20,640], 18, "Black")
    canvas.draw_text('Equivalent Weight units incorporate the chemical behavior of a solute.', [20,660], 18, "Black")
    canvas.draw_text('Equivalent Weight = Formula Weight/charge.', [20,680], 18, "Black")
    canvas.draw_text('The equivalent weight is the mass of one equivalent of a substance', [20,700], 18, "Black")
    canvas.draw_text('Example: 1 mol H2SO4 = 2 equivalents H2SO4 ', [20,720], 18, "Black")
    canvas.draw_text('Example: Ca2+ = 40.08 gm/mole = 2 equivalents ', [20,740], 18, "Black")

    image = simplegui.load_image('http://misterguch.brinkster.net/molarity2.gif')
    #print image.get_width()
    #print image.get_height()
    canvas.draw_image(image, (438 / 2, 187 / 2), (438, 187), (300,500), (438/1, 187/1)) 
    image2 = simplegui.load_image('http://m.c.lnkd.licdn.com/media/p/1/000/203/012/2815545.jpg')
    canvas.draw_image(image2, (319 / 2, 319 / 2), (319, 319), (550,50), (319/3, 319/3))
    canvas.draw_text('Ankan Basu, CPG ', [490,120], 15, "Black")
    canvas.draw_text('Hydrogeologist ', [490,140], 17, "Black")

    #print image2.get_width()
    #print image2.get_height()
# Create a frame and assign callbacks to event handlers
frame = simplegui.create_frame("Home", 600, 800,250)
frame.set_canvas_background('Aquamarine ')
frame.add_input("Enter Formula Weight in Grams, hit enter", get_data_formula_weight, 100)
frame.add_input("Enter Concentrations in mg/L, hit enter", get_data_concentration, 100)
frame.add_input("Enter Charge, hit enter", get_data_charge, 100)

frame.add_button("Calculate", calculate,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 *