Esempio n. 1
0
 def __init__(self, country, code, long_name='', has_F=True):
     if long_name == '':
         long_name = 'Sector Object {0} in Country {1}'.format(
             code, country.Code)
     self.Code = code
     EconomicObject.__init__(self, country, code=code)
     self.CurrencyZone = country.CurrencyZone
     country._AddSector(self)
     # This is calculated by the Model
     self.FullCode = ''
     self.LongName = long_name
     # self.Equations = {}
     self.HasF = has_F
     self.IsTaxable = False
     self.EquationBlock = EquationBlock()
     if has_F:
         # self.AddVariable('F', 'Financial assets', '<TO BE GENERATED>')
         F = Equation('F', 'Financial assets')
         F.AddTerm('LAG_F')
         self.AddVariableFromEquation(F)
         # self.AddVariable('LAG_F', 'Previous period''s financial assets.', 'F(k-1)')
         INC = Equation('INC', 'Income (PreTax)', rhs=[])
         self.AddVariableFromEquation(INC)
         self.AddVariable('LAG_F', 'Previous period'
                          's financial assets.', 'F(k-1)')
Esempio n. 2
0
    def _GenerateMultiSupply(self):
        """
        Generate the supply terms with multiple suppliers.

        :return:
        """
        sup_name = 'SUP_' + self.Code
        dem_name = 'DEM_' + self.Code
        # Set aggregate supply equal to demand
        self.SetEquationRightHandSide(sup_name, rhs=dem_name)
        # Generate individual supply equations
        # These are already supplied for everything other than the residual supply, so
        # we need to build it up.
        # Also, the name of the supply varies, depending on whether we are in te same
        # country/region.
        residual_sector = self.ResidualSupply

        residual_equation = Equation(self.GetSupplierTerm(residual_sector),
                                     'Residual supply', sup_name)
        sector_list = self.OtherSuppliers
        # residual supply = total supply less other supply terms
        for supplier, _ in sector_list:
            term = '-SUP_' + supplier.FullCode
            residual_equation.AddTerm(term)
        # Now that we have an equation for the residual sector, append it to the
        # list of suppliers, so we can process all suppliers in one block of code.
        sector_list.append((residual_sector, residual_equation.RHS()))

        for supplier, eqn in sector_list:
            local_name = 'SUP_' + supplier.FullCode
            self.AddVariable(local_name,
                             'Supply from {0}'.format(supplier.LongName), eqn)
            # Push this local variable into the supplying sector
            # If we are in the same country, use 'SUP_{CODE}'
            # If we are in different countries, use 'SUP_{FULLCODE}'
            supply_name = self.GetSupplierTerm(supplier)
            if supply_name not in supplier.EquationBlock:
                supplier.AddVariable(supply_name,
                                     'Supply to {0}'.format(self.FullCode), '')
            if self.IsSharedCurrencyZone(supplier):
                supplier.AddTermToEquation(supply_name,
                                           self.GetVariableName(local_name))
                supplier.AddCashFlow('+' + supply_name)
            else:
                model = self.GetModel()
                if model.ExternalSector is None:
                    raise LogicError(
                        'Must create ExternalSector if we have cross-currency suppliers'
                    )
                full_local_name = self.GetVariableName(local_name)
                model.ExternalSector._SendMoney(self, full_local_name)
                term = model.ExternalSector._ReceiveMoney(
                    supplier, self, full_local_name)
                supplier.AddTermToEquation(supply_name, term)
                supplier.AddCashFlow(term)
        return
Esempio n. 3
0
 def test_AddTermFail(self):
     eq = Equation('x', 'desc', 'y')
     t2 = Term('y^2', is_blob=True)
     with self.assertRaises(LogicError):
         eq.AddTerm(t2)
Esempio n. 4
0
 def test_Addterm_3(self):
     eq = Equation('x', 'define x')
     eq.AddTerm('y')
     self.assertEqual('y', eq.GetRightHandSide())
     eq.AddTerm('w*z')
     self.assertEqual('y+w*z', eq.GetRightHandSide())