Refactoring: conversion functions instead of gnpy.core.units.UNITS

The TL;DR behind this patch is that it's better to have a utility
conversion function instead of having multiplier LUT and open code which
implements the conversion.

The FiberParams handling looked fishy -- apparently, it was keeping the
multiplier around, but it was unconditionally setting the units to
meters, anyway. Given that the units were not being preserved anyway
(everything got converted to meters), and that the multipler was not
used anywhere, let's refactor the code to just convert to meters using
our new utility function, and remove the unused argument.

Change-Id: Id886d409a4046f980eed569265baefd97db841bd
This commit is contained in:
Jan Kundrát
2020-05-23 13:44:15 +02:00
parent 04e764d024
commit 07eb2dd13a
5 changed files with 32 additions and 22 deletions

View File

@@ -12,5 +12,4 @@
.. automodule:: gnpy.core.science_utils .. automodule:: gnpy.core.science_utils
.. automodule:: gnpy.core.service_sheet .. automodule:: gnpy.core.service_sheet
.. automodule:: gnpy.core.spectrum_assignment .. automodule:: gnpy.core.spectrum_assignment
.. automodule:: gnpy.core.units
.. automodule:: gnpy.core.utils .. automodule:: gnpy.core.utils

View File

@@ -17,8 +17,7 @@ from gnpy.core import ansi_escapes, elements
from gnpy.core.elements import Fiber, Edfa, Transceiver, Roadm, Fused from gnpy.core.elements import Fiber, Edfa, Transceiver, Roadm, Fused
from gnpy.core.equipment import edfa_nf from gnpy.core.equipment import edfa_nf
from gnpy.core.exceptions import ConfigurationError, NetworkTopologyError from gnpy.core.exceptions import ConfigurationError, NetworkTopologyError
from gnpy.core.units import UNITS from gnpy.core.utils import load_json, save_json, round2float, merge_amplifier_restrictions, convert_length
from gnpy.core.utils import (load_json, save_json, round2float, merge_amplifier_restrictions)
from gnpy.tools.convert import convert_file from gnpy.tools.convert import convert_file
from collections import namedtuple from collections import namedtuple
@@ -515,7 +514,7 @@ def add_fiber_padding(network, fibers, padding):
def build_network(network, equipment, pref_ch_db, pref_total_db): def build_network(network, equipment, pref_ch_db, pref_total_db):
default_span_data = equipment['Span']['default'] default_span_data = equipment['Span']['default']
max_length = int(default_span_data.max_length * UNITS[default_span_data.length_units]) max_length = int(convert_length(default_span_data.max_length, default_span_data.length_units))
min_length = max(int(default_span_data.padding / 0.2 * 1e3), 50_000) min_length = max(int(default_span_data.padding / 0.2 * 1e3), 50_000)
bounds = range(min_length, max_length) bounds = range(min_length, max_length)
target_length = max(min_length, 90_000) target_length = max(min_length, 90_000)

View File

@@ -14,8 +14,7 @@ from scipy.constants import c, pi
from numpy import squeeze, log10, exp from numpy import squeeze, log10, exp
from gnpy.core.units import UNITS from gnpy.core.utils import db2lin, convert_length
from gnpy.core.utils import db2lin
from gnpy.core.exceptions import ParametersError from gnpy.core.exceptions import ParametersError
@@ -142,9 +141,7 @@ class SimParams(Parameters):
class FiberParams(Parameters): class FiberParams(Parameters):
def __init__(self, **kwargs): def __init__(self, **kwargs):
try: try:
self._length_units_factor = UNITS[kwargs['length_units']] self._length = convert_length(kwargs['length'], kwargs['length_units'])
self._length = kwargs['length'] * self._length_units_factor # m
self._length_units = 'm'
# fixed attenuator for padding # fixed attenuator for padding
self._att_in = kwargs['att_in'] if 'att_in' in kwargs else 0 self._att_in = kwargs['att_in'] if 'att_in' in kwargs else 0
# if not defined in the network json connector loss in/out # if not defined in the network json connector loss in/out
@@ -190,14 +187,6 @@ class FiberParams(Parameters):
"""length must be in m""" """length must be in m"""
self._length = length self._length = length
@property
def length_units(self):
return self._length_units
@property
def length_units_factor(self):
return self._length_units_factor
@property @property
def att_in(self): def att_in(self):
return self._att_in return self._att_in
@@ -281,4 +270,5 @@ class FiberParams(Parameters):
def asdict(self): def asdict(self):
dictionary = super().asdict() dictionary = super().asdict()
dictionary['loss_coef'] = self.loss_coef * 1e3 dictionary['loss_coef'] = self.loss_coef * 1e3
dictionary['length_units'] = 'm'
return dictionary return dictionary

View File

@@ -1,5 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
UNITS = {'m': 1,
'km': 1E3}

View File

@@ -15,6 +15,7 @@ from csv import writer
import numpy as np import numpy as np
from numpy import pi, cos, sqrt, log10 from numpy import pi, cos, sqrt, log10
from scipy import constants from scipy import constants
from gnpy.core.exceptions import ConfigurationError
def load_json(filename): def load_json(filename):
@@ -282,3 +283,29 @@ def automatic_fmax(f_min, spacing, nch):
196125000000000.0 196125000000000.0
""" """
return f_min + spacing * nch return f_min + spacing * nch
def convert_length(value, units):
"""Convert length into basic SI units
>>> convert_length(1, 'km')
1000.0
>>> convert_length(2.0, 'km')
2000.0
>>> convert_length(123, 'm')
123.0
>>> convert_length(123.0, 'm')
123.0
>>> convert_length(42.1, 'km')
42100.0
>>> convert_length(666, 'yards')
Traceback (most recent call last):
...
gnpy.core.exceptions.ConfigurationError: Cannot convert length in "yards" into meters
"""
if units == 'm':
return value * 1e0
elif units == 'km':
return value * 1e3
else:
raise ConfigurationError(f'Cannot convert length in "{units}" into meters')