1010/// All constants are provided as Math values in SI units unless otherwise noted.
1111/// Units are documented in the constant descriptions.
1212public struct AstronomyConstants : Sendable {
13+ private static func compoundUnit(
14+ name: String ,
15+ symbol: String ,
16+ components: [ ( unit: Unit , exponent: Int ) ]
17+ ) -> Unit {
18+ Unit . compound ( name: name, symbol: symbol, components: components, notes: name)
19+ }
1320
1421 // MARK: - Cosmological Constants
1522
@@ -21,107 +28,187 @@ public struct AstronomyConstants: Sendable {
2128 /// Alias for Hubble constant
2229 public static let H ₀ = hubbleConstant
2330
31+ /// Hubble constant with units (km/s/Mpc)
32+ public static let hubbleConstantUnit = MathUnit (
33+ hubbleConstant,
34+ compoundUnit (
35+ name: " hubble constant " ,
36+ symbol: " km/(s·Mpc) " ,
37+ components: [
38+ ( StandardUnits . kilometer, 1 ) ,
39+ ( StandardUnits . second, - 1 ) ,
40+ ( StandardUnits . megaparsec, - 1 )
41+ ]
42+ )
43+ )
44+
45+ /// Alias for Hubble constant with units
46+ public static let H ₀Unit = hubbleConstantUnit
47+
2448 /// Speed of light (c)
2549 /// Value: 299,792,458 m/s
2650 public static let c = PhysicsConstants . c
2751
2852 /// Alias for speed of light
2953 public static let speedOfLight = c
3054
55+ /// Speed of light with units
56+ public static let speedOfLightUnit = PhysicsConstants . cUnit
57+
3158 // MARK: - Solar System - Sun
3259
3360 /// Solar mass (M☉)
3461 /// Value: 1.98892 × 10³⁰ kg
3562 public static let solarMass = Math ( 1.98892e30 )
3663
64+ /// Solar mass with units
65+ public static let solarMassUnit = MathUnit ( solarMass, StandardUnits . kilogram)
66+
3767 /// Solar radius (R☉)
3868 /// Value: 6.96 × 10⁸ m (696,000 km)
3969 public static let solarRadius = Math ( 6.96e8 )
4070
71+ /// Solar radius with units
72+ public static let solarRadiusUnit = MathUnit ( solarRadius, StandardUnits . meter)
73+
4174 /// Solar luminosity (L☉)
4275 /// Value: 3.828 × 10²⁶ W
4376 public static let solarLuminosity = Math ( 3.828e26 )
4477
78+ /// Solar luminosity with units
79+ public static let solarLuminosityUnit = MathUnit ( solarLuminosity, StandardUnits . watt)
80+
4581 /// Solar temperature (effective surface temperature)
4682 /// Value: 5778 K
4783 public static let solarTemperature = Math ( 5778 )
4884
85+ /// Solar temperature with units
86+ public static let solarTemperatureUnit = MathUnit ( solarTemperature, StandardUnits . kelvin)
87+
4988 // MARK: - Solar System - Earth
5089
5190 /// Earth mass (M⊕)
5291 /// Value: 5.97237 × 10²⁴ kg
5392 public static let earthMass = Math ( 5.97237e24 )
5493
94+ /// Earth mass with units
95+ public static let earthMassUnit = MathUnit ( earthMass, StandardUnits . kilogram)
96+
5597 /// Earth radius (mean)
5698 /// Value: 6.371 × 10⁶ m (6,371 km)
5799 public static let earthRadius = Math ( 6.371e6 )
58100
101+ /// Earth radius with units
102+ public static let earthRadiusUnit = MathUnit ( earthRadius, StandardUnits . meter)
103+
59104 /// Earth equatorial radius
60105 /// Value: 6.3781 × 10⁶ m (6,378.1 km)
61106 public static let earthEquatorialRadius = Math ( 6.3781e6 )
62107
108+ /// Earth equatorial radius with units
109+ public static let earthEquatorialRadiusUnit = MathUnit ( earthEquatorialRadius, StandardUnits . meter)
110+
63111 /// Earth polar radius
64112 /// Value: 6.3568 × 10⁶ m (6,356.8 km)
65113 public static let earthPolarRadius = Math ( 6.3568e6 )
66114
115+ /// Earth polar radius with units
116+ public static let earthPolarRadiusUnit = MathUnit ( earthPolarRadius, StandardUnits . meter)
117+
67118 /// Standard gravity on Earth (g)
68119 /// Value: 9.80665 m/s² (exact, standard)
69120 public static let standardGravity = Math ( 9.80665 )
70121
71122 /// Alias for standard gravity
72123 public static let g = standardGravity
73124
125+ /// Standard gravity with units
126+ public static let standardGravityUnit = MathUnit ( standardGravity, StandardUnits . meterPerSecondSquared)
127+
74128 /// Astronomical unit (AU) - Earth-Sun mean distance
75129 /// Value: 1.495978707 × 10¹¹ m (exact)
76130 public static let astronomicalUnit = Math ( 1.495978707e11 )
77131
78132 /// Alias for astronomical unit
79133 public static let AU = astronomicalUnit
80134
135+ /// Astronomical unit with units
136+ public static let astronomicalUnitUnit = MathUnit ( astronomicalUnit, StandardUnits . meter)
137+
81138 /// Earth orbital period (sidereal year)
82139 /// Value: 365.256363004 days
83140 public static let siderealYear = Math ( 365.256363004 )
84141
142+ /// Sidereal year with units
143+ public static let siderealYearUnit = MathUnit ( siderealYear, StandardUnits . day)
144+
85145 /// Tropical year (equinox to equinox)
86146 /// Value: 365.24219 days
87147 public static let tropicalYear = Math ( 365.24219 )
88148
149+ /// Tropical year with units
150+ public static let tropicalYearUnit = MathUnit ( tropicalYear, StandardUnits . day)
151+
89152 /// Earth's orbital velocity (mean)
90153 /// Value: 29,780 m/s (29.78 km/s)
91154 public static let earthOrbitalVelocity = Math ( 29780 )
92155
156+ /// Earth's orbital velocity with units
157+ public static let earthOrbitalVelocityUnit = MathUnit ( earthOrbitalVelocity, StandardUnits . meterPerSecond)
158+
93159 // MARK: - Solar System - Moon
94160
95161 /// Lunar mass (M☾)
96162 /// Value: 7.342 × 10²² kg
97163 public static let lunarMass = Math ( 7.342e22 )
98164
165+ /// Lunar mass with units
166+ public static let lunarMassUnit = MathUnit ( lunarMass, StandardUnits . kilogram)
167+
99168 /// Lunar radius (mean)
100169 /// Value: 1.7374 × 10⁶ m (1,737.4 km)
101170 public static let lunarRadius = Math ( 1.7374e6 )
102171
172+ /// Lunar radius with units
173+ public static let lunarRadiusUnit = MathUnit ( lunarRadius, StandardUnits . meter)
174+
103175 /// Earth-Moon mean distance
104176 /// Value: 3.844 × 10⁸ m (384,400 km)
105177 public static let earthMoonDistance = Math ( 3.844e8 )
106178
179+ /// Earth-Moon distance with units
180+ public static let earthMoonDistanceUnit = MathUnit ( earthMoonDistance, StandardUnits . meter)
181+
107182 /// Lunar orbital period (sidereal month)
108183 /// Value: 27.321661 days
109184 public static let siderealMonth = Math ( 27.321661 )
110185
186+ /// Sidereal month with units
187+ public static let siderealMonthUnit = MathUnit ( siderealMonth, StandardUnits . day)
188+
111189 /// Synodic month (new moon to new moon)
112190 /// Value: 29.530589 days
113191 public static let synodicMonth = Math ( 29.530589 )
114192
193+ /// Synodic month with units
194+ public static let synodicMonthUnit = MathUnit ( synodicMonth, StandardUnits . day)
195+
115196 // MARK: - Solar System - Other Planets
116197
117198 /// Jupiter mass (M♃)
118199 /// Value: 1.8982 × 10²⁷ kg
119200 public static let jupiterMass = Math ( 1.8982e27 )
120201
202+ /// Jupiter mass with units
203+ public static let jupiterMassUnit = MathUnit ( jupiterMass, StandardUnits . kilogram)
204+
121205 /// Jupiter radius (equatorial)
122206 /// Value: 7.1492 × 10⁷ m (71,492 km)
123207 public static let jupiterRadius = Math ( 7.1492e7 )
124208
209+ /// Jupiter radius with units
210+ public static let jupiterRadiusUnit = MathUnit ( jupiterRadius, StandardUnits . meter)
211+
125212 // MARK: - Astronomical Distances
126213
127214 /// Light-year (ly)
@@ -131,27 +218,39 @@ public struct AstronomyConstants: Sendable {
131218 /// Alias for light-year
132219 public static let ly = lightYear
133220
221+ /// Light-year with units
222+ public static let lightYearUnit = MathUnit ( lightYear, StandardUnits . meter)
223+
134224 /// Parsec (pc)
135225 /// Value: 3.0856775814913673 × 10¹⁶ m
136226 public static let parsec = Math ( 3.0856775814913673e16 )
137227
138228 /// Alias for parsec
139229 public static let pc = parsec
140230
231+ /// Parsec with units
232+ public static let parsecUnit = MathUnit ( parsec, StandardUnits . meter)
233+
141234 /// Kiloparsec (kpc)
142235 /// Value: 3.0856775814913673 × 10¹⁹ m
143236 public static let kiloparsec = Math ( 3.0856775814913673e19 )
144237
145238 /// Alias for kiloparsec
146239 public static let kpc = kiloparsec
147240
241+ /// Kiloparsec with units
242+ public static let kiloparsecUnit = MathUnit ( kiloparsec, StandardUnits . meter)
243+
148244 /// Megaparsec (Mpc)
149245 /// Value: 3.0856775814913673 × 10²² m
150246 public static let megaparsec = Math ( 3.0856775814913673e22 )
151247
152248 /// Alias for megaparsec
153249 public static let Mpc = megaparsec
154250
251+ /// Megaparsec with units
252+ public static let megaparsecUnit = MathUnit ( megaparsec, StandardUnits . meter)
253+
155254 // MARK: - Calculation Helpers
156255
157256 /// Calculate Schwarzschild radius for a given mass
@@ -172,6 +271,13 @@ public struct AstronomyConstants: Sendable {
172271 return ( 2 * G * mass) / ( c * c)
173272 }
174273
274+ /// Calculate Schwarzschild radius for a given mass with units
275+ public static func schwarzschildRadius( mass: MathUnit ) -> MathUnit ? {
276+ guard let m = mass. converted ( to: StandardUnits . kilogram) else { return nil }
277+ let rs = schwarzschildRadius ( mass: m. value)
278+ return MathUnit ( rs, StandardUnits . meter)
279+ }
280+
175281 /// Calculate escape velocity from a celestial body
176282 /// Formula: v_esc = √(2GM/r)
177283 ///
@@ -192,4 +298,12 @@ public struct AstronomyConstants: Sendable {
192298 let G = PhysicsConstants . G
193299 return Math . sqrt ( ( 2 * G * mass) / radius)
194300 }
301+
302+ /// Calculate escape velocity for a given mass and radius with units
303+ public static func escapeVelocity( mass: MathUnit , radius: MathUnit ) -> MathUnit ? {
304+ guard let m = mass. converted ( to: StandardUnits . kilogram) else { return nil }
305+ guard let r = radius. converted ( to: StandardUnits . meter) else { return nil }
306+ let v = escapeVelocity ( mass: m. value, radius: r. value)
307+ return MathUnit ( v, StandardUnits . meterPerSecond)
308+ }
195309}
0 commit comments