-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAcid_Base.py
550 lines (463 loc) · 22.9 KB
/
Acid_Base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
from typing import Final
from manimlib import *
from tkinter import Tk, simpledialog
from tkinter import messagebox
__python_version__ = 3.9
__doc__ = r'''
Run the code in the follwing format in command line / anaconda prompt
manimgl [file_path] [Scene_name]
manimgl C:\Users\jiant\anaconda3\envs\manim\projects\GL\Acid_Base.py FinalScene
'''
# manimgl C:\Users\jiant\anaconda3\envs\manim\projects\GL\Acid_Base.py WeakAcidStrongBase
class WeakAcidStrongBase(Scene):
def construct(self, pKa: float = 4.74, acid_volume: float = 10.0,
molarity_titrated: float = 0.5, molarity_titrant: float = 0.1, demo: bool = False
) -> None:
fr: CameraFrame = self.camera.frame
fr.scale(2).shift(4*UP+6*RIGHT)
def functionAcid(pKa, AMinus, HA, addedBase):
#A- and HA and addedBase are in moles, not molarity
return pKa + math.log10((AMinus + addedBase) / (HA - addedBase))
def functionBase(pKb, B, BHPlus, addedAcid):
#Still all in moles, not molarity
return 14 - (pKb + math.log10((B + addedAcid) / (BHPlus - addedAcid)))
def ice_table_WASB(molar_A_minus: float, ka: float, reverse: bool = True) -> float:
kb = 1/ka if reverse else ka
c = - molar_A_minus * kb
x = (kb + math.sqrt(kb**2 - 4 * c))/2
return x
def calc_pH_WASB(pKa: float, strong_base_volume: float, strong_base_molarity: float,
weak_acid_volume: float, weak_acid_molarity: float, reverse: bool = False
) -> typing.Sequence[float]:
"""
Calculate pH for Weak Acid Strong Base
Const: pKa, both molarities, and weak acid volume
Changing: strong base volume
Extra arg: reverse to change it to Weak Base Strong Acid TODO
Returns: Tuple(pH, [A], [HA-], pOH)
"""
total_volume = weak_acid_volume + strong_base_volume
# mole of initial base (NaOH for example)
mole_base = strong_base_molarity * strong_base_volume
# mole of HA initial (CH3COOH for example)
mole_acid = weak_acid_molarity * weak_acid_volume
# mole of A- initial (CH3COO- for example)
mole_A_minus = 0
molarity_A_minus = mole_A_minus / total_volume
molarity_acid = mole_acid / total_volume
# Tuple of ([A], [HA-], pOH)
return_tuple = (molarity_acid, molarity_A_minus)
# To determine if its before equiv, at equiv, or after
if mole_base == 0:
return ice_table_WASB(mole_acid / total_volume, 10**(-pKa)), *(molarity_acid, molarity_A_minus)
elif mole_acid > mole_base:
# Before equiv
mole_acid -= mole_base
mole_A_minus += mole_base
molarity_A_minus = mole_A_minus / total_volume
molarity_acid = mole_acid / total_volume
elif mole_acid == mole_base:
mole_acid -= mole_base
mole_A_minus += mole_base
molarity_A_minus = mole_A_minus / total_volume
molarity_acid = mole_acid / total_volume
return 14.0 + math.log10(ice_table_WASB(mole_base / total_volume, 10**(-pKa))), *(molarity_acid, molarity_A_minus)
elif mole_acid <= mole_base:
return 14.0 + math.log10((mole_base - mole_acid) / total_volume), *(0.0, mole_acid / total_volume)
return pKa + math.log10((mole_A_minus) / (mole_acid)), *(molarity_acid, molarity_A_minus)
# Volume of Acid needed in mL
# acid_volume = 10.0
# Molarity of Acid
# molarity_titrated = .5
# Molarity of Titrant
# molarity_titrant = .1
equivlibrium_mL_for_base = (molarity_titrated * acid_volume) / molarity_titrant
# print(equivlibrium_mL_for_base)
titrated = equivlibrium_mL_for_base * 2
# pKa: Final = 4.74
common_plane_config = dict(
x_range=[0, titrated, titrated/10],
height=FRAME_HEIGHT*1.6, width=FRAME_WIDTH*1.6
)
n_conf = {"y_axis_config": {"decimal_number_config": {
"num_decimal_places": 3,
"font_size": 36,
}}}
plane = NumberPlane(
**common_plane_config, y_range=[0, 14.0, 2.0],
).scale(0.5).shift(0.5*LEFT)
plane.add_coordinate_labels()
self.add(
plane.get_x_axis_label("mL", direction=RIGHT),
plane.get_y_axis_label("pH", direction=UP)
)
y_max = molarity_titrated * titrated / (titrated)
# y_max = 1
a_conentration_plane = NumberPlane(
**common_plane_config, **n_conf,
y_range=[0, y_max, y_max / 7.],
).scale(0.5).shift(0.5*LEFT + 7.5*UP)
a_conentration_plane.add_coordinate_labels()
a_conentration_plane.add(
a_conentration_plane.get_x_axis_label("mL", direction=RIGHT),
a_conentration_plane.get_y_axis_label("[A-](M)", direction=UP)
)
ha_concentration_plane = NumberPlane(
**common_plane_config, **n_conf,
y_range=[0, y_max, y_max / 7.],
).scale(0.5).shift(13.*RIGHT + 7.5* UP)
ha_concentration_plane.add_coordinate_labels()
self.add(
ha_concentration_plane.get_x_axis_label("mL", direction=UP),
ha_concentration_plane.get_y_axis_label("[HA](M))", direction=UP)
)
oh_concentration_plane = NumberPlane(
**common_plane_config, y_range=[0, 14, 2],
).scale(0.5).shift(13.*RIGHT)
oh_concentration_plane.add_coordinate_labels()
self.add(
oh_concentration_plane.get_x_axis_label("mL", direction=RIGHT),
oh_concentration_plane.get_y_axis_label("pOH", direction=UP)
)
self.add(plane, a_conentration_plane, ha_concentration_plane, oh_concentration_plane)
graph = plane.get_graph(
lambda x: calc_pH_WASB(pKa, x + 0.1, molarity_titrant, acid_volume, molarity_titrated)[0],
use_smoothing=False, # step_size=.10,
x_range=[0, titrated-1]
)
a_conc_graph = a_conentration_plane.get_graph(
lambda x: calc_pH_WASB(pKa, x + 0.1, molarity_titrant, acid_volume, molarity_titrated)[2],
use_smoothing=False, # step_size=.10,
x_range=[0, titrated-1]
)
ha_conc_graph = ha_concentration_plane.get_graph(
lambda x: calc_pH_WASB(pKa, x + 0.1, molarity_titrant, acid_volume, molarity_titrated)[1],
use_smoothing=False, # step_size=.10,
x_range=[0, titrated-1]
)
oh_conc_graph = oh_concentration_plane.get_graph(
lambda x: 14 - calc_pH_WASB(pKa, x + 0.1, molarity_titrant, acid_volume, molarity_titrated)[0],
use_smoothing=False, # step_size=.10,
x_range=[0, titrated-1]
)
self.add(graph, a_conc_graph, ha_conc_graph, oh_conc_graph)
if demo:
self.wait(60)
sys.exit()
class WeakBaseStrongAcid(Scene):
def construct(self, pKb: float = 4.74, base_volume: float = 40.0,
base_molarity: float = .1, molarity_titrant: float = .1, demo: bool = False
) -> None:
fr: CameraFrame = self.camera.frame
fr.scale(2).shift(4*UP+6*RIGHT)
def ice_table_WASB(molar_A_minus: float, kb: float, reverse: bool = False) -> float:
kb = 1/kb if reverse else kb
c = -molar_A_minus * kb
x = (kb + math.sqrt(kb**2 - 4 * c))/2
return x
def calc_pH_WASB(pKb: float, strong_acid_volume: float, strong_acid_molarity: float,
weak_base_volume: float, weak_base_molarity: float, reverse: bool = False
) -> typing.Sequence[float]:
"""
Calculate pH for Weak Acid Strong Base
Const: pKa, both molarities, and weak acid volume
Changing: strong base volume
Extra arg: reverse to change it to Weak Base Strong Acid TODO
Returns: Tuple(pH, [A], [HA-], pOH)
"""
total_volume = weak_base_volume + strong_acid_volume
# mole of initial base (NaOH for example)
mole_base = weak_base_molarity * weak_base_volume
# mole of HA initial (CH3COOH for example)
mole_acid = strong_acid_molarity * strong_acid_volume
# mole of A- initial (CH3COO- for example)
mole_BH_plus = 0
molarity_BH_plus = mole_BH_plus / total_volume
molarity_base = mole_base / total_volume
# To determine if its before equiv, at equiv, or after
if mole_base == 0:
return ice_table_WASB(mole_acid, 10**(-pKb), False), *(molarity_base, molarity_BH_plus)
elif mole_acid < mole_base:
# Before equiv
mole_base -= mole_acid
mole_BH_plus += mole_acid
molarity_BH_plus = mole_BH_plus / total_volume
molarity_base = mole_base / total_volume
elif mole_acid == mole_base:
mole_base -= mole_acid
mole_BH_plus += mole_base
molarity_BH_plus = mole_BH_plus / total_volume
molarity_base = mole_base / total_volume
return 14.0 + math.log10(ice_table_WASB(mole_base, 10**(-pKb))), *(molarity_base, molarity_BH_plus)
elif mole_acid >= mole_base:
return -math.log10((-mole_base + mole_acid) / total_volume), *(0.0, mole_base / total_volume)
return 14 - pKb - math.log10((mole_BH_plus) / (mole_base)), *(molarity_base, molarity_BH_plus)
# Volume of Base needed in mL
# base_volume = 40.0
# Molarity of Base
# base_molarity = .1
# Molarity of Titrant (Acid)
# molarity_titrant = .1
equivlibrium_mL_for_base = (base_molarity * base_volume) / molarity_titrant
# print(equivlibrium_mL_for_base)
titrated = equivlibrium_mL_for_base * 2
# pKb = 4.74
common_plane_config = dict(
x_range=[0, titrated, titrated/10],
height=FRAME_HEIGHT*1.6, width=FRAME_WIDTH*1.6
)
n_conf = {"y_axis_config": {"decimal_number_config": {
"num_decimal_places": 3,
"font_size": 36,
}}}
plane = NumberPlane(
**common_plane_config, y_range=[0, 14.0, 2.0],
).scale(0.5).shift(0.5*LEFT)
plane.add_coordinate_labels()
self.add(
plane.get_x_axis_label("mL", direction=RIGHT),
plane.get_y_axis_label("pH", direction=UP)
)
y_max = base_molarity * titrated / (titrated)
# y_max = 1
b_conentration_plane = NumberPlane(
**common_plane_config, **n_conf,
y_range=[0, y_max, y_max / 7.],
).scale(0.5).shift(0.5*LEFT + 7.5*UP)
b_conentration_plane.add_coordinate_labels()
b_conentration_plane.add(
b_conentration_plane.get_x_axis_label("mL", direction=RIGHT),
b_conentration_plane.get_y_axis_label("[B](M)", direction=UP)
)
bh_concentration_plane = NumberPlane(
**common_plane_config, **n_conf,
y_range=[0, y_max, y_max / 7.],
).scale(0.5).shift(13.*RIGHT + 7.5* UP)
bh_concentration_plane.add_coordinate_labels()
self.add(
bh_concentration_plane.get_x_axis_label("mL", direction=UP),
bh_concentration_plane.get_y_axis_label("[BH+](M))", direction=UP)
)
oh_concentration_plane = NumberPlane(
**common_plane_config, y_range=[0, 14, 2],
).scale(0.5).shift(13.*RIGHT)
oh_concentration_plane.add_coordinate_labels()
self.add(
oh_concentration_plane.get_x_axis_label("mL", direction=RIGHT),
oh_concentration_plane.get_y_axis_label("pOH", direction=UP)
)
self.add(plane, b_conentration_plane, bh_concentration_plane, oh_concentration_plane)
graph = plane.get_graph(
lambda x: calc_pH_WASB(pKb, x + 0.1, molarity_titrant, base_volume, base_molarity)[0],
use_smoothing=False, # step_size=.10,
x_range=[0, titrated-1]
)
b_conc_graph = b_conentration_plane.get_graph(
lambda x: calc_pH_WASB(pKb, x + 0.1, molarity_titrant, base_volume, base_molarity)[1],
use_smoothing=False, # step_size=.10,
x_range=[0, titrated-1]
)
bh_conc_graph = bh_concentration_plane.get_graph(
lambda x: calc_pH_WASB(pKb, x + 0.1, molarity_titrant, base_volume, base_molarity)[2],
use_smoothing=False, # step_size=.10,
x_range=[0, titrated-1]
)
oh_conc_graph = oh_concentration_plane.get_graph(
lambda x: 14 - calc_pH_WASB(pKb, x + 0.1, molarity_titrant, base_volume, base_molarity)[0],
use_smoothing=False, # step_size=.10,
x_range=[0, titrated-1]
)
self.add(graph, b_conc_graph, bh_conc_graph, oh_conc_graph)
if demo:
self.wait(60)
sys.exit()
class StrongAcidStrongBase(Scene):
def construct(self, demo: bool = False):
raise Exception
fr: CameraFrame = self.camera.frame
fr.scale(2).shift(4*UP+6*RIGHT)
def calc_pH_WASB(pKb: float, strong_acid_volume: float, strong_acid_molarity: float,
weak_base_volume: float, weak_base_molarity: float, reverse: bool = False
) -> typing.Sequence[float]:
"""
Calculate pH for Weak Acid Strong Base
Const: pKa, both molarities, and weak acid volume
Changing: strong base volume
Extra arg: reverse to change it to Weak Base Strong Acid TODO
Returns: Tuple(pH, [A], [HA-], pOH)
"""
total_volume = weak_base_volume + strong_acid_volume
# mole of initial base (NaOH for example)
mole_base = weak_base_molarity * weak_base_volume
# mole of HA initial (CH3COOH for example)
mole_acid = strong_acid_molarity * strong_acid_volume
# mole of A- initial (CH3COO- for example)
mole_BH_plus = 0
molarity_BH_plus = mole_BH_plus / total_volume
molarity_base = mole_base / total_volume
# To determine if its before equiv, at equiv, or after
if mole_base == 0:
return ice_table_WASB(mole_acid, 10**(-pKb), False), *(molarity_base, molarity_BH_plus)
elif mole_acid < mole_base:
# Before equiv
mole_base -= mole_acid
mole_BH_plus += mole_acid
molarity_BH_plus = mole_BH_plus / total_volume
molarity_base = mole_base / total_volume
elif mole_acid == mole_base:
mole_base -= mole_acid
mole_BH_plus += mole_base
molarity_BH_plus = mole_BH_plus / total_volume
molarity_base = mole_base / total_volume
return 14.0 + math.log10(ice_table_WASB(mole_base, 10**(-pKb))), *(molarity_base, molarity_BH_plus)
elif mole_acid >= mole_base:
return -math.log10((-mole_base + mole_acid) / total_volume), *(0.0, mole_base / total_volume)
return 14 - pKb - math.log10((mole_BH_plus) / (mole_base)), *(molarity_base, molarity_BH_plus)
# Volume of Base needed in mL
base_volume = 40.0
# Molarity of Base
base_molarity = .1
# Molarity of Titrant (Acid)
molarity_titrant = .1
equivlibrium_mL_for_base = (base_molarity * base_volume) / molarity_titrant
# print(equivlibrium_mL_for_base)
titrated = equivlibrium_mL_for_base * 2
pKb = 4.74
common_plane_config = dict(
x_range=[0, titrated, titrated/10],
height=FRAME_HEIGHT*1.6, width=FRAME_WIDTH*1.6
)
n_conf = {"y_axis_config": {"decimal_number_config": {
"num_decimal_places": 3,
"font_size": 36,
}}}
plane = NumberPlane(
**common_plane_config, y_range=[0, 14.0, 2.0],
).scale(0.5).shift(0.5*LEFT)
plane.add_coordinate_labels()
self.add(
plane.get_x_axis_label("mL", direction=RIGHT),
plane.get_y_axis_label("pH", direction=UP)
)
oh_concentration_plane = NumberPlane(
**common_plane_config, y_range=[0, 14, 2],
).scale(0.5).shift(13.*RIGHT)
oh_concentration_plane.add_coordinate_labels()
self.add(
oh_concentration_plane.get_x_axis_label("mL", direction=RIGHT),
oh_concentration_plane.get_y_axis_label("pOH", direction=UP)
)
self.add(plane, b_conentration_plane, bh_concentration_plane, oh_concentration_plane)
graph = plane.get_graph(
lambda x: calc_pH_WASB(pKb, x + 0.1, molarity_titrant, base_volume, base_molarity)[0],
use_smoothing=False, # step_size=.10,
x_range=[0, titrated-1]
)
oh_conc_graph = oh_concentration_plane.get_graph(
lambda x: 14 - calc_pH_WASB(pKb, x + 0.1, molarity_titrant, base_volume, base_molarity)[0],
use_smoothing=False, # step_size=.10,
x_range=[0, titrated-1]
)
self.add(graph, oh_conc_graph)
if demo:
self.wait(60)
sys.exit()
# manimgl C:\Users\jiant\anaconda3\envs\manim\projects\GL\Acid_Base.py FinalScene
class FinalScene(StrongAcidStrongBase, WeakAcidStrongBase, WeakBaseStrongAcid):
def construct(self):
strongAcid = ["HCl","HBr","HI","HNO3","H2SO4","HClO4"]
strongBase = ["LiOH","NaOH","KOH","Ca(OH)2","Sr(OH)2","Ba(OH)2"]
flagStrongAcid = False
flagStrongBase = False
pka = 0.0
pkb = 0.0
acid = ""
base = ""
cAcid = 0.0 #Concentration
cBase = 0.0
vAcid = 0.0 #Volume
vBase = 0.0
titrationStatus = False # False is titrating acid with base, True is titrating base with acid
Tk()
while True:
acid = simpledialog.askstring("Acid","Please enter an acid", initialvalue="")
if acid is not None:
break
if acid in strongAcid:
flagStrongAcid=True
warning = "Decimal ONLY! \n"
if(not flagStrongAcid):
try:
pka = float(simpledialog.askstring(
"pKa","Please enter an the pKa for the entered weak acid", initialvalue=""))
except ValueError:
pka = float(simpledialog.askstring(
"pKa",f"{warning}Please enter an the pKa for the entered weak acid", initialvalue=""))
while True:
base = simpledialog.askstring("Base","Please enter a base", initialvalue="")
if base is not None:
break
if base in strongBase:
flagStrongBase = True
if(not flagStrongBase):
try:
pkb = float(simpledialog.askstring(
"Kb","Please enter an the pKb for the entered weak base", initialvalue=""))
except ValueError:
pkb = float(simpledialog.askstring(
"Kb", f"{warning}Please enter an the pKb for the entered weak base", initialvalue=""))
if (not flagStrongAcid) and (not flagStrongBase):
raise Exception("Does not support weak-weak titration, please enter at least a strong base or acid")
add_initial_value = "Acid"
if flagStrongAcid and not flagStrongBase:
add_initial_value = "Acid"
if not flagStrongAcid and flagStrongBase:
add_initial_value = "Base"
t = simpledialog.askstring(
"Choose Titrant",
"""
Please enter
Acid if you are titrating base with acid
Base if you are titrating acid with base.
If initialvalue appears, go positive, do not change
""",
initialvalue=add_initial_value
)
try:
if(t == "Acid"):
cAcid = float(simpledialog.askstring(
"Concentration of Acid", "Please enter the concentration of "+ acid, initialvalue=""))
cBase = float(simpledialog.askstring(
"Concentration of Base","Please enter the concentration of "+ base, initialvalue=""))
vBase = float(simpledialog.askstring(
"Volume of Base","Please enter the volume of " + base, initialvalue=""))
else:
cAcid = float(simpledialog.askstring(
"Concentration of Acid","Please enter the concentration of " + acid, initialvalue=""))
cBase = float(simpledialog.askstring(
"Concentration of Base","Please enter the concentration of " + base, initialvalue=""))
vAcid = float(simpledialog.askstring(
"Volume of Acid","Please enter the volume of " + acid, initialvalue=""))
except ValueError:
if(t == "Acid"):
cAcid = float(simpledialog.askstring(
"Concentration of Acid",warning + " Please enter the concentration of " + acid, initialvalue=""))
cBase = float(simpledialog.askstring(
"Concentration of Base","Please enter the concentration of " + base, initialvalue=""))
vBase = float(simpledialog.askstring(
"Volume of Base","Please enter the volume of " + base, initialvalue=""))
else:
cAcid = float(simpledialog.askstring(
"Concentration of Acid",warning + " Please enter the concentration of " + acid, initialvalue=""))
cBase = float(simpledialog.askstring(
"Concentration of Base","Please enter the concentration of " + base, initialvalue=""))
vAcid = float(simpledialog.askstring(
"Volume of Acid","Please enter the volume of " + acid, initialvalue=""))
if flagStrongAcid and flagStrongBase: # TODO
return StrongAcidStrongBase.construct(self)
elif flagStrongAcid and not flagStrongBase:
# t == "Acid"
return WeakBaseStrongAcid.construct(self, pkb, vBase, cBase, cAcid, True)
elif not flagStrongAcid and flagStrongBase:
# t == "Base"
return WeakAcidStrongBase.construct(self, pka, vAcid, cAcid, cBase, True)