aboutsummaryrefslogtreecommitdiffstats
path: root/solar.c
diff options
context:
space:
mode:
Diffstat (limited to 'solar.c')
-rw-r--r--solar.c258
1 files changed, 133 insertions, 125 deletions
diff --git a/solar.c b/solar.c
index ed9cf93..6d8b6dd 100644
--- a/solar.c
+++ b/solar.c
@@ -3,6 +3,7 @@
#include <math.h>
#include <time.h>
#include <errno.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@@ -21,228 +22,250 @@
/**
- * Get current Julian Centuries time (100 Julian days since J2000)
+ * Get current Julian Centuries time (100 Julian Days since J2000)
+ * and Julian Day time
*
- * @param nowp Output parameter for the current Julian Centuries time
- * @return 0 on success, -1 on failure
- * @throws Any error specified for clock_gettime(3) on error
+ * @param tc_out Output parameter for the current Julian Centuries time
+ * @param td_out Output parameter for the current Julian Day time
+ * @return 0 on success, -1 on failure
+ * @throws Any error specified for clock_gettime(3) on error
*/
static int
-julian_centuries(double *nowp)
+julian_time(double *tc_out, double *td_out)
{
struct timespec now;
+ double tu;
if (clock_gettime(CLOCK_REALTIME_COARSE, &now))
return -1;
- *nowp = (double)(now.tv_nsec) / 1000000000.0 + (double)(now.tv_sec);
- *nowp = (*nowp / 86400.0 + 2440587.5 - 2451545.0) / 36525.0;
+ tu = fma((double)now.tv_nsec, 0.000000001, (double)now.tv_sec);
+ *td_out = tu / 86400.0 + 2440587.5;
+ *tc_out = (*td_out - 2451545.0) / 36525.0;
return 0;
}
+
/**
- * Convert a Julian Centuries timestamp to a Julian Day timestamp
+ * Convert an angle (or otherwise) from degrees to radians
*
- * @param tm The time in Julian Centuries
- * @return The time in Julian Days
+ * @param deg The angle in degrees
+ * @param The angle in radians
*/
-static inline double
-julian_centuries_to_julian_day(double tm)
+static double
+radians(double deg)
{
- return tm * 36525.0 + 2451545.0;
+ return (double)M_PI / 180.0 * deg;
}
+/**
+ * Convert an angle (or otherwise) from radians to degrees
+ *
+ * @param rad The angle in radians
+ * @param The angle in degrees
+ */
+static double
+degrees(double rad)
+{
+ return 180.0 / (double)M_PI * rad;
+}
/**
* Convert an angle (or otherwise) from degrees to radians
+ * and, using fused multply–add, add some number of degrees
*
- * @param deg The angle in degrees.
- * @param The angle in radians.
+ * @param deg The angle in degrees
+ * @param aug The number of radians to add
+ * @param The angle in radians, plus `aug`
*/
-static inline double
-radians(double deg)
+static double
+radians_plus(double deg, double aug)
{
- return deg * (double)M_PI / 180.0;
+ return fma((double)M_PI / 180.0, deg, aug);
}
/**
* Convert an angle (or otherwise) from radians to degrees
+ * and, using fused multply–add, add some number of degrees
*
- * @param rad The angle in radians.
- * @param The angle in degrees.
+ * @param rad The angle in radians
+ * @param aug The number of degrees to add
+ * @param The angle in degrees, plus `aug`
*/
-static inline double
-degrees(double rad)
+static double
+degrees_plus(double rad, double aug)
{
- return rad * 180.0 / (double)M_PI;
+ return fma(180.0 / (double)M_PI, rad, aug);
}
/**
* Calculates the Sun's elevation from the solar hour angle
*
- * @param longitude The longitude in degrees eastwards
- * from Greenwich, negative for westwards
+ * @param latitude The latitude in degrees northwards from
+ * the equator, negative for southwards
* @param declination The declination, in radians
* @param hour_angle The solar hour angle, in radians
* @return The Sun's elevation, in radians
*/
-static inline double
+static double
elevation_from_hour_angle(double latitude, double declination, double hour_angle)
{
- double rc = cos(radians(latitude));
- rc *= cos(hour_angle) * cos(declination);
- rc += sin(radians(latitude)) * sin(declination);
- return asin(rc);
+ double c, s;
+ latitude = radians(latitude);
+ c = cos(latitude) * cos(declination);
+ s = sin(latitude) * sin(declination);
+ return asin(fma(c, cos(hour_angle), s));
}
/**
* Calculates the Sun's geometric mean longitude
*
- * @param tm The time in Julian Centuries
- * @return The Sun's geometric mean longitude in radians
+ * @param t The time in Julian Centuries
+ * @return The Sun's geometric mean longitude in radians
*/
-static inline double
-sun_geometric_mean_longitude(double tm)
+static double
+sun_geometric_mean_longitude(double t)
{
- double rc = fmod(pow(0.0003032 * tm, 2.0) + 36000.76983 * tm + 280.46646, 360.0);
-#if defined(TIMETRAVELLER)
- rc = rc < 0.0 ? (rc + 360.0) : rc;
-#endif
- return radians(rc);
+ return radians(fmod(fma(fma(0.0003032, t, 36000.76983), t, 280.46646), 360.0));
}
/**
* Calculates the Sun's geometric mean anomaly
*
- * @param tm The time in Julian Centuries
- * @return The Sun's geometric mean anomaly in radians
+ * @param t The time in Julian Centuries
+ * @return The Sun's geometric mean anomaly in radians
*/
-static inline double
-sun_geometric_mean_anomaly(double tm)
+static double
+sun_geometric_mean_anomaly(double t)
{
- return radians(pow(-0.0001537 * tm, 2.0) + 35999.05029 * tm + 357.52911);
+ return radians(fmod(fma(fma(-0.0001537, t, 35999.05029), t, 357.52911), 360.0));
}
/**
* Calculates the Earth's orbit eccentricity
*
- * @param tm The time in Julian Centuries
- * @return The Earth's orbit eccentricity
+ * @param t The time in Julian Centuries
+ * @return The Earth's orbit eccentricity
*/
-static inline double
-earth_orbit_eccentricity(double tm)
+static double
+earth_orbit_eccentricity(double t)
{
- return pow(-0.0000001267 * tm, 2.0) - 0.000042037 * tm + 0.016708634;
+ return fma(fma(-0.0000001267, t, -0.000042037), t, 0.016708634);
}
/**
* Calculates the Sun's equation of the centre, the difference
* between the true anomaly and the mean anomaly
*
- * @param tm The time in Julian Centuries
- * @return The Sun's equation of the centre, in radians
+ * @param t The time in Julian Centuries
+ * @return The Sun's equation of the centre, in radians
*/
-static inline double
-sun_equation_of_centre(double tm)
+static double
+sun_equation_of_centre(double t)
{
- double a = sun_geometric_mean_anomaly(tm), rc;
- rc = sin(1.0 * a) * (pow(-0.000014 * tm, 2.0) - 0.004817 * tm + 1.914602);
- rc += sin(2.0 * a) * (-0.000101 * tm + 0.019993);
- rc += sin(3.0 * a) * 0.000289;
- return radians(rc);
+ double a = sun_geometric_mean_anomaly(t), r;
+ r = sin(1.0 * a) * fma(fma(-0.000014, t, -0.004817), t, 1.914602);
+ r = fma(sin(2.0 * a), fma(-0.000101, t, 0.019993), r);
+ r = fma(sin(3.0 * a), 0.000289, r);
+ return radians(r);
}
/**
* Calculates the Sun's real longitudinal position
*
- * @param tm The time in Julian Centuries
- * @return The longitude, in radians
+ * @param t The time in Julian Centuries
+ * @return The longitude, in radians
*/
-static inline double
-sun_real_longitude(double tm)
+static double
+sun_real_longitude(double t)
{
- return sun_geometric_mean_longitude(tm) + sun_equation_of_centre(tm);
+ return sun_geometric_mean_longitude(t) + sun_equation_of_centre(t);
}
/**
* Calculates the Sun's apparent longitudinal position
*
- * @param tm The time in Julian Centuries
- * @return The longitude, in radians
+ * @param t The time in Julian Centuries
+ * @return The longitude, in radians
*/
-static inline double
-sun_apparent_longitude(double tm)
+static double
+sun_apparent_longitude(double t)
{
- double rc = degrees(sun_real_longitude(tm)) - 0.00569;
- return radians(rc - 0.00478 * sin(radians(-1934.136 * tm + 125.04)));
+ double r = degrees_plus(sun_real_longitude(t), -0.00569);
+ double a = radians(fma(-1934.136, t, 125.04));
+ return radians(fma(-0.00478, sin(a), r));
}
/**
* Calculates the mean ecliptic obliquity of the Sun's
* apparent motion without variation correction
*
- * @param tm The time in Julian Centuries
- * @return The uncorrected mean obliquity, in radians
+ * @param t The time in Julian Centuries
+ * @return The uncorrected mean obliquity, in radians
*/
static double
-mean_ecliptic_obliquity(double tm)
+mean_ecliptic_obliquity(double t)
{
- double rc = pow(0.001813 * tm, 3.0) - pow(0.00059 * tm, 2.0) - 46.815 * tm + 21.448;
- return radians(23.0 + (26.0 + rc / 60.0) / 60.0);
+ double r = fma(fma(fma(0.001813, t, -0.00059), t, -46.815), t, 21.448);
+ return radians(23.0 + (26.0 + r / 60.0) / 60.0);
}
/**
* Calculates the mean ecliptic obliquity of the Sun's
* parent motion with variation correction
*
- * @param tm The time in Julian Centuries
- * @return The mean obliquity, in radians
+ * @param t The time in Julian Centuries
+ * @return The mean obliquity, in radians
*/
static double
-corrected_mean_ecliptic_obliquity(double tm)
+corrected_mean_ecliptic_obliquity(double t)
{
- double rc = 0.00256 * cos(radians(-1934.136 * tm + 125.04));
- return radians(rc + degrees(mean_ecliptic_obliquity(tm)));
+ double r = cos(radians(fma(-1934.136, t, 125.04)));
+ return radians_plus(0.00256 * r, mean_ecliptic_obliquity(t));
}
/**
* Calculates the Sun's declination
*
- * @param tm The time in Julian Centuries
- * @return The Sun's declination, in radian
+ * @param t The time in Julian Centuries
+ * @return The Sun's declination, in radian
*/
-static inline double
-solar_declination(double tm)
+static double
+solar_declination(double t)
{
- double rc = sin(corrected_mean_ecliptic_obliquity(tm));
- return asin(rc * sin(sun_apparent_longitude(tm)));
+ double r = sin(corrected_mean_ecliptic_obliquity(t));
+ return asin(r * sin(sun_apparent_longitude(t)));
}
/**
* Calculates the equation of time, the discrepancy
* between apparent and mean solar time
*
- * @param tm The time in Julian Centuries
- * @return The equation of time, in degrees
+ * @param t The time in Julian Centuries
+ * @return The equation of time, in minutes of time
*/
-static inline double
-equation_of_time(double tm)
+static double
+equation_of_time(double t)
{
- double l = sun_geometric_mean_longitude(tm);
- double e = earth_orbit_eccentricity(tm);
- double m = sun_geometric_mean_anomaly(tm);
- double y = pow(tan(corrected_mean_ecliptic_obliquity(tm) / 2.0), 2.0);
- double rc = y * sin(2.0 * l);
- rc += (4.0 * y * cos(2.0 * l) - 2.0) * e * sin(m);
- rc -= pow(0.5 * y, 2.0) * sin(4.0 * l);
- rc -= pow(1.25 * e, 2.0) * sin(2.0 * m);
- return 4.0 * degrees(rc);
+ double l = sun_geometric_mean_longitude(t);
+ double e = earth_orbit_eccentricity(t);
+ double m = sun_geometric_mean_anomaly(t);
+ double y = tan(corrected_mean_ecliptic_obliquity(t) / 2.0;
+ double r, c, s;
+ y *= y;
+ s = y * sin(2.0 * l);
+ c = y * cos(2.0 * l);
+ r = fma(fma(4.0, c, -2.0), e * sin(m), s);
+ r = fma(-0.5 * y*y, sin(4.0 * l), r);
+ r = fma(-1.25 * e*e, sin(2.0 * m), r);
+ return 4.0 * degrees(r);
}
/**
* Calculates the Sun's elevation as apparent
* from a geographical position
*
- * @param tm The time in Julian Centuries
+ * @param tc The time in Julian Centuries
+ * @param td The time in Julian Days
* @param latitude The latitude in degrees northwards from
* the equator, negative for southwards
* @param longitude The longitude in degrees eastwards from
@@ -250,14 +273,14 @@ equation_of_time(double tm)
* @return The Sun's apparent elevation at the specified time as seen
* from the specified position, measured in radians
*/
-static inline double
-solar_elevation_from_time(double tm, double latitude, double longitude)
+static double
+solar_elevation_from_time(double tc, double td, double latitude, double longitude)
{
- double rc = julian_centuries_to_julian_day(tm);
- rc = (rc - round(rc) - 0.5) * 1440;
- rc = 720.0 - rc - equation_of_time(tm);
- rc = radians(rc / 4.0 - longitude);
- return elevation_from_hour_angle(latitude, solar_declination(tm), rc);
+ double r;
+ td = td - round(td);
+ r = fma(1440, td - 1, -equation_of_time(tc));
+ r = radians(fma(0.25, r, -longitude));
+ return elevation_from_hour_angle(latitude, solar_declination(tc), r);
}
/**
@@ -277,33 +300,18 @@ solar_elevation_from_time(double tm, double latitude, double longitude)
double
libred_solar_elevation(double latitude, double longitude, double *elevation)
{
- double tm;
- if (julian_centuries(&tm))
+ double tc, td;
+ if (julian_time(&tc, &td))
return -1;
- *elevation = degrees(solar_elevation_from_time(tm, latitude, longitude));
+ *elevation = degrees(solar_elevation_from_time(tc, td, latitude, longitude));
return 0;
}
/**
- * Exit if time the is before year 0 in J2000
- *
- * @return 0 on success, -1 on error
+ * This function is obsolete
*/
int
libred_check_timetravel(void)
{
-#if !defined(TIMETRAVELLER)
- struct timespec now;
- if (clock_gettime(CLOCK_REALTIME, &now))
- return -1;
- if (now.tv_sec < (time_t)946728000L) {
- fprintf(stderr,
- "We have detected that you are a time-traveller"
- "(or your clock is not configured correctly.)"
- "Please recompile libred with -DTIMETRAVELLER"
- "(or correct your clock.)");
- exit(1);
- }
-#endif
return 0;
}