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

Add consistent scaling for optimization problems #29571

Open
wants to merge 2 commits into
base: next
Choose a base branch
from

Conversation

maxnezdyur
Copy link
Contributor

closes #29570

@maxnezdyur
Copy link
Contributor Author

@lynnmunday
This fixes the scaling problems.

@dschwen dschwen changed the title add consistent scaling for optimization problems #29570 Add consistent scaling for optimization problems Dec 18, 2024
@maxnezdyur maxnezdyur force-pushed the cons_scaling branch 2 times, most recently from 4d6fc7d to 75dd85b Compare December 18, 2024 20:38
@moosebuild
Copy link
Contributor

moosebuild commented Dec 18, 2024

Job Documentation, step Docs: sync website on 904462c wanted to post the following:

View the site here

This comment will be updated on new commits.

modules/optimization/src/executioners/AdjointSolve.C Outdated Show resolved Hide resolved

// This is to prevent automatic scaling of the adjoint system. Scaling is
// taken from the forward system
if (_nl_adjoint.hasVector("scaling_factors"))
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to be clear, the above error is checking for manual scaling and this if statement is stripping away adjoint variable scaling when automatic_scaling=true.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct

nl_abs_tol = 1e-10
l_tol = 1e-10

automatic_scaling = false
Copy link
Contributor

Choose a reason for hiding this comment

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

does automatic_scaling=true override the manual variable scaling?

Copy link
Contributor

Choose a reason for hiding this comment

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

Does all of this still work when they use residual_vs_jacobian scaling of somethign other than the default, like 1 or 0.5 or 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes works with all of those

input = main.i
jsondiff = main_out.json
recover = false
cli_args = "MultiApps/forward/cli_args='Executioner/automatic_scaling=true'"
Copy link
Contributor

Choose a reason for hiding this comment

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

Same question as above, does this override the manual scaling?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, when setting verbose=true, it outputs the new scalings which are different from the manual.

@@ -52,6 +53,14 @@ AdjointSolve::AdjointSolve(Executioner & ex)
paramError("forward_system", "Forward system does not appear to be a 'NonlinearSystem'.");
if (!dynamic_cast<NonlinearSystem *>(&_nl_adjoint))
paramError("adjoint_system", "Adjoint system does not appear to be a 'NonlinearSystem'.");
// Adjoint system should never perform it's own automatic scaling. Scaling is
// taken from the forward system
_nl_adjoint.automaticScaling(false);
Copy link
Contributor

Choose a reason for hiding this comment

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

do you need this and the below if statement on 197 or do you include both to be extra careful?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I include both to be careful.

@@ -95,6 +104,10 @@ AdjointSolve::solve()

// Solve the adjoint system
solver.adjoint_solve(matrix, solution, rhs, tol, maxits);
Copy link
Contributor

Choose a reason for hiding this comment

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

Line 109 is a really smart way of doing this. For my clarification, the adjoint solve in line 106 is using the scaled system matrix from the forward system on line 90. Do you think it matters that our solution is from A^T=lambda.
We should try this with a nonsymmetric A. Regular scaling of the system Ax=b would be AD^-1Dx=b. The adjoint system would be A^Tx=b so (AD^-1D)^Tx=b -> (D^TD^-TA^T)x=b.
So I wonder if D needs to be transposed for the adjoint solve. Maybe this doesn't matter because D is for blocks and not columns of matrix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we have an example of a nonsymmetric A in the tests that I could "borrow"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also a diagonal matrix transposed is just the same diagonal matrix.

Copy link
Contributor

Choose a reason for hiding this comment

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

I didn't think of that. You're right. I'll try to get a thermal mechanical example we can try.

input = main.i
max_threads = 1 # Optimize executioner does not support multiple threads
cli_args = "MultiApps/forward/cli_args='Variables/T_real_adj/scaling=5'"
expect_err = 'User supplied scaling factors for adjoint variables. Adjoint system is scaled automatically by the forward system.'
Copy link
Contributor

@lynnmunday lynnmunday Dec 19, 2024

Choose a reason for hiding this comment

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

This will fix the failing tests if I copied it in correctly.

Suggested change
expect_err = 'User supplied scaling factors for adjoint variables. Adjoint system is scaled automatically by the forward system.'
expect_err = 'User cannot supply scaling factors for adjoint variables. Adjoint system is scaled automatically by the forward system.'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optimization Auto Adjoint doesn't work with scaling of the system
3 participants