##############################################################################
# Institute for the Design of Advanced Energy Systems Process Systems
# Engineering Framework (IDAES PSE Framework) Copyright (c) 2018-2019, by the
# software owners: The Regents of the University of California, through
# Lawrence Berkeley National Laboratory,  National Technology & Engineering
# Solutions of Sandia, LLC, Carnegie Mellon University, West Virginia
# University Research Corporation, et al. All rights reserved.
#
# Please see the files COPYRIGHT.txt and LICENSE.txt for full copyright and
# license information, respectively. Both files are also available online
# at the URL "https://github.com/IDAES/idaes-pse".
##############################################################################
"""
Tests for ControlVolumeBlockData.

Author: Andrew Lee
"""
import pytest

from pyomo.environ import (ConcreteModel,
                           TerminationCondition,
                           SolverStatus,
                           value)

from idaes.core import FlowsheetBlock, EnergyBalanceType, MomentumBalanceType
from idaes.unit_models.gibbs_reactor import GibbsReactor
from idaes.property_models.examples.methane_combustion_ideal import (
    MethaneCombustionParameterBlock)
from idaes.core.util.model_statistics import (degrees_of_freedom,
                                              number_variables,
                                              number_total_constraints,
                                              fixed_variables_set,
                                              activated_constraints_set,
                                              number_unused_variables)
from idaes.core.util.testing import (get_default_solver,
                                     PhysicalParameterTestBlock)


# -----------------------------------------------------------------------------
# Get default solver for testing
solver = get_default_solver()


# -----------------------------------------------------------------------------
def test_config():
    m = ConcreteModel()
    m.fs = FlowsheetBlock(default={"dynamic": False})

    m.fs.properties = PhysicalParameterTestBlock()

    m.fs.unit = GibbsReactor(default={"property_package": m.fs.properties})

    # Check unit config arguments
    assert len(m.fs.unit.config) == 8

    assert not m.fs.unit.config.dynamic
    assert not m.fs.unit.config.has_holdup
    assert m.fs.unit.config.energy_balance_type == \
        EnergyBalanceType.useDefault
    assert m.fs.unit.config.momentum_balance_type == \
        MomentumBalanceType.pressureTotal
    assert not m.fs.unit.config.has_heat_transfer
    assert not m.fs.unit.config.has_pressure_change
    assert m.fs.unit.config.property_package is m.fs.properties


# -----------------------------------------------------------------------------
class TestSaponification(object):
    @pytest.fixture(scope="class")
    def methane(self):
        m = ConcreteModel()
        m.fs = FlowsheetBlock(default={"dynamic": False})

        m.fs.properties = MethaneCombustionParameterBlock()

        m.fs.unit = GibbsReactor(default={
                "property_package": m.fs.properties,
                "has_heat_transfer": True,
                "has_pressure_change": True})

        return m

    @pytest.mark.build
    def test_build(self, methane):
        assert hasattr(methane.fs.unit, "inlet")
        assert len(methane.fs.unit.inlet.vars) == 3
        assert hasattr(methane.fs.unit.inlet, "flow_mol_comp")
        assert hasattr(methane.fs.unit.inlet, "temperature")
        assert hasattr(methane.fs.unit.inlet, "pressure")

        assert hasattr(methane.fs.unit, "outlet")
        assert len(methane.fs.unit.outlet.vars) == 3
        assert hasattr(methane.fs.unit.outlet, "flow_mol_comp")
        assert hasattr(methane.fs.unit.outlet, "temperature")
        assert hasattr(methane.fs.unit.outlet, "pressure")

        assert hasattr(methane.fs.unit, "gibbs_minimization")
        assert hasattr(methane.fs.unit, "heat_duty")
        assert hasattr(methane.fs.unit, "deltaP")

        assert number_variables(methane) == 78
        assert number_total_constraints(methane) == 66
        assert number_unused_variables(methane) == 0

    def test_dof(self, methane):
        methane.fs.unit.inlet.flow_mol_comp[0, "H2"].fix(10.0)
        methane.fs.unit.inlet.flow_mol_comp[0, "N2"].fix(150.0)
        methane.fs.unit.inlet.flow_mol_comp[0, "O2"].fix(40.0)
        methane.fs.unit.inlet.flow_mol_comp[0, "CO2"].fix(1e-5)
        methane.fs.unit.inlet.flow_mol_comp[0, "CH4"].fix(30.0)
        methane.fs.unit.inlet.flow_mol_comp[0, "CO"].fix(1e-5)
        methane.fs.unit.inlet.flow_mol_comp[0, "H2O"].fix(1e-5)
        methane.fs.unit.inlet.flow_mol_comp[0, "NH3"].fix(1e-5)
        methane.fs.unit.inlet.temperature[0].fix(1500.0)
        methane.fs.unit.inlet.pressure[0].fix(101325.0)

        methane.fs.unit.outlet.temperature[0].fix(2844.38)
        methane.fs.unit.deltaP.fix(0)

        assert degrees_of_freedom(methane) == 0

    @pytest.mark.initialize
    @pytest.mark.solver
    @pytest.mark.skipif(solver is None, reason="Solver not available")
    def test_initialize_temperature(self, methane):
        orig_fixed_vars = fixed_variables_set(methane)
        orig_act_consts = activated_constraints_set(methane)

        methane.fs.unit.initialize(
                          optarg={'tol': 1e-6},
                          state_args={'temperature': 2844.38,
                                      'pressure': 101325.0,
                                      'flow_mol_comp': {'CH4': 1e-5,
                                                        'CO': 23.0,
                                                        'CO2': 7.05,
                                                        'H2': 29.0,
                                                        'H2O': 41.0,
                                                        'N2': 150.0,
                                                        'NH3': 1e-5,
                                                        'O2': 1.0}})

        assert degrees_of_freedom(methane) == 0

        fin_fixed_vars = fixed_variables_set(methane)
        fin_act_consts = activated_constraints_set(methane)

        assert len(fin_act_consts) == len(orig_act_consts)
        assert len(fin_fixed_vars) == len(orig_fixed_vars)

        for c in fin_act_consts:
            assert c in orig_act_consts
        for v in fin_fixed_vars:
            assert v in orig_fixed_vars

    @pytest.mark.solver
    @pytest.mark.skipif(solver is None, reason="Solver not available")
    def test_solve_temperature(self, methane):
        results = solver.solve(methane)

        # Check for optimal solution
        assert results.solver.termination_condition == \
            TerminationCondition.optimal
        assert results.solver.status == SolverStatus.ok

    @pytest.mark.initialize
    @pytest.mark.solver
    @pytest.mark.skipif(solver is None, reason="Solver not available")
    def test_solution_temperature(self, methane):
        assert (pytest.approx(0.0, abs=1e-2) ==
                value(methane.fs.unit.outlet.flow_mol_comp[0, "CH4"]))
        assert (pytest.approx(22.614, abs=1e-2) ==
                value(methane.fs.unit.outlet.flow_mol_comp[0, "CO"]))
        assert (pytest.approx(7.386, abs=1e-2) ==
                value(methane.fs.unit.outlet.flow_mol_comp[0, "CO2"]))
        assert (pytest.approx(28.806, abs=1e-2) ==
                value(methane.fs.unit.outlet.flow_mol_comp[0, "H2"]))
        assert (pytest.approx(41.194, abs=1e-2) ==
                value(methane.fs.unit.outlet.flow_mol_comp[0, "H2O"]))
        assert (pytest.approx(150.0, abs=1e-2) ==
                value(methane.fs.unit.outlet.flow_mol_comp[0, "N2"]))
        assert (pytest.approx(0.0, abs=1e-2) ==
                value(methane.fs.unit.outlet.flow_mol_comp[0, "NH3"]))
        assert (pytest.approx(0.710, abs=1e-2) ==
                value(methane.fs.unit.outlet.flow_mol_comp[0, "O2"]))
        assert (pytest.approx(161882.3, abs=1e-2) ==
                value(methane.fs.unit.heat_duty[0]))
        assert (pytest.approx(101325.0, abs=1e-2) ==
                value(methane.fs.unit.outlet.pressure[0]))

    @pytest.mark.initialize
    @pytest.mark.solver
    @pytest.mark.skipif(solver is None, reason="Solver not available")
    def test_conservation_temperature(self, methane):
        assert abs(value(
                sum(methane.fs.unit.inlet.flow_mol_comp[0, j]
                    for j in methane.fs.properties.component_list) *
                methane.fs.unit.control_volume.properties_in[0].enth_mol -
                sum(methane.fs.unit.outlet.flow_mol_comp[0, j]
                    for j in methane.fs.properties.component_list) *
                methane.fs.unit.control_volume.properties_out[0].enth_mol +
                methane.fs.unit.heat_duty[0])) <= 1e-6

    @pytest.mark.initialize
    @pytest.mark.solver
    @pytest.mark.skipif(solver is None, reason="Solver not available")
    def test_initialize_duty(self, methane):
        methane.fs.unit.outlet.temperature[0].unfix()
        methane.fs.unit.heat_duty.fix(161882.303661)
        assert degrees_of_freedom(methane) == 0

        orig_fixed_vars = fixed_variables_set(methane)
        orig_act_consts = activated_constraints_set(methane)

        methane.fs.unit.initialize(
                          optarg={'tol': 1e-6},
                          state_args={'temperature': 2844.38,
                                      'pressure': 101325.0,
                                      'flow_mol_comp': {'CH4': 1e-5,
                                                        'CO': 23.0,
                                                        'CO2': 7.05,
                                                        'H2': 29.0,
                                                        'H2O': 41.0,
                                                        'N2': 150.0,
                                                        'NH3': 1e-5,
                                                        'O2': 1.0}})

        assert degrees_of_freedom(methane) == 0

        fin_fixed_vars = fixed_variables_set(methane)
        fin_act_consts = activated_constraints_set(methane)

        assert len(fin_act_consts) == len(orig_act_consts)
        assert len(fin_fixed_vars) == len(orig_fixed_vars)

        for c in fin_act_consts:
            assert c in orig_act_consts
        for v in fin_fixed_vars:
            assert v in orig_fixed_vars

    @pytest.mark.solver
    @pytest.mark.skipif(solver is None, reason="Solver not available")
    def test_solve_heat_duty(self, methane):
        results = solver.solve(methane)

        # Check for optimal solution
        assert results.solver.termination_condition == \
            TerminationCondition.optimal
        assert results.solver.status == SolverStatus.ok

    @pytest.mark.initialize
    @pytest.mark.solver
    @pytest.mark.skipif(solver is None, reason="Solver not available")
    def test_solution_duty(self, methane):
        assert (pytest.approx(0.0, abs=1e-2) ==
                value(methane.fs.unit.outlet.flow_mol_comp[0, "CH4"]))
        assert (pytest.approx(22.614, abs=1e-2) ==
                value(methane.fs.unit.outlet.flow_mol_comp[0, "CO"]))
        assert (pytest.approx(7.386, abs=1e-2) ==
                value(methane.fs.unit.outlet.flow_mol_comp[0, "CO2"]))
        assert (pytest.approx(28.806, abs=1e-2) ==
                value(methane.fs.unit.outlet.flow_mol_comp[0, "H2"]))
        assert (pytest.approx(41.194, abs=1e-2) ==
                value(methane.fs.unit.outlet.flow_mol_comp[0, "H2O"]))
        assert (pytest.approx(150.0, abs=1e-2) ==
                value(methane.fs.unit.outlet.flow_mol_comp[0, "N2"]))
        assert (pytest.approx(0.0, abs=1e-2) ==
                value(methane.fs.unit.outlet.flow_mol_comp[0, "NH3"]))
        assert (pytest.approx(0.710, abs=1e-2) ==
                value(methane.fs.unit.outlet.flow_mol_comp[0, "O2"]))
        assert (pytest.approx(161882.3, abs=1e-2) ==
                value(methane.fs.unit.heat_duty[0]))
        assert (pytest.approx(101325.0, abs=1e-2) ==
                value(methane.fs.unit.outlet.pressure[0]))

    @pytest.mark.initialize
    @pytest.mark.solver
    @pytest.mark.skipif(solver is None, reason="Solver not available")
    def test_conservation_duty(self, methane):
        assert abs(value(
                sum(methane.fs.unit.inlet.flow_mol_comp[0, j]
                    for j in methane.fs.properties.component_list) *
                methane.fs.unit.control_volume.properties_in[0].enth_mol -
                sum(methane.fs.unit.outlet.flow_mol_comp[0, j]
                    for j in methane.fs.properties.component_list) *
                methane.fs.unit.control_volume.properties_out[0].enth_mol +
                methane.fs.unit.heat_duty[0])) <= 1e-6

    @pytest.mark.ui
    def test_report(self, methane):
        methane.fs.unit.report()
