ChemDave commited on
Commit
418afe8
·
verified ·
1 Parent(s): 8183450

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +187 -97
app.py CHANGED
@@ -690,6 +690,7 @@ def calculate_concentration(
690
  return f"Concentration = {concentration:.4f} mol/dm³"
691
 
692
 
 
693
  def calculate_acid_base_analysis(
694
  acid,
695
  base,
@@ -699,9 +700,13 @@ def calculate_acid_base_analysis(
699
  known_volume,
700
  unknown_volume,
701
  sample_mass,
702
- total_prepared_volume
 
 
 
 
703
  ):
704
- """Calculate supported acid-base titration results."""
705
 
706
  import math
707
 
@@ -716,50 +721,70 @@ def calculate_acid_base_analysis(
716
  ("HCl", "Na2CO3"): (2, 1, "2HCl + Na₂CO₃ → 2NaCl + H₂O + CO₂"),
717
  ("HNO3", "Na2CO3"): (2, 1, "2HNO₃ + Na₂CO₃ → 2NaNO₃ + H₂O + CO₂"),
718
  ("H2SO4", "Na2CO3"): (1, 1, "H₂SO₄ + Na₂CO₃ → Na₂SO₄ + H₂O + CO₂"),
 
719
  }
720
 
721
  molar_masses = {
722
  "HCl": 36.46,
723
  "H2SO4": 98.079,
724
  "HNO3": 63.012,
 
725
  "NaOH": 39.997,
726
  "KOH": 56.105,
727
  "Ca(OH)2": 74.092,
728
  "Na2CO3": 105.988,
729
  }
730
 
731
- reaction = reactions.get((acid, base))
732
-
733
- if reaction is None:
734
- return (
735
- f"Unsupported acid-base pair: {acid} and {base}. "
736
- "Choose a supported pair."
737
- )
738
-
739
- acid_coefficient, base_coefficient, equation = reaction
740
-
741
  try:
742
  ck = float(known_concentration)
743
  vk = float(known_volume)
744
  vu = float(unknown_volume)
745
- mass = float(sample_mass)
746
- total_volume = float(total_prepared_volume)
 
 
 
747
  except (TypeError, ValueError):
748
- return "Error: Enter valid numerical values in all fields."
749
 
750
- values = [ck, vk, vu, mass, total_volume]
 
 
 
751
 
752
  if not all(math.isfinite(v) for v in values):
753
- return "Error: All numerical inputs must be finite."
754
 
755
  if ck <= 0 or vk <= 0 or vu <= 0:
756
- return "Error: Concentration and both volumes must be positive."
 
 
757
 
758
- if mass <= 0 or total_volume <= 0:
759
- return "Error: Sample mass and total prepared volume must be positive."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
760
 
761
- if vu > total_volume:
762
- return "Error: The aliquot volume cannot exceed the total prepared volume."
763
 
764
  if known_solution == "Acid":
765
  known_coefficient = acid_coefficient
@@ -770,110 +795,175 @@ def calculate_acid_base_analysis(
770
  unknown_coefficient = acid_coefficient
771
  unknown_substance = acid
772
 
773
- # Convert volumes from mL to dm³ before calculating moles.
774
- known_moles = ck * vk / 1000
775
-
776
- unknown_moles_in_aliquot = (
777
- known_moles * unknown_coefficient / known_coefficient
778
- )
779
 
780
- unknown_concentration = (
781
- unknown_moles_in_aliquot / (vu / 1000)
782
- )
 
 
783
 
784
- result = [
785
- f"Balanced equation: {equation}",
786
- f"Known solution: {known_solution}",
787
- f"Unknown substance: {unknown_substance}",
788
- f"Known moles = {known_moles:.6f} mol",
789
- f"Unknown moles in aliquot = {unknown_moles_in_aliquot:.6f} mol",
790
- f"Unknown concentration = {unknown_concentration:.4f} mol/dm³",
 
 
 
 
 
 
 
 
 
 
 
 
791
  ]
792
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
793
  if calculation_type == "Molar concentration":
794
- return "\n".join(result)
 
795
 
796
  if calculation_type == "Mass concentration":
797
- mass_concentration = (
798
- unknown_concentration * molar_masses[unknown_substance]
799
- )
800
- result.append(
801
- f"Mass concentration = {mass_concentration:.4f} g/dm³"
802
- )
803
- result.append(
804
- "Formula: mass concentration = molar concentration × molar mass"
805
- )
806
- return "\n".join(result)
 
 
 
 
807
 
808
- # Purity and hydration calculations currently support sodium carbonate
809
- # as the unknown base, titrated using a known acid.
810
- if calculation_type in (
811
- "Percentage purity",
812
- "Water of crystallisation"
813
- ):
814
  if base != "Na2CO3" or known_solution != "Acid":
815
  return (
816
  "This calculation currently requires sodium carbonate "
817
  "as the unknown base and a known acid as the titrant."
818
  )
819
 
820
- total_moles = (
821
- unknown_moles_in_aliquot
822
- * total_volume / vu
823
- )
 
824
 
 
825
  anhydrous_mass = total_moles * molar_masses["Na2CO3"]
826
 
827
- if calculation_type == "Percentage purity":
828
- pure_mass = anhydrous_mass
829
- purity = (pure_mass / mass) * 100
830
-
831
- if purity > 100.5:
832
- return (
833
- "Check the readings and sample preparation: "
834
- "the calculated pure mass exceeds the sample mass."
835
- )
836
-
837
- result.append(f"Sample mass = {mass:.4f} g")
838
- result.append(f"Calculated Na₂CO₃ mass = {pure_mass:.4f} g")
839
- result.append(f"Percentage purity = {purity:.2f}%")
840
- result.append(
841
- "Formula: purity (%) = calculated pure mass / sample mass × 100"
842
- )
843
- return "\n".join(result)
844
 
845
  if anhydrous_mass > mass:
846
  return (
847
- "Error: Calculated anhydrous Na₂CO₃ mass exceeds "
848
- "the original sample mass. Check the experimental data."
 
849
  )
850
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
851
  water_mass = mass - anhydrous_mass
852
  water_moles = water_mass / 18.015
853
-
854
- if total_moles <= 0 or water_moles < 0:
855
- return "Error: The experimental data cannot give a valid hydration number."
856
-
857
- x = water_moles / total_moles
858
-
859
- result.append(f"Hydrated sample mass = {mass:.4f} g")
860
- result.append(f"Anhydrous Na₂CO₃ mass = {anhydrous_mass:.4f} g")
861
- result.append(f"Water mass = {water_mass:.4f} g")
862
- result.append(f"Moles of water = {water_moles:.6f} mol")
863
- result.append(f"Hydration number, x = {x:.3f}")
864
- result.append(
865
- "Formula: x = moles of water / moles of Na₂CO₃"
866
- )
867
- result.append(
868
- "The calculated x is experimental; assess uncertainty "
869
- "before assigning a whole-number formula."
870
- )
871
- return "\n".join(result)
 
 
 
 
 
 
 
 
872
 
873
  return "Error: Select a supported calculation type."
874
 
875
 
876
 
 
877
  def test_calculate_concentration():
878
  tests = [
879
  {
 
690
  return f"Concentration = {concentration:.4f} mol/dm³"
691
 
692
 
693
+
694
  def calculate_acid_base_analysis(
695
  acid,
696
  base,
 
700
  known_volume,
701
  unknown_volume,
702
  sample_mass,
703
+ total_prepared_volume,
704
+ custom_equation="",
705
+ custom_acid_coefficient=1,
706
+ custom_base_coefficient=1,
707
+ custom_unknown_molar_mass=0
708
  ):
709
+ """Calculate supported acid-base results and show each working step."""
710
 
711
  import math
712
 
 
721
  ("HCl", "Na2CO3"): (2, 1, "2HCl + Na₂CO₃ → 2NaCl + H₂O + CO₂"),
722
  ("HNO3", "Na2CO3"): (2, 1, "2HNO₃ + Na₂CO₃ → 2NaNO₃ + H₂O + CO₂"),
723
  ("H2SO4", "Na2CO3"): (1, 1, "H₂SO₄ + Na₂CO₃ → Na₂SO₄ + H₂O + CO₂"),
724
+ ("CH3COOH", "NaOH"): (1, 1, "CH₃COOH + NaOH → CH₃COONa + H₂O"),
725
  }
726
 
727
  molar_masses = {
728
  "HCl": 36.46,
729
  "H2SO4": 98.079,
730
  "HNO3": 63.012,
731
+ "CH3COOH": 60.052,
732
  "NaOH": 39.997,
733
  "KOH": 56.105,
734
  "Ca(OH)2": 74.092,
735
  "Na2CO3": 105.988,
736
  }
737
 
 
 
 
 
 
 
 
 
 
 
738
  try:
739
  ck = float(known_concentration)
740
  vk = float(known_volume)
741
  vu = float(unknown_volume)
742
+ mass = float(sample_mass or 0)
743
+ total_volume = float(total_prepared_volume or 0)
744
+ acid_coeff_custom = float(custom_acid_coefficient)
745
+ base_coeff_custom = float(custom_base_coefficient)
746
+ custom_molar_mass = float(custom_unknown_molar_mass or 0)
747
  except (TypeError, ValueError):
748
+ return "Input error: Please enter valid numerical values."
749
 
750
+ values = [
751
+ ck, vk, vu, mass, total_volume,
752
+ acid_coeff_custom, base_coeff_custom, custom_molar_mass
753
+ ]
754
 
755
  if not all(math.isfinite(v) for v in values):
756
+ return "Input error: All numerical values must be finite."
757
 
758
  if ck <= 0 or vk <= 0 or vu <= 0:
759
+ return "Input error: Known concentration and both volumes must be positive."
760
+
761
+ custom_pair = acid == "Custom" or base == "Custom"
762
 
763
+ if custom_pair:
764
+ if not custom_equation.strip():
765
+ return (
766
+ "Custom reaction required: enter the balanced equation "
767
+ "and verify its coefficients before calculating."
768
+ )
769
+ if (
770
+ acid_coeff_custom <= 0 or base_coeff_custom <= 0
771
+ or not acid_coeff_custom.is_integer()
772
+ or not base_coeff_custom.is_integer()
773
+ ):
774
+ return "Custom reaction error: Coefficients must be positive integers."
775
+
776
+ acid_coefficient = int(acid_coeff_custom)
777
+ base_coefficient = int(base_coeff_custom)
778
+ equation = custom_equation.strip()
779
+ else:
780
+ reaction = reactions.get((acid, base))
781
+ if reaction is None:
782
+ return (
783
+ f"Unsupported reaction: {acid} + {base}. "
784
+ "Choose a supported pair or use Custom with a verified equation."
785
+ )
786
 
787
+ acid_coefficient, base_coefficient, equation = reaction
 
788
 
789
  if known_solution == "Acid":
790
  known_coefficient = acid_coefficient
 
795
  unknown_coefficient = acid_coefficient
796
  unknown_substance = acid
797
 
798
+ if custom_pair:
799
+ unknown_molar_mass = custom_molar_mass
800
+ else:
801
+ unknown_molar_mass = molar_masses.get(unknown_substance, 0)
 
 
802
 
803
+ if calculation_type == "Mass concentration" and unknown_molar_mass <= 0:
804
+ return (
805
+ "Molar mass required: enter the unknown substance's molar mass "
806
+ "for a custom reaction."
807
+ )
808
 
809
+ steps = [
810
+ "STEP 1 — BALANCED CHEMICAL EQUATION",
811
+ equation,
812
+ "",
813
+ "STEP 2 — IDENTIFY THE MOLE RATIO",
814
+ f"Known solution coefficient = {known_coefficient}",
815
+ f"Unknown solution coefficient = {unknown_coefficient}",
816
+ (
817
+ f"Mole ratio (unknown : known) = "
818
+ f"{unknown_coefficient} : {known_coefficient}"
819
+ ),
820
+ "",
821
+ "STEP 3 — CONVERT THE VOLUMES TO dm³",
822
+ f"Known volume = {vk:.2f} mL ÷ 1000 = {vk / 1000:.5f} dm³",
823
+ f"Unknown aliquot = {vu:.2f} mL ÷ 1000 = {vu / 1000:.5f} dm³",
824
+ "",
825
+ "STEP 4 — CALCULATE MOLES OF THE KNOWN SOLUTION",
826
+ "Formula: n = C × V",
827
+ f"n(known) = {ck:.5f} mol/dm³ × {vk / 1000:.5f} dm³",
828
  ]
829
 
830
+ known_moles = ck * vk / 1000
831
+ unknown_moles = known_moles * unknown_coefficient / known_coefficient
832
+ unknown_concentration = unknown_moles / (vu / 1000)
833
+
834
+ steps.extend([
835
+ f"n(known) = {known_moles:.6f} mol",
836
+ "",
837
+ "STEP 5 — USE THE MOLE RATIO",
838
+ (
839
+ f"n(unknown) = {known_moles:.6f} × "
840
+ f"{unknown_coefficient}/{known_coefficient}"
841
+ ),
842
+ f"n(unknown) = {unknown_moles:.6f} mol",
843
+ "",
844
+ "STEP 6 — CALCULATE THE UNKNOWN MOLAR CONCENTRATION",
845
+ "Formula: C = n ÷ V",
846
+ (
847
+ f"C(unknown) = {unknown_moles:.6f} mol ÷ "
848
+ f"{vu / 1000:.5f} dm³"
849
+ ),
850
+ f"C(unknown) = {unknown_concentration:.4f} mol/dm³",
851
+ ])
852
+
853
  if calculation_type == "Molar concentration":
854
+ steps.append("\nFINAL ANSWER: Molar concentration calculated above.")
855
+ return "\n".join(steps)
856
 
857
  if calculation_type == "Mass concentration":
858
+ mass_concentration = unknown_concentration * unknown_molar_mass
859
+ steps.extend([
860
+ "",
861
+ "STEP 7 — CONVERT TO MASS CONCENTRATION",
862
+ "Formula: mass concentration = molar concentration × molar mass",
863
+ (
864
+ f"= {unknown_concentration:.4f} mol/dm³ × "
865
+ f"{unknown_molar_mass:.4f} g/mol"
866
+ ),
867
+ f"= {mass_concentration:.4f} g/dm³",
868
+ "",
869
+ f"FINAL ANSWER: {mass_concentration:.4f} g/dm³",
870
+ ])
871
+ return "\n".join(steps)
872
 
873
+ if calculation_type in ("Percentage purity", "Water of crystallisation"):
 
 
 
 
 
874
  if base != "Na2CO3" or known_solution != "Acid":
875
  return (
876
  "This calculation currently requires sodium carbonate "
877
  "as the unknown base and a known acid as the titrant."
878
  )
879
 
880
+ if mass <= 0 or total_volume <= 0 or vu > total_volume:
881
+ return (
882
+ "Input error: For purity/hydration, enter a positive sample "
883
+ "mass and total prepared volume. The aliquot must not exceed it."
884
+ )
885
 
886
+ total_moles = unknown_moles * total_volume / vu
887
  anhydrous_mass = total_moles * molar_masses["Na2CO3"]
888
 
889
+ steps.extend([
890
+ "",
891
+ "STEP 7 — SCALE THE ALIQUOT RESULT TO THE FULL PREPARED SOLUTION",
892
+ (
893
+ f"Total moles = {unknown_moles:.6f} × "
894
+ f"({total_volume:.2f}/{vu:.2f})"
895
+ ),
896
+ f"Total moles of Na₂CO₃ = {total_moles:.6f} mol",
897
+ "",
898
+ "STEP 8 — CALCULATE THE MASS OF ANHYDROUS Na₂CO₃",
899
+ "Formula: mass = moles × molar mass",
900
+ (
901
+ f"= {total_moles:.6f} mol × "
902
+ f"{molar_masses['Na2CO3']:.3f} g/mol"
903
+ ),
904
+ f"= {anhydrous_mass:.4f} g",
905
+ ])
906
 
907
  if anhydrous_mass > mass:
908
  return (
909
+ "\n".join(steps)
910
+ + "\n\nCHECK THE DATA: Calculated anhydrous mass exceeds "
911
+ "the original sample mass."
912
  )
913
 
914
+ if calculation_type == "Percentage purity":
915
+ purity = anhydrous_mass / mass * 100
916
+ steps.extend([
917
+ "",
918
+ "STEP 9 — CALCULATE PERCENTAGE PURITY",
919
+ "Formula: purity (%) = pure mass ÷ sample mass × 100",
920
+ f"= {anhydrous_mass:.4f} g ÷ {mass:.4f} g × 100",
921
+ f"= {purity:.2f}%",
922
+ "",
923
+ f"FINAL ANSWER: Percentage purity = {purity:.2f}%",
924
+ ])
925
+ if purity > 100.5:
926
+ steps.append(
927
+ "WARNING: Purity exceeds 100%; investigate measurement, "
928
+ "preparation, reaction, or interference errors."
929
+ )
930
+ return "\n".join(steps)
931
+
932
  water_mass = mass - anhydrous_mass
933
  water_moles = water_mass / 18.015
934
+ x = water_moles / total_moles if total_moles > 0 else float("nan")
935
+
936
+ if not math.isfinite(x) or x < 0:
937
+ return "Error: Experimental data cannot give a valid hydration number."
938
+
939
+ steps.extend([
940
+ "",
941
+ "STEP 9 — CALCULATE THE WATER MASS",
942
+ f"Water mass = sample mass − anhydrous mass",
943
+ f"= {mass:.4f} g − {anhydrous_mass:.4f} g",
944
+ f"= {water_mass:.4f} g",
945
+ "",
946
+ "STEP 10 — CALCULATE THE MOLES OF WATER",
947
+ "Moles of water = water mass ÷ 18.015 g/mol",
948
+ f"= {water_mass:.4f} g ÷ 18.015 g/mol",
949
+ f"= {water_moles:.6f} mol",
950
+ "",
951
+ "STEP 11 — CALCULATE THE WATER OF CRYSTALLISATION",
952
+ "Formula: x = moles of water ÷ moles of Na₂CO₃",
953
+ f"x = {water_moles:.6f} ÷ {total_moles:.6f}",
954
+ f"x = {x:.3f}",
955
+ "",
956
+ f"FINAL RESULT: Experimental hydration number x ≈ {x:.3f}",
957
+ "Do not automatically round x to a whole number. Check the "
958
+ "experimental uncertainty and whether the sample is a pure hydrate."
959
+ ])
960
+ return "\n".join(steps)
961
 
962
  return "Error: Select a supported calculation type."
963
 
964
 
965
 
966
+
967
  def test_calculate_concentration():
968
  tests = [
969
  {