From 1d4b7059aa969875dbd4904c8a6980afdb39f647 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Fri, 10 Jan 2020 18:10:35 +0100 Subject: Implement model for local sun direction `local_sunrise.py` renders the sky for a given time and place on earth. --- local_sunrise.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ sun.py | 32 +++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 local_sunrise.py create mode 100644 sun.py diff --git a/local_sunrise.py b/local_sunrise.py new file mode 100644 index 0000000..58ec73d --- /dev/null +++ b/local_sunrise.py @@ -0,0 +1,67 @@ +import numpy as np +import matplotlib.pyplot as plt +from string import Template + +import pyopencl as cl +from pyopencl.cltypes import make_double3 + +mf = cl.mem_flags + +from planets import earth + +from sun import sun_direction +from datetime import datetime + +config = { + 'size_x': 1920//4, + 'size_y': 1080//4, + + 'ray_samples' : 16, + 'light_samples': 8, + + 'exposure': 2.0, + 'zoom': 1.0, + + 'eye_pos': np.array([0, 0, 1.0001]), + 'eye_dir': np.array([0, 1, 0]), + + 'date': (2020, 1, 10), + 'latitude': 49.01, + 'longitude': 8.4 +} + +time_range = (5, 20, 1) + +cl_platform = cl.get_platforms()[0] +cl_context = cl.Context(properties=[(cl.context_properties.PLATFORM, cl_platform)]) +cl_queue = cl.CommandQueue(cl_context) + +cl_picture = cl.Buffer(cl_context, mf.WRITE_ONLY, size=config['size_x']*config['size_y']*3*np.float64(0).nbytes) +program = None + +with open('raymarch.cl') as f: + program = cl.Program(cl_context, Template(f.read()).substitute( + {**config, **earth} + )).build() + +for time in np.arange(*time_range): + pit = datetime(*config['date'], int(np.floor(time)), int((time-np.floor(time))*60), 0) + sun_dir = sun_direction(config['latitude'], config['longitude'], pit, 1.0) + + sun = make_double3( + np.sin(sun_dir[1])*np.cos(sun_dir[0]), + np.cos(sun_dir[1])*np.cos(sun_dir[0]), + np.sin(sun_dir[0]) + ) + print(sun_dir) + + program.render( + cl_queue, (config['size_x'], config['size_y']), None, cl_picture, + make_double3(*(config['eye_pos'] * earth['earth_radius'])), + make_double3(*(config['eye_dir'] * earth['earth_radius'])), + sun) + + np_picture = np.ndarray(shape=(config['size_y'], config['size_x'], 3), dtype=np.float64) + cl.enqueue_copy(cl_queue, np_picture, cl_picture).wait(); + + plt.imsave("sky_%05.1f.png" % time, np_picture, origin='lower') diff --git a/sun.py b/sun.py new file mode 100644 index 0000000..87bc8a9 --- /dev/null +++ b/sun.py @@ -0,0 +1,32 @@ +import numpy as np +from datetime import datetime + +## Sun direction depending on time and place +# As described in Appendix D of "ME 4131 Thermal Environmental Engineering Laboratory Manual" + +def sun_declination(time): + day_of_year = time.timetuple().tm_yday + return 23.45 * np.sin(np.radians((360/365)*(284+day_of_year))) + +def equation_of_time(time): + day_of_year = time.timetuple().tm_yday + b = np.radians(360*(day_of_year-81)/364) + return 0.165*np.sin(2*b) - 0.126*np.cos(b) - 0.025*np.sin(b) + +def sun_direction(lat, lon, time, time_diff, summertime_shift = 0): + lon_std = time_diff * 15 + clock_time = time.hour + time.minute/60 + local_solar_time = clock_time + (1/15)*(lon - lon_std) + equation_of_time(time) - summertime_shift + hour_angle = 15*(local_solar_time - 12) + + l = np.radians(lat) + h = np.radians(hour_angle) + d = np.radians(sun_declination(time)) + + altitude = np.arcsin(np.cos(l) * np.cos(h) * np.cos(d) + np.sin(l) * np.sin(d)) + azimuth = np.arccos((np.cos(d) * np.sin(l) * np.cos(h) - np.sin(d) * np.cos(l)) / np.cos(altitude)) + + if (time.timetuple().tm_hour <= 12): + return (altitude, -azimuth) + else: + return (altitude, azimuth) -- cgit v1.2.3