forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshear_stress.py
More file actions
76 lines (58 loc) · 2.42 KB
/
shear_stress.py
File metadata and controls
76 lines (58 loc) · 2.42 KB
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
from __future__ import annotations
"""
In foundational mechanics, shear stress is defined as a stress component that acts
parallel to the material's cross-section. It arises from a shear force, which is the
component of the force vector acting coplanar with the cross-section.
The relationship is governed by the core formula: tau = F / A
where:
tau (τ) is the shear stress
F is the tangential force (shear force)
A is the cross-sectional area
Reference: https://en.wikipedia.org/wiki/Shear_stress
"""
def calculate_shear_stress(
shear_stress: float | None = None,
tangential_force: float | None = None,
area: float | None = None,
) -> float:
"""
Calculates the missing variable in the shear stress formula (tau = F / A).
Exactly two of the parameters must be provided.
Args:
shear_stress: The stress parallel to the cross-section (Pascal).
tangential_force: The force acting parallel to the cross-section (Newton).
area: The cross-sectional area (Square meters).
Returns:
The calculated missing value (shear_stress, tangential_force, or area).
Raises:
ValueError: If fewer or more than two parameters are provided,
if a parameter is negative, or if a division by zero occurs.
Examples:
>>> calculate_shear_stress(tangential_force=100.0, area=4.0)
25.0
>>> calculate_shear_stress(shear_stress=8.0, area=200.0)
1600.0
>>> calculate_shear_stress(shear_stress=1000.0, tangential_force=1200000.0)
1200.0
"""
params = (shear_stress, tangential_force, area)
none_count = params.count(None)
if none_count != 1:
raise ValueError("Exactly two values must be provided to calculate the third.")
# Validation: Physically, these values should not be negative in this context
if any(p is not None and p < 0 for p in params):
raise ValueError("Parameters cannot be negative.")
if shear_stress is None:
if area == 0:
raise ValueError("Area cannot be zero when calculating stress.")
return tangential_force / area
if tangential_force is None:
return shear_stress * area
if area is None:
if shear_stress == 0:
raise ValueError("Shear stress cannot be zero when calculating area.")
return tangential_force / shear_stress
return 0.0
if __name__ == "__main__":
import doctest
doctest.testmod()