Skip to content

Commit

Permalink
Explicit flags to manage enabling / disabling optional configuration …
Browse files Browse the repository at this point in the history
…segments
  • Loading branch information
Areustle committed Mar 7, 2024
1 parent 591535c commit 37498da
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 43 deletions.
5 changes: 4 additions & 1 deletion sample_input_file.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ moon_alt_cut = "0.0 deg"
moon_min_phase_angle_cut = "150.0 deg"

[detector.optical]
enable = true
telescope_effective_area = "2.5 m2"
quantum_efficiency = 0.2
photo_electron_threshold = 10

[detector.radio]
enable = true
low_frequency = "30.0 MHz"
high_frequency = "300.0 MHz"
snr_threshold = 5.0
Expand All @@ -35,6 +37,7 @@ angle_from_limb = "7.0 deg"
cherenkov_light_engine = "Default"

[simulation.ionosphere]
enable = true
total_electron_content = 10.0
total_electron_error = 0.1

Expand All @@ -50,7 +53,7 @@ log_nu_energy = 8.0
[simulation.cloud_model]
id = "no_cloud"

[simulation.too]
[simulation.target]
source_RA = "0.0 deg"
source_DEC = "0.0 deg"
source_date = "2022-06-02T01:00:00"
Expand Down
13 changes: 7 additions & 6 deletions src/nuspacesim/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,11 @@ def mc_logv(mcint, mcintgeo, numEvPass, mcunc, method):
eas = EAS(config)
eas_radio = EASRadio(config)

if config.simulation.mode == "ToO":
geom = RegionGeomToO(config)
else:
geom = RegionGeom(config)
geom = (
RegionGeomToO(config)
if config.simulation.mode == "Target"
else RegionGeom(config)
)

class StagedWriter:
"""Optionally write intermediate values to file"""
Expand Down Expand Up @@ -212,7 +213,7 @@ def add_meta(self, name: str, value: Any, comment: str):
altDec, lenDec = eas.altDec(beta_tr, tauBeta, tauLorentz, store=sw)

# if config.detector.method == "Optical" or config.detector.method == "Both":
if config.detector.optical:
if config.detector.optical.enable:
logv("Computing [green] EAS Optical Cherenkov light.[/]")

numPEs, costhetaChEff = eas(
Expand Down Expand Up @@ -246,7 +247,7 @@ def add_meta(self, name: str, value: Any, comment: str):

mc_logv(mcint, mcintgeo, passEV, mcunc, "Optical")

if config.detector.radio:
if config.detector.radio.enable:
logv("Computing [green] EAS Radio signal.[/]")

eFields = eas_radio(
Expand Down
11 changes: 7 additions & 4 deletions src/nuspacesim/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def serialize_rad(self, x: float) -> str:

class Optical(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True)
enable: bool = True
telescope_effective_area: float = 2.5 # Quantity(2.5, u.m**2)
""" Effective area of the detector telescope (sq.meters). """
quantum_efficiency: float = 0.2
Expand All @@ -150,6 +151,7 @@ def serialize_aream2(self, x: float) -> str:

class Radio(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True)
enable: bool = True
low_frequency: float = Quantity(30.0, u.MHz).value
""" Low end for radio band in MHz: Default = 30 """
high_frequency: float = Quantity(300.0, u.MHz).value
Expand Down Expand Up @@ -188,8 +190,8 @@ def validate_high_frequency(self):
name: str = "Default Name"
initial_position: InitialPos = InitialPos()
"""Initial conditions for detector"""
sun_moon: SunMoon = SunMoon()
"""[ToO only] Detector sensitivity to effects of the sun and moon"""
sun_moon: Optional[SunMoon] = SunMoon()
"""[Target only] Detector sensitivity to effects of the sun and moon"""
optical: Optional[Optical] = Optical()
"""Characteristics of the optical detector"""
radio: Optional[Radio] = Radio()
Expand All @@ -207,6 +209,7 @@ class Simulation(BaseModel):
################ Radio Ionosphere classes ################

class Ionosphere(BaseModel):
enable: bool = True
total_electron_content: float = 10.0
"""Total Electron Content for ionospheric propagation. """
total_electron_error: float = 0.1
Expand Down Expand Up @@ -303,7 +306,7 @@ def serialize_rad(self, x: float) -> str:

################################################################################

mode: Literal["Diffuse", "ToO"] = "Diffuse"
mode: Literal["Diffuse", "Target"] = "Diffuse"
""" The Simulation Mode """
thrown_events: int = 1000
""" Number of thrown event trajectories. """
Expand All @@ -324,7 +327,7 @@ def serialize_rad(self, x: float) -> str:
cloud_model: Union[NoCloud, MonoCloud, PressureMapCloud] = Field(
default=NoCloud(), discriminator="id"
)
too: Optional[TargetOfOpportunity] = TargetOfOpportunity()
target: Optional[TargetOfOpportunity] = TargetOfOpportunity()

@field_validator(
"max_cherenkov_angle", "max_azimuth_angle", "angle_from_limb", mode="before"
Expand Down
4 changes: 2 additions & 2 deletions src/nuspacesim/simulation/geometry/region_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ def __init__(self, config):
self.detLat = config.detector.initial_position.latitude
self.detLong = config.detector.initial_position.longitude

self.sourceOBSTime = self.config.simulation.too.source_obst
self.sourceOBSTime = self.config.simulation.target.source_obst
self.too_source = ToOEvent(self.config)

self.alphaHorizon = 0.5 * np.pi - np.arccos(self.earth_radius / self.core_alt)
Expand All @@ -437,7 +437,7 @@ def __call__(self, numtrajs, *args, **kwargs):
return self.beta_rad(), self.thetas(), self.pathLens(), self.val_times()

def throw(self, times=None) -> None:
"""Throw N events with 1 * u random numbers for the ToO detection mode"""
"""Throw N events with 1 * u random numbers for the Target detection mode"""

# Calculate the local nadir angle of the source
self.times = self.generate_times(times)
Expand Down
12 changes: 6 additions & 6 deletions src/nuspacesim/simulation/geometry/too.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ def __init__(self, config):
self.detlat = self.config.detector.initial_position.latitude
self.detlong = self.config.detector.initial_position.longitude
self.detalt = self.config.detector.initial_position.altitude
# ToO definitions
self.sourceRA = self.config.simulation.too.source_RA
self.sourceDEC = self.config.simulation.too.source_DEC
self.sourceDATE = self.config.simulation.too.source_date
self.sourceDateFormat = self.config.simulation.too.source_date_format
self.sourceOBSTime = self.config.simulation.too.source_obst
# Target(ToO) definitions
self.sourceRA = self.config.simulation.target.source_RA
self.sourceDEC = self.config.simulation.target.source_DEC
self.sourceDATE = self.config.simulation.target.source_date
self.sourceDateFormat = self.config.simulation.target.source_date_format
self.sourceOBSTime = self.config.simulation.target.source_obst

self.eventtime = astropy.time.Time(
self.sourceDATE, format=self.sourceDateFormat, scale="utc"
Expand Down
20 changes: 16 additions & 4 deletions test/core/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def test_detector_optical_default():
assert a.photo_electron_threshold == 10

assert a.model_dump() == {
"enable": True,
"telescope_effective_area": "2.5 m2",
"quantum_efficiency": 0.2,
"photo_electron_threshold": 10.0,
Expand All @@ -107,6 +108,7 @@ def test_detector_optical_units():
assert a.photo_electron_threshold == 100

assert a.model_dump() == {
"enable": True,
"telescope_effective_area": "16.0 m2",
"quantum_efficiency": 0.5,
"photo_electron_threshold": 100.0,
Expand All @@ -118,6 +120,7 @@ def test_detector_optical_units():
assert b.photo_electron_threshold == 10

assert b.model_dump() == {
"enable": True,
"telescope_effective_area": "2.5 m2",
"quantum_efficiency": 0.2,
"photo_electron_threshold": 10.0,
Expand All @@ -138,6 +141,7 @@ def test_detector_radio_default():
assert radio.gain == 1.8

assert radio.model_dump() == {
"enable": True,
"low_frequency": "30.0 MHz",
"high_frequency": "300.0 MHz",
"snr_threshold": 5.0,
Expand All @@ -161,6 +165,7 @@ def test_detector_radio_units():
assert radio.gain == 2.5

assert radio.model_dump() == {
"enable": True,
"low_frequency": "40.0 MHz",
"high_frequency": "200.0 MHz",
"snr_threshold": 7.0,
Expand All @@ -176,6 +181,7 @@ def test_detector_radio_units():
assert radio_with_units.gain == 3.0

assert radio_with_units.model_dump() == {
"enable": True,
"low_frequency": "0.001 MHz",
"high_frequency": "1000.0 MHz",
"snr_threshold": 5.0,
Expand Down Expand Up @@ -258,11 +264,13 @@ def test_detector_serialization():
"sun_moon_cuts": True,
},
"optical": {
"enable": True,
"telescope_effective_area": "2.5 m2",
"quantum_efficiency": 0.2,
"photo_electron_threshold": 10.0,
},
"radio": {
"enable": True,
"low_frequency": "30.0 MHz",
"high_frequency": "300.0 MHz",
"snr_threshold": 5.0,
Expand Down Expand Up @@ -299,11 +307,15 @@ def test_default_simulation():
"max_azimuth_angle": "360.0 deg",
"angle_from_limb": "7.0 deg",
"cherenkov_light_engine": "Default",
"ionosphere": {"total_electron_content": 10.0, "total_electron_error": 0.1},
"ionosphere": {
"enable": True,
"total_electron_content": 10.0,
"total_electron_error": 0.1,
},
"tau_shower": {"id": "nupyprop", "etau_frac": 0.5, "table_version": "3"},
"spectrum": {"id": "monospectrum", "log_nu_energy": 8.0},
"cloud_model": {"id": "no_cloud"},
"too": {
"target": {
"source_DEC": "0.0 deg",
"source_RA": "0.0 deg",
"source_date": "2022-06-02T01:00:00",
Expand All @@ -315,7 +327,7 @@ def test_default_simulation():

def test_custom_simulation():
a = Simulation(
mode="ToO",
mode="Target",
thrown_events=500,
max_cherenkov_angle=3.5 * u.deg,
max_azimuth_angle=270.0 * u.deg,
Expand All @@ -326,7 +338,7 @@ def test_custom_simulation():
cloud_model=Simulation.MonoCloud(altitude=20.0),
)

assert a.mode == "ToO"
assert a.mode == "Target"
assert a.thrown_events == 500
assert a.max_cherenkov_angle == np.radians(3.5)
assert a.max_azimuth_angle == np.radians(270.0)
Expand Down
28 changes: 16 additions & 12 deletions test/simulation/geometry/test_too.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
@pytest.fixture
def nss_config_event():
conf = NssConfig()
conf.simulation.too.source_RA = np.radians(22)
conf.simulation.too.source_DEC = np.radians(-45)
conf.simulation.target.source_RA = np.radians(22)
conf.simulation.target.source_DEC = np.radians(-45)
conf.detector.initial_position.altitude = 33.0
conf.detector.initial_position.latitude = np.radians(0.0)
conf.detector.initial_position.longitude = np.radians(10.0)
Expand All @@ -21,12 +21,12 @@ def nss_config_event():

conf.simulation.thrown_events: int = 10000
conf.simulation.max_cherenkov_angle: float = np.radians(3.0)
conf.simulation.mode: str = "ToO"
conf.simulation.too.source_RA: float = 0.0
conf.simulation.too.source_DEC: float = 0.0
conf.simulation.too.source_date: str = "2022-06-02T01:00:00.000"
conf.simulation.too.source_date_format: str = "isot"
conf.simulation.too.source_obst: float = 24 * 60 * 60
conf.simulation.mode: str = "Target"
conf.simulation.target.source_RA: float = 0.0
conf.simulation.target.source_DEC: float = 0.0
conf.simulation.target.source_date: str = "2022-06-02T01:00:00.000"
conf.simulation.target.source_date_format: str = "isot"
conf.simulation.target.source_obst: float = 24 * 60 * 60
return conf


Expand All @@ -44,14 +44,18 @@ def test_too_construction(nss_config_event, too_event):
== nss_config_event.detector.sun_moon.moon_min_phase_angle_cut
)

assert too_event.sourceOBSTime == nss_config_event.simulation.too.source_obst
assert too_event.sourceOBSTime == nss_config_event.simulation.target.source_obst

assert too_event.eventtime.utc.isot == nss_config_event.simulation.too.source_date
assert (
too_event.eventcoords.icrs.ra.deg == nss_config_event.simulation.too.source_RA
too_event.eventtime.utc.isot == nss_config_event.simulation.target.source_date
)
assert (
too_event.eventcoords.icrs.dec.deg == nss_config_event.simulation.too.source_DEC
too_event.eventcoords.icrs.ra.deg
== nss_config_event.simulation.target.source_RA
)
assert (
too_event.eventcoords.icrs.dec.deg
== nss_config_event.simulation.target.source_DEC
)

assert too_event.detcords.lat.rad == pytest.approx(
Expand Down
16 changes: 8 additions & 8 deletions test/simulation/geometry/test_too_region_geom.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
@pytest.fixture
def nss_config_event():
conf = NssConfig()
conf.simulation.too.source_RA = np.radians(100)
conf.simulation.too.source_DEC = 0
conf.simulation.target.source_RA = np.radians(100)
conf.simulation.target.source_DEC = 0
conf.detector.initial_position.altitude = 33.0
conf.detector.initial_position.latitude = 0.0
conf.detector.initial_position.longitude = np.radians(10.0)
Expand All @@ -21,12 +21,12 @@ def nss_config_event():

conf.simulation.thrown_events: int = 10000
conf.simulation.max_cherenkov_angle: float = np.radians(3.0)
conf.simulation.mode: str = "ToO"
conf.simulation.too.source_RA: float = 0.0
conf.simulation.too.source_DEC: float = 0.0
conf.simulation.too.source_date: str = "2022-03-21T00:00:00.000"
conf.simulation.too.source_date_format: str = "isot"
conf.simulation.too.source_obst: float = 24 * 60 * 60
conf.simulation.mode: str = "Target"
conf.simulation.target.source_RA: float = 0.0
conf.simulation.target.source_DEC: float = 0.0
conf.simulation.target.source_date: str = "2022-03-21T00:00:00.000"
conf.simulation.target.source_date_format: str = "isot"
conf.simulation.target.source_obst: float = 24 * 60 * 60
return conf


Expand Down

0 comments on commit 37498da

Please sign in to comment.