Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anaerobic Digester Re-Scaling #1535

Open
wants to merge 44 commits into
base: main
Choose a base branch
from

Conversation

MarcusHolly
Copy link
Contributor

@MarcusHolly MarcusHolly commented Nov 29, 2024

Summary/Motivation:

Adds the new IDAES scaling routine (scaler objects) to the anaerobic digester unit model. This should be merged after #1519 and #1530 are merged in.

Changes proposed in this PR:

  • Updates the scaling routine of the anaerobic digester
  • Updates testing

Legal Acknowledgement

By contributing to this software project, I agree to the following terms and conditions for my contribution:

  1. I agree my contributions are submitted under the license terms described in the LICENSE.txt file at the top level of this directory.
  2. I represent I am authorized to make the contributions and grant the license. If my employer has rights to intellectual property that includes these contributions, I represent that I have received permission to make contributions and grant the required license on behalf of that employer.

@MarcusHolly MarcusHolly added the Priority:Normal Normal Priority Issue or PR label Nov 29, 2024
@MarcusHolly MarcusHolly self-assigned this Nov 29, 2024
@MarcusHolly MarcusHolly marked this pull request as ready for review December 17, 2024 17:43
Comment on lines +930 to +941
iscale.set_scaling_factor(self.KH_co2, 1e2)
iscale.set_scaling_factor(self.KH_ch4, 1e2)
iscale.set_scaling_factor(self.KH_h2, 1e2)
iscale.set_scaling_factor(self.hydraulic_retention_time, 1e-6)
iscale.set_scaling_factor(self.volume_AD, 1e-2)
iscale.set_scaling_factor(self.volume_vapor, 1e-2)
iscale.set_scaling_factor(self.liquid_phase.rate_reaction_generation, 1e4)
iscale.set_scaling_factor(self.liquid_phase.mass_transfer_term, 1e2)
iscale.set_scaling_factor(self.liquid_phase.rate_reaction_extent, 1e4)
iscale.set_scaling_factor(self.liquid_phase.enthalpy_transfer, 1e0)
iscale.set_scaling_factor(self.liquid_phase.volume, 1e-2)
iscale.set_scaling_factor(self.electricity_consumption, 1e0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these be handled in the scaler class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Andrew mentioned that we should not mix and match the new and old scaling tools. In other words, the scaler class should not make use of iscale. This is the reason why I've moved all of the iscale.set_scaling_factor to the calculate_scaling_factors function (in this PR and in others), which won't be called when exclusively using the new scaling tools.

If your question is why don't I set these default scaling factors in the ADScaler class, it's because I figured these would make more sense as user-defined scaling factors. I know I did do some testing with having default scaling factors for these variables in the new scaler class, but I forget how thoroughly I played around with this. Currently, the only variable scaled by default in the new scaler class is volume.

@@ -56,71 +56,71 @@ def test_solve(self, system_frame):
0.24219, rel=1e-3
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to check the condition number at the flowsheet level between old scaling approach and new?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then, every time another PR impacts scaling at this flowsheet level, we can track whether condition number improves or not as we go.

@MarcusHolly
Copy link
Contributor Author

Noting that the ScalingProfiler cannot currently be applied to the AD unit model since the ADM1 and modified ADM1 reaction packages have potential divisions by zero (see def rule_I_pH_aa(self)).

@adam-a-a
Copy link
Contributor

Noting that the ScalingProfiler cannot currently be applied to the AD unit model since the ADM1 and modified ADM1 reaction packages have potential divisions by zero (see def rule_I_pH_aa(self)).

Interesting note. What happens exactly? Does ScalingProfiler do some sort of check for potential division by zero beforehand and raise an exception, or do you get back some exception that occurred having to do with division by zero? It’d be good to elaborate on this.

@MarcusHolly
Copy link
Contributor Author

Noting that the ScalingProfiler cannot currently be applied to the AD unit model since the ADM1 and modified ADM1 reaction packages have potential divisions by zero (see def rule_I_pH_aa(self)).

Interesting note. What happens exactly? Does ScalingProfiler do some sort of check for potential division by zero beforehand and raise an exception, or do you get back some exception that occurred having to do with division by zero? It’d be good to elaborate on this.

This is the traceback:

Traceback (most recent call last):
  File "C:\GitHub\watertap\watertap\watertap\unit_models\tests\profiler_sandbox_AD.py", line 187, in <module>
    output = main()
             ^^^^^^
  File "C:\GitHub\watertap\watertap\watertap\unit_models\tests\profiler_sandbox_AD.py", line 181, in main
    results = sp.report_scaling_profiles()
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\mholl\miniconda3\envs\watertap\Lib\site-packages\idaes\core\scaling\scaler_profiling.py", line 166, in report_scaling_profiles
    results = self.profile_scaling_methods()
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\mholl\miniconda3\envs\watertap\Lib\site-packages\idaes\core\scaling\scaler_profiling.py", line 152, in profile_scaling_methods
    results[case] = self.run_case(meth, **margs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\mholl\miniconda3\envs\watertap\Lib\site-packages\idaes\core\scaling\scaler_profiling.py", line 246, in run_case
    manual = self._run_scenario(
             ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\mholl\miniconda3\envs\watertap\Lib\site-packages\idaes\core\scaling\scaler_profiling.py", line 289, in _run_scenario
    self._apply_scaling(m, scaling_method, block_based=block_based, **kwargs)
  File "C:\Users\mholl\miniconda3\envs\watertap\Lib\site-packages\idaes\core\scaling\scaler_profiling.py", line 281, in _apply_scaling
    scaling_method(c, **kwargs)
  File "C:\Users\mholl\miniconda3\envs\watertap\Lib\site-packages\idaes\core\scaling\custom_scaler_base.py", line 505, in scale_constraint_by_nominal_derivative_norm
    value(
  File "C:\Users\mholl\miniconda3\envs\watertap\Lib\site-packages\pyomo\common\numeric_types.py", line 382, in value
    tmp = obj(exception=True)
          ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\mholl\miniconda3\envs\watertap\Lib\site-packages\pyomo\core\expr\base.py", line 118, in __call__
    return visitor.evaluate_expression(self, exception)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\mholl\miniconda3\envs\watertap\Lib\site-packages\pyomo\core\expr\visitor.py", line 1301, in evaluate_expression
    ans = visitor.dfs_postorder_stack(exp)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\mholl\miniconda3\envs\watertap\Lib\site-packages\pyomo\core\expr\visitor.py", line 936, in dfs_postorder_stack
    ans = self.visit(_obj, _result)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\mholl\miniconda3\envs\watertap\Lib\site-packages\pyomo\core\expr\visitor.py", line 1187, in visit
    return node._apply_operation(values)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\mholl\miniconda3\envs\watertap\Lib\site-packages\pyomo\core\expr\numeric_expr.py", line 1124, in _apply_operation
    return result[0] / result[1]
           ~~~~~~~~~~^~~~~~~~~~~
ZeroDivisionError: float division by zero

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IDAES Priority:Normal Normal Priority Issue or PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants