Skip to content

Commit

Permalink
Adds methods to set band start and stop frequencies, & corresponding …
Browse files Browse the repository at this point in the history
…unit tests (#3773)

* Adds methods to set band start and stop frequencies, and corresponding unit tests.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Docstring edits - parameter type for band_node, added examples

* docstring edits, check for valid freq range, and start/stop frequency order.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update pyaedt/modeler/circuits/PrimitivesEmit.py

Co-authored-by: Kathy Pippert <[email protected]>

* Update pyaedt/modeler/circuits/PrimitivesEmit.py

Co-authored-by: Kathy Pippert <[email protected]>

* added more unit tests, docstring suggestions, removed Typeerror check

* Update pyaedt/modeler/circuits/PrimitivesEmit.py

* Update pyaedt/modeler/circuits/PrimitivesEmit.py

* Update pyaedt/modeler/circuits/PrimitivesEmit.py

* Update pyaedt/modeler/circuits/PrimitivesEmit.py

* Update pyaedt/modeler/circuits/PrimitivesEmit.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Maxime Rey <[email protected]>
Co-authored-by: Kathy Pippert <[email protected]>
Co-authored-by: Samuel Lopez <[email protected]>
  • Loading branch information
5 people authored Nov 24, 2023
1 parent 2a62fa5 commit 2ba12b5
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 0 deletions.
39 changes: 39 additions & 0 deletions _unittest_solvers/test_26_emit.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,45 @@ def test_radio_component(self, add_app):
assert start_freq == 0.1
start_freq = radio.band_start_frequency(band, "THz")
assert start_freq == 0.0001
# test band.set_band_start_frequency
start_freq = 10
units = 'MHz'
radio.set_band_start_frequency(band, start_freq, units=units)
assert radio.band_start_frequency(band, units=units) == start_freq
start_freq = 20000000
radio.set_band_start_frequency(band, start_freq)
assert radio.band_start_frequency(band, units='Hz') == start_freq
# test band.set_band_stop_frequency
stop_freq = 30
units = 'MHz'
radio.set_band_stop_frequency(band, stop_freq, units=units)
assert radio.band_stop_frequency(band, units=units) == stop_freq
stop_freq = 40000000
radio.set_band_stop_frequency(band, stop_freq)
assert radio.band_stop_frequency(band, units='Hz') == stop_freq
# test corner cases for band start and stop frequencies
start_freq = 10
stop_freq = 9
units = 'Hz'
radio.set_band_start_frequency(band, start_freq, units=units)
radio.set_band_stop_frequency(band, stop_freq, units=units)
assert radio.band_start_frequency(band, units="Hz") == 8
radio.set_band_start_frequency(band, 10, units=units)
assert radio.band_stop_frequency(band, units="Hz") == 11
units = 'wrong'
radio.set_band_stop_frequency(band, 10, units=units)
assert radio.band_stop_frequency(band, units='Hz') == 10
radio.set_band_start_frequency(band, 10, units=units)
assert radio.band_start_frequency(band, units='Hz') == 10
with pytest.raises(ValueError) as e:
start_freq = 101
units = 'GHz'
radio.set_band_start_frequency(band, start_freq, units=units)
assert "Frequency should be within 1Hz to 100 GHz." in str(e)
stop_freq = 102
radio.set_band_stop_frequency(band, stop_freq, units=units)
assert "Frequency should be within 1Hz to 100 GHz." in str(e)

# test power unit conversions
band_power = radio.band_tx_power(band)
assert band_power == 40.0
Expand Down
138 changes: 138 additions & 0 deletions pyaedt/modeler/circuits/PrimitivesEmit.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,144 @@ def band_stop_frequency(self, band_node, units=""):
units = self.units["Frequency"]
return consts.unit_converter(float(band_node.props["StopFrequency"]), "Freq", "Hz", units)

def set_band_start_frequency(self, band_node, band_start_freq, units=""):
"""Set start frequency of the band.
Parameters
----------
band_node : pyaedt.modeler.circuits.PrimitivesEmit.EmitComponentPropNode object
Instance of the band node
band_start_freq : float
Start frequency of the band.
units : str, optional
Units of the start frequency. If no units are specified or the units
specified are invalid, then `"Hz"`` is assumed.
Returns
------
None
Examples
--------
>>> from pyaedt import Emit
>>> aedtapp = Emit(new_desktop_session=False)
>>> radio = aedtapp.modeler.components.create_component("New Radio")
>>> band = radio.bands()[0]
>>> start_freq = 10
>>> units = 'MHz'
>>> radio.set_band_start_frequency(band, start_freq, units=units)
"""

# if "Band" not in band_node.props["Type"]:
# raise TypeError("{} must be a band.".format(band_node.node_name))

if not units or units not in emit_consts.EMIT_VALID_UNITS["Frequency"]:
units = "Hz"

# convert to Hz
freq_float_in_Hz = consts.unit_converter(band_start_freq, "Freq", units, "Hz")
freq_string = "{}".format(freq_float_in_Hz)
if not (1 <= freq_float_in_Hz <= 100000000000): # pragma: no cover
raise ValueError("Frequency should be within 1Hz to 100 GHz.")
if float(band_node.props["StopFrequency"]) < freq_float_in_Hz: # pragma: no cover
stop_freq = freq_float_in_Hz + 1
band_node._set_prop_value({"StopFrequency": str(stop_freq)})
else:
prop_list = {"StartFrequency": freq_string}
band_node._set_prop_value(prop_list)

def set_band_stop_frequency(self, band_node, band_stop_freq, units=""):
"""Set stop frequency of the band.
Parameters
----------
band_node : pyaedt.modeler.circuits.PrimitivesEmit.EmitComponentPropNode object
Instance of the band node
band_stop_freq : float
Stop frequency of the band.
units : str, optional
Units of the stop frequency. If no units are specified or the units
specified are invalid, then `"Hz"`` is assumed.
Returns
------
None
Examples
--------
>>> from pyaedt import Emit
>>> aedtapp = Emit(new_desktop_session=False)
>>> radio = aedtapp.modeler.components.create_component("New Radio")
>>> band = radio.bands()[0]
>>> stop_freq = 10
>>> units = 'MHz'
>>> radio.set_band_stop_frequency(band, stop_freq, units=units)
"""
# if "Band" not in band_node.props["Type"]:
# raise TypeError("{} must be a band.".format(band_node.node_name))
if not units or units not in emit_consts.EMIT_VALID_UNITS["Frequency"]:
units = "Hz"
# convert to Hz
freq_float_in_Hz = consts.unit_converter(band_stop_freq, "Freq", units, "Hz")
if not (1 <= freq_float_in_Hz <= 100000000000): # pragma: no cover
raise ValueError("Frequency should be within 1Hz to 100 GHz.")
if float(band_node.props["StartFrequency"]) > freq_float_in_Hz: # pragma: no cover
if freq_float_in_Hz > 1:
band_node._set_prop_value({"StartFrequency": str(freq_float_in_Hz - 1)})
else: # pragma: no cover
raise ValueError("Band stop frequency is less than start frequency.")
freq_string = "{}".format(freq_float_in_Hz)
prop_list = {"StopFrequency": freq_string}
band_node._set_prop_value(prop_list)

# def duplicate_band(self, band_node_to_duplicate):
# """
# [Incomplete 10/19/2023]
# Parameters
# ----------
# band_node_to_duplicate
#
# Returns
# -------
#
# """
# # append number to the name of the band to duplicate.
# print('Duplicating...')
#
#
# # return band node
# def convert_channels_to_multi_bands(self, band_node):
# """
# [Incomplete 10/19/2023]
# Parameters
# ----------
# band_node
#
# Returns
# -------
#
# """
# # get the channels. Say returns 10 channels in the band_node
# # Name = r.bands()[0].children[0].props['Name']
# # band_node.props['Name']
# # Start = r.bands()[0].props['StartFrequency']
# band_start_frequency = float(band_node.props['StartFrequency'])
# # Stop = r.bands()[0].props['StopFrequency']
# band_stop_frequency = float(band_node.props['StopFrequency'])
# # Spacing = r.bands()[0].props['ChannelSpacing']
# channel_spacing = float(band_node.props['ChannelSpacing'])
# # for each channel
# # 1) create a band (duplicate original one)
# # 2) set band start and stop frequencies
# for channel in list(range(int(band_start_frequency), int(band_stop_frequency), int(channel_spacing))):
# baby_band_start = channel
# baby_band_stop = channel+channel_spacing
# baby_band_node = self.duplicate_band(band_node) # return band name or some handle to it
# self.set_band_start_frequency(baby_band_node, baby_band_start)
# self.set_band_stop_frequency(baby_band_node, baby_band_stop)
# # set start and stop freq for that band name
# # to be

def band_channel_bandwidth(self, band_node, units=""):
"""Get the channel bandwidth of the band node.
Expand Down

0 comments on commit 2ba12b5

Please sign in to comment.