Skip to content

Commit 79b1217

Browse files
ShivaDahal99jlhuhncclausspre-commit-ci[bot]
authored andcommitted
Speed of sound (TheAlgorithms#8803)
* Create TestShiva * Delete TestShiva * Add speed of sound * Update physics/speed_of_sound.py Co-authored-by: Christian Clauss <[email protected]> * Update physics/speed_of_sound.py Co-authored-by: Christian Clauss <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update speed_of_sound.py * Update speed_of_sound.py --------- Co-authored-by: jlhuhn <[email protected]> Co-authored-by: Christian Clauss <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent c6e52a2 commit 79b1217

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

Diff for: physics/speed_of_sound.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
Title : Calculating the speed of sound
3+
4+
Description :
5+
The speed of sound (c) is the speed that a sound wave travels
6+
per unit time (m/s). During propagation, the sound wave propagates
7+
through an elastic medium. Its SI unit is meter per second (m/s).
8+
9+
Only longitudinal waves can propagate in liquids and gas other then
10+
solid where they also travel in transverse wave. The following Algo-
11+
rithem calculates the speed of sound in fluid depanding on the bulk
12+
module and the density of the fluid.
13+
14+
Equation for calculating speed od sound in fluid:
15+
c_fluid = (K_s*p)**0.5
16+
17+
c_fluid: speed of sound in fluid
18+
K_s: isentropic bulk modulus
19+
p: density of fluid
20+
21+
22+
23+
Source : https://en.wikipedia.org/wiki/Speed_of_sound
24+
"""
25+
26+
27+
def speed_of_sound_in_a_fluid(density: float, bulk_modulus: float) -> float:
28+
"""
29+
This method calculates the speed of sound in fluid -
30+
This is calculated from the other two provided values
31+
Examples:
32+
Example 1 --> Water 20°C: bulk_moduls= 2.15MPa, density=998kg/m³
33+
Example 2 --> Murcery 20°: bulk_moduls= 28.5MPa, density=13600kg/m³
34+
35+
>>> speed_of_sound_in_a_fluid(bulk_modulus=2.15*10**9, density=998)
36+
1467.7563207952705
37+
>>> speed_of_sound_in_a_fluid(bulk_modulus=28.5*10**9, density=13600)
38+
1447.614670861731
39+
"""
40+
41+
if density <= 0:
42+
raise ValueError("Impossible fluid density")
43+
if bulk_modulus <= 0:
44+
raise ValueError("Impossible bulk modulus")
45+
46+
return (bulk_modulus / density) ** 0.5
47+
48+
49+
if __name__ == "__main__":
50+
import doctest
51+
52+
doctest.testmod()

0 commit comments

Comments
 (0)