def test_GetVariableName_3(self): mod = Model() us = Country(mod, 'US', 'USA') hh = Sector(us, 'HH', 'Household') mod._GenerateFullSectorCodes() with self.assertRaises(KeyError): hh.GetVariableName('Kaboom')
def test_BadUnderBars(self): mod = Model() country = Country(mod, 'US', 'US') sec = Sector(country, 'B__A__D', 'Bad') sec.AddVariable('foo', 'desc', 'x') sec2 = Sector(country, 'OK', 'OK sector') mod._GenerateFullSectorCodes() with self.assertRaises(ValueError): sec.GetVariableName('foo') with self.assertRaises(ValueError): sec2.AddVariable('b__ad', 'desc', 'x') # Force it in.. # Do not go through the constructor, in case the EquationBlock # enforces the naming convention sec2.EquationBlock.Equations['b__ad'] = 'Dummy' with self.assertRaises(ValueError): sec2.GetVariableName('b__ad')
def test_Fixaliases(self): mod = Model() c = Country(mod, 'co', 'co') sec1 = Sector(c, 'sec1', 'sec1') sec1.AddVariable('x', 'eqn x', '') varname = sec1.GetVariableName('x') ID = "{0}".format(sec1.ID) self.assertIn(ID, varname) sec2 = Sector(c, 'sec2', 'sec2') sec2.AddVariable('two_x', 'Test variable', '2 * {0}'.format(varname)) self.assertEqual('2*' + varname, kill_spaces(sec2.EquationBlock['two_x'].RHS())) mod._GenerateFullSectorCodes() mod._FixAliases() self.assertEqual('2*sec1__x', kill_spaces(sec2.EquationBlock['two_x'].RHS()))
def test_fix_aliases_2(self): mod = Model() c = Country(mod, 'co', 'co') sec1 = Sector(c, 'sec1', 'sec1') sec1.AddVariable('x', 'eqn x', '') varname = sec1.GetVariableName('x') # The ID is the key part of the alias self.assertIn('{0}'.format(sec1.ID), varname) sec2 = Sector(c, 'sec2', 'sec2') mod.RegisterCashFlow(sec1, sec2, 'x') mod._GenerateRegisteredCashFlows() self.assertIn('-' + varname, sec1.EquationBlock['F'].RHS()) self.assertIn(varname, sec2.EquationBlock['F'].RHS()) mod._GenerateFullSectorCodes() mod._FixAliases() self.assertIn('-sec1__x', sec1.EquationBlock['F'].RHS()) self.assertEqual('LAG_F+sec1__x', kill_spaces(sec2.EquationBlock['F'].RHS()))
def GetVariableName(self, varname): if varname in ('PRICE', 'NETOZ'): if varname not in self.EquationBlock.GetEquationList(): self.SetUpVariables() return Sector.GetVariableName(self, varname)
limitations under the License. """ # Imports # This next line looks bizarre, but is needed for backwards compatibility with Python 2.7. from __future__ import print_function import sfc_models from sfc_models.models import Model, Country from sfc_models.sector import Sector sfc_models.register_standard_logs(output_dir='output', base_file_name=__file__) mod = Model() can = Country(mod, 'CA', 'Canada') # has_F=False: turns off creation of financial asset variables. sector_yy = Sector(can, 'YY', has_F=False) sector_yy.AddVariable('W', 'Variable W <constant>', '4.0') sector_yy.AddVariable('Y', 'Variable Y - depends on local variable', '2*W') sector_xx = Sector(can, 'XX', has_F=False) variable_name = sector_yy.GetVariableName('Y') # format: inserts variable_name where {0} is eqn = '{0} + 2.0'.format(variable_name) sector_xx.AddVariable('X', 'Variable x; depends on other sector', eqn) # Bind the model; solve eqns = mod.main() print(eqns) mod.main()
limitations under the License. """ # Imports # This next line looks bizarre, but is needed for backwards compatibility with Python 2.7. from __future__ import print_function import sfc_models from sfc_models.models import Model, Country from sfc_models.sector import Sector mod = Model() can = Country(mod, 'CA', 'Canada') # has_F=False: turns off creation of financial asset variables. sector_yy = Sector(can, 'YY', has_F=False) sector_yy.AddVariable('W', 'Variable W <constant>', '4.0') sector_yy.AddVariable('Y', 'Variable Y - depends on local variable', '2*W') # Only the next two lines have changed: put sector_xx into a another Country us = Country(mod, 'US') sector_xx = Sector(us, 'XX', has_F=False) variable_name = sector_yy.GetVariableName('Y') print("Name of variable before binding:", variable_name) # format: inserts variable_name where {0} is eqn = '{0} + 2.0'.format(variable_name) sector_xx.AddVariable('X', 'Variable x; depends on other sector', eqn) # Bind the model; solve eqns = mod.main() print(eqns) print('Variable name after binding:', sector_yy.GetVariableName('Y'))