Skip to content

Commit

Permalink
Fix ObservationEvents infinite loop (#110)
Browse files Browse the repository at this point in the history
In some cases new iterations of rise-transit-set calculations are not
more precise than the previous ones. Quite the opposite, it is
apparently possible to loop indefinitely over some values without ever
crossing the targeted precision.

This introduces a limit of 5 iterations. Passed this limit, it is
admitted that the program in its current form will not achieve a better
precision.

Fixes #109
  • Loading branch information
rhannequin authored Nov 22, 2024
1 parent ddc6583 commit 7a7a3d1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/astronoby/events/observation_events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class ObservationEvents
RISING_SETTING_HOUR_ANGLE_RATIO_RANGE = (-1..1)
EARTH_SIDEREAL_ROTATION_RATE = 360.98564736629
ITERATION_PRECISION = 0.0001
ITERATION_LIMIT = 5

attr_reader :rising_time,
:rising_azimuth,
Expand Down Expand Up @@ -89,10 +90,11 @@ def compute

def iterate(initial_rising, initial_transit, initial_setting)
delta = 1
iteration = 1
corrected_rising = initial_rising
corrected_transit = initial_transit
corrected_setting = initial_setting
until delta < ITERATION_PRECISION
until delta < ITERATION_PRECISION || iteration > ITERATION_LIMIT
iterate = RiseTransitSetIteration.new(
observer: @observer,
date: @date,
Expand All @@ -108,6 +110,7 @@ def iterate(initial_rising, initial_transit, initial_setting)
corrected_rising = rationalize_decimal_time corrected_rising + iterate[0]
corrected_transit = rationalize_decimal_time corrected_transit + iterate[1]
corrected_setting = rationalize_decimal_time corrected_setting + iterate[2]
iteration += 1
end
[corrected_rising, corrected_transit, corrected_setting]
end
Expand Down
16 changes: 15 additions & 1 deletion spec/astronoby/bodies/moon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@
end
end

it "returns the sunrise time on 1991-03-14" do
it "returns the moonrise time on 1991-03-14" do
time = Time.utc(1991, 3, 14)
observer = Astronoby::Observer.new(
latitude: Astronoby::Angle.from_degrees(48.8566),
Expand All @@ -431,6 +431,20 @@
# Time from IMCCE: 1991-03-14T05:08:08
end

it "returns moonrise time on 2024-11-10" do
time = Time.utc(2024, 11, 10)
observer = Astronoby::Observer.new(
latitude: Astronoby::Angle.from_degrees(41.02054),
longitude: Astronoby::Angle.from_degrees(-72.15608)
)
moon = described_class.new(time: time)
observation_events = moon.observation_events(observer: observer)

rising_time = observation_events.rising_time

expect(rising_time).to eq Time.utc(2024, 11, 10, 18, 49, 45)
end

describe "#rising_azimuth" do
it "returns the moonrise azimuth on 2024-05-31" do
time = Time.utc(2024, 5, 31)
Expand Down

0 comments on commit 7a7a3d1

Please sign in to comment.