|
| 1 | +""" |
| 2 | +Title: Graham's Law of Effusion |
| 3 | +
|
| 4 | +Description: Graham's law of effusion states that the rate of effusion of a gas is |
| 5 | +inversely proportional to the square root of the molar mass of its particles: |
| 6 | +
|
| 7 | +r1/r2 = sqrt(m2/m1) |
| 8 | +
|
| 9 | +r1 = Rate of effusion for the first gas. |
| 10 | +r2 = Rate of effusion for the second gas. |
| 11 | +m1 = Molar mass of the first gas. |
| 12 | +m2 = Molar mass of the second gas. |
| 13 | +
|
| 14 | +(Description adapted from https://en.wikipedia.org/wiki/Graham%27s_law) |
| 15 | +""" |
| 16 | + |
| 17 | +from math import pow, sqrt |
| 18 | + |
| 19 | + |
| 20 | +def validate(*values) -> bool: |
| 21 | + for value in values: |
| 22 | + if value >= 0.0: |
| 23 | + return True |
| 24 | + else: |
| 25 | + raise ValueError( |
| 26 | + """Invalid inputs. Effusion rates and molar masses must be a positive |
| 27 | + value.""" |
| 28 | + ) |
| 29 | + |
| 30 | + return False |
| 31 | + |
| 32 | + |
| 33 | +def effusion_ratio(molar_mass_1: float, molar_mass_2: float) -> float: |
| 34 | + """ |
| 35 | + Input Parameters: |
| 36 | + ----------------- |
| 37 | + molar_mass_1: Molar mass of the first gas (g/mol, kg/kmol, etc.) |
| 38 | + molar_mass_2: Molar mass of the second gas (g/mol, kg/kmol, etc.) |
| 39 | +
|
| 40 | + Returns: |
| 41 | + -------- |
| 42 | + >>> effusion_ratio(2.016, 4.002) |
| 43 | + 1.408943 |
| 44 | + >>> effusion_ratio(-2.016, 4.002) |
| 45 | + Traceback (most recent call last): |
| 46 | + ... |
| 47 | + ValueError: Invalid inputs. Effusion rates and molar masses must be a positive |
| 48 | + value. |
| 49 | + >>> effusion_ratio(2.016) |
| 50 | + Traceback (most recent call last): |
| 51 | + ... |
| 52 | + TypeError: effusion_ratio() missing 1 required positional argument: 'molar_mass_2' |
| 53 | + """ |
| 54 | + validate(molar_mass_1, molar_mass_2) |
| 55 | + return round(sqrt(molar_mass_2 / molar_mass_1), 6) |
| 56 | + |
| 57 | + |
| 58 | +def first_effusion_rate( |
| 59 | + effusion_rate: float, molar_mass_1: float, molar_mass_2: float |
| 60 | +) -> float: |
| 61 | + """ |
| 62 | + Input Parameters: |
| 63 | + ----------------- |
| 64 | + effusion_rate: Effustion rate of second gas (m^2/s, mm^2/s, etc.) |
| 65 | + molar_mass_1: Molar mass of the first gas (g/mol, kg/kmol, etc.) |
| 66 | + molar_mass_2: Molar mass of the second gas (g/mol, kg/kmol, etc.) |
| 67 | +
|
| 68 | + Returns: |
| 69 | + -------- |
| 70 | + >>> first_effusion_rate(1, 2.016, 4.002) |
| 71 | + 1.408943 |
| 72 | + >>> first_effusion_rate(-1, 2.016, 4.002) |
| 73 | + Traceback (most recent call last): |
| 74 | + ... |
| 75 | + ValueError: Invalid inputs. Effusion rates and molar masses must be a positive |
| 76 | + value. |
| 77 | + >>> first_effusion_rate(1) |
| 78 | + Traceback (most recent call last): |
| 79 | + ... |
| 80 | + TypeError: first_effusion_rate() missing 2 required positional arguments: |
| 81 | + 'molar_mass_1' and 'molar_mass_2' |
| 82 | + >>> first_effusion_rate(1, 2.016) |
| 83 | + Traceback (most recent call last): |
| 84 | + ... |
| 85 | + TypeError: first_effusion_rate() missing 1 required positional argument: |
| 86 | + 'molar_mass_2' |
| 87 | + """ |
| 88 | + validate(effusion_rate, molar_mass_1, molar_mass_2) |
| 89 | + return round(effusion_rate * sqrt(molar_mass_2 / molar_mass_1), 6) |
| 90 | + |
| 91 | + |
| 92 | +def second_effusion_rate( |
| 93 | + effusion_rate: float, molar_mass_1: float, molar_mass_2: float |
| 94 | +) -> float: |
| 95 | + """ |
| 96 | + Input Parameters: |
| 97 | + ----------------- |
| 98 | + effusion_rate: Effustion rate of second gas (m^2/s, mm^2/s, etc.) |
| 99 | + molar_mass_1: Molar mass of the first gas (g/mol, kg/kmol, etc.) |
| 100 | + molar_mass_2: Molar mass of the second gas (g/mol, kg/kmol, etc.) |
| 101 | +
|
| 102 | + Returns: |
| 103 | + -------- |
| 104 | + >>> second_effusion_rate(1, 2.016, 4.002) |
| 105 | + 0.709752 |
| 106 | + >>> second_effusion_rate(-1, 2.016, 4.002) |
| 107 | + Traceback (most recent call last): |
| 108 | + ... |
| 109 | + ValueError: Invalid inputs. Effusion rates and molar masses must be a positive |
| 110 | + value. |
| 111 | + >>> second_effusion_rate(1) |
| 112 | + Traceback (most recent call last): |
| 113 | + ... |
| 114 | + TypeError: second_effusion_rate() missing 2 required positional arguments: |
| 115 | + 'molar_mass_1' and 'molar_mass_2' |
| 116 | + >>> second_effusion_rate(1, 2.016) |
| 117 | + Traceback (most recent call last): |
| 118 | + ... |
| 119 | + TypeError: second_effusion_rate() missing 1 required positional argument: |
| 120 | + 'molar_mass_2' |
| 121 | + """ |
| 122 | + validate(effusion_rate, molar_mass_1, molar_mass_2) |
| 123 | + return round(effusion_rate / sqrt(molar_mass_2 / molar_mass_1), 6) |
| 124 | + |
| 125 | + |
| 126 | +def first_molar_mass( |
| 127 | + molar_mass: float, effusion_rate_1: float, effusion_rate_2: float |
| 128 | +) -> float: |
| 129 | + """ |
| 130 | + Input Parameters: |
| 131 | + ----------------- |
| 132 | + molar_mass: Molar mass of the first gas (g/mol, kg/kmol, etc.) |
| 133 | + effusion_rate_1: Effustion rate of first gas (m^2/s, mm^2/s, etc.) |
| 134 | + effusion_rate_2: Effustion rate of second gas (m^2/s, mm^2/s, etc.) |
| 135 | +
|
| 136 | + Returns: |
| 137 | + -------- |
| 138 | + >>> first_molar_mass(2, 1.408943, 0.709752) |
| 139 | + 0.507524 |
| 140 | + >>> first_molar_mass(-1, 2.016, 4.002) |
| 141 | + Traceback (most recent call last): |
| 142 | + ... |
| 143 | + ValueError: Invalid inputs. Effusion rates and molar masses must be a positive |
| 144 | + value. |
| 145 | + >>> first_molar_mass(1) |
| 146 | + Traceback (most recent call last): |
| 147 | + ... |
| 148 | + TypeError: first_molar_mass() missing 2 required positional arguments: |
| 149 | + 'effusion_rate_1' and 'effusion_rate_2' |
| 150 | + >>> first_molar_mass(1, 2.016) |
| 151 | + Traceback (most recent call last): |
| 152 | + ... |
| 153 | + TypeError: first_molar_mass() missing 1 required positional argument: |
| 154 | + 'effusion_rate_2' |
| 155 | + """ |
| 156 | + validate(molar_mass, effusion_rate_1, effusion_rate_2) |
| 157 | + return round(molar_mass / pow(effusion_rate_1 / effusion_rate_2, 2), 6) |
| 158 | + |
| 159 | + |
| 160 | +def second_molar_mass( |
| 161 | + molar_mass: float, effusion_rate_1: float, effusion_rate_2: float |
| 162 | +) -> float: |
| 163 | + """ |
| 164 | + Input Parameters: |
| 165 | + ----------------- |
| 166 | + molar_mass: Molar mass of the first gas (g/mol, kg/kmol, etc.) |
| 167 | + effusion_rate_1: Effustion rate of first gas (m^2/s, mm^2/s, etc.) |
| 168 | + effusion_rate_2: Effustion rate of second gas (m^2/s, mm^2/s, etc.) |
| 169 | +
|
| 170 | + Returns: |
| 171 | + -------- |
| 172 | + >>> second_molar_mass(2, 1.408943, 0.709752) |
| 173 | + 1.970351 |
| 174 | + >>> second_molar_mass(-2, 1.408943, 0.709752) |
| 175 | + Traceback (most recent call last): |
| 176 | + ... |
| 177 | + ValueError: Invalid inputs. Effusion rates and molar masses must be a positive |
| 178 | + value. |
| 179 | + >>> second_molar_mass(1) |
| 180 | + Traceback (most recent call last): |
| 181 | + ... |
| 182 | + TypeError: second_molar_mass() missing 2 required positional arguments: |
| 183 | + 'effusion_rate_1' and 'effusion_rate_2' |
| 184 | + >>> second_molar_mass(1, 2.016) |
| 185 | + Traceback (most recent call last): |
| 186 | + ... |
| 187 | + TypeError: second_molar_mass() missing 1 required positional argument: |
| 188 | + 'effusion_rate_2' |
| 189 | + """ |
| 190 | + validate(molar_mass, effusion_rate_1, effusion_rate_2) |
| 191 | + return round(pow(effusion_rate_1 / effusion_rate_2, 2) / molar_mass, 6) |
0 commit comments