From 8ea13bb4d6e09c19a935647bab2d8bff6ccdf45d Mon Sep 17 00:00:00 2001 From: EstherLerouzic Date: Wed, 19 Oct 2022 17:30:57 +0200 Subject: [PATCH] refactor cli to use a common design function Signed-off-by: EstherLerouzic Change-Id: I029d8c7fc29b1e86e1e3b2b64933bae5da134226 --- gnpy/core/network.py | 11 +++++++- gnpy/tools/cli_examples.py | 57 +++++++++++++++++++++++--------------- 2 files changed, 45 insertions(+), 23 deletions(-) diff --git a/gnpy/core/network.py b/gnpy/core/network.py index cd391498..660789de 100644 --- a/gnpy/core/network.py +++ b/gnpy/core/network.py @@ -14,7 +14,7 @@ from logging import getLogger from gnpy.core import elements from gnpy.core.exceptions import ConfigurationError, NetworkTopologyError -from gnpy.core.utils import round2float, convert_length, psd2powerdbm +from gnpy.core.utils import round2float, convert_length, psd2powerdbm, lin2db, watt2dbm from gnpy.core.info import ReferenceCarrier from gnpy.tools.json_io import Amp @@ -678,3 +678,12 @@ def build_network(network, equipment, pref_ch_db, pref_total_db, set_connector_l set_roadm_input_powers(network, roadm, equipment, pref_ch_db) for fiber in [f for f in network.nodes() if isinstance(f, (elements.Fiber, elements.RamanFiber))]: set_fiber_input_power(network, fiber, equipment, pref_ch_db) + + +def design_network(reference_channel, network, equipment, verbose=True): + """Network is designed according to reference channel. Verbose indicate if the function should + print all warnings or not + """ + pref_ch_db = watt2dbm(reference_channel.power) # reference channel power + pref_total_db = pref_ch_db + lin2db(reference_channel.nb_channel) # reference total power + build_network(network, equipment, pref_ch_db, pref_total_db, verbose) diff --git a/gnpy/tools/cli_examples.py b/gnpy/tools/cli_examples.py index 2f244ff2..23961d1d 100644 --- a/gnpy/tools/cli_examples.py +++ b/gnpy/tools/cli_examples.py @@ -19,7 +19,7 @@ import gnpy.core.ansi_escapes as ansi_escapes from gnpy.core.elements import Transceiver, Fiber, RamanFiber from gnpy.core.equipment import trx_mode_params import gnpy.core.exceptions as exceptions -from gnpy.core.network import build_network, add_missing_elements_in_network +from gnpy.core.network import add_missing_elements_in_network, design_network from gnpy.core.parameters import SimParams from gnpy.core.utils import db2lin, lin2db, automatic_nch from gnpy.topology.request import (ResultElement, jsontocsv, compute_path_dsjctn, requests_aggregation, @@ -204,6 +204,7 @@ def transmission_main_example(args=None): print('User input for spectrum used for propagation instead of SI') params['nb_channel'] = nb_channels req = PathRequest(**params) + pref_ch_db = lin2db(req.power * 1e3) # reference channel power / span (SL=20dB) req.initial_spectrum = initial_spectrum print(f'There are {nb_channels} channels propagating') power_mode = equipment['Span']['default'].power_mode @@ -219,25 +220,8 @@ def transmission_main_example(args=None): print(f'{ansi_escapes.red}Configuration error:{ansi_escapes.reset} {e}') sys.exit(1) - # Keep the reference channel for design: the one from SI, with full load same channels - pref_ch_db = lin2db(req.power * 1e3) # reference channel power / span (SL=20dB) - pref_total_db = pref_ch_db + lin2db(req.nb_channel) # reference total power / span (SL=20dB) - try: - build_network(network, equipment, pref_ch_db, pref_total_db) - except exceptions.NetworkTopologyError as e: - print(f'{ansi_escapes.red}Invalid network definition:{ansi_escapes.reset} {e}') - sys.exit(1) - except exceptions.ConfigurationError as e: - print(f'{ansi_escapes.red}Configuration error:{ansi_escapes.reset} {e}') - sys.exit(1) - path = compute_constrained_path(network, req) - spans = [s.params.length for s in path if isinstance(s, RamanFiber) or isinstance(s, Fiber)] - print(f'\nThere are {len(spans)} fiber spans over {sum(spans)/1000:.0f} km between {source.uid} ' - f'and {destination.uid}') - print(f'\nNow propagating between {source.uid} and {destination.uid}:') - power_range = [0] if power_mode: # power cannot be changed in gain mode @@ -247,6 +231,20 @@ def transmission_main_example(args=None): power_range = list(linspace(p_start, p_stop, p_num)) except TypeError: print('invalid power range definition in eqpt_config, should be power_range_db: [lower, upper, step]') + # initial network is designed using req.power. that is that any missing information (amp gain or delta_p) is filled + # using this req.power, previous to any sweep requested later on. + try: + design_network(req, network, equipment, verbose=True) + except exceptions.NetworkTopologyError as e: + print(f'{ansi_escapes.red}Invalid network definition:{ansi_escapes.reset} {e}') + sys.exit(1) + except exceptions.ConfigurationError as e: + print(f'{ansi_escapes.red}Configuration error:{ansi_escapes.reset} {e}') + sys.exit(1) + + print(f'\nThere are {len(spans)} fiber spans over {sum(spans)/1000:.0f} km between {source.uid} ' + f'and {destination.uid}') + print(f'\nNow propagating between {source.uid} and {destination.uid}:') for dp_db in power_range: req.power = db2lin(pref_ch_db + dp_db) * 1e-3 # if initial spectrum did not contain any power, now we need to use this one. @@ -350,11 +348,26 @@ def path_requests_run(args=None): print(f'{ansi_escapes.red}Configuration error:{ansi_escapes.reset} {e}') sys.exit(1) - p_db = equipment['SI']['default'].power_dbm - p_total_db = p_db + lin2db(automatic_nch(equipment['SI']['default'].f_min, - equipment['SI']['default'].f_max, equipment['SI']['default'].spacing)) + params = { + 'request_id': 'reference', + 'trx_type': '', + 'trx_mode': '', + 'source': None, + 'destination': None, + 'bidir': False, + 'nodes_list': [], + 'loose_list': [], + 'format': '', + 'path_bandwidth': 0, + 'effective_freq_slot': None, + 'nb_channel': automatic_nch(equipment['SI']['default'].f_min, equipment['SI']['default'].f_max, + equipment['SI']['default'].spacing) + } + trx_params = trx_mode_params(equipment) + params.update(trx_params) + reference_channel = PathRequest(**params) try: - build_network(network, equipment, p_db, p_total_db) + design_network(reference_channel, network, equipment, verbose=True) except exceptions.NetworkTopologyError as e: print(f'{ansi_escapes.red}Invalid network definition:{ansi_escapes.reset} {e}') sys.exit(1)