@@ -7,59 +7,66 @@ using Test
77using ManualNLPModels, JSOSolvers
88using MadNLP
99
10+ onlygrad = 1
1011
1112# Load model from MATLAB file
12- file = matopen (joinpath (@__DIR__ , " .." , " data" ," temp .mat" )) # SMALL model (35 elements)
13+ file = matopen (joinpath (@__DIR__ , " .." , " data" ," PIG_testopt .mat" )) # SMALL model (35 elements)
1314mat = read (file, " md" )
1415close (file)
1516md = model (mat)
1617
1718# make model run faster
1819md. stressbalance. maxiter = 20
1920
21+
2022# Now call AD!
2123md. inversion. iscontrol = 1
22- md. inversion. onlygrad = 0
24+ md. inversion. onlygrad = onlygrad
2325md. inversion. independent = md. friction. coefficient
2426md. inversion. min_parameters = ones (md. mesh. numberofvertices)* (0.0 )
2527md. inversion. max_parameters = ones (md. mesh. numberofvertices)* (1.0e3 )
2628md. inversion. independent_string = " FrictionCoefficient"
2729md. inversion. dependent_string = [" SurfaceAbsVelMisfit" ]
2830
29- α = md. inversion. independent
30- ∂J_∂α = zero (α)
3131
32+ if onlygrad == 1
33+ md = solve (md, :sb )
34+ else
35+ α = md. inversion. independent
36+ ∂J_∂α = zero (α)
3237
33- # cost function f
34- f (x) = begin
35- femmodel= DJUICE. ModelProcessor (md, :StressbalanceSolution )
36- DJUICE. costfunction (x, femmodel)
37- end
38+ g! (∂J_∂α,α)
3839
39- # Enzyme gradient g
40- g! (gx, x) = begin
41- femmodel= DJUICE. ModelProcessor (md, :StressbalanceSolution )
42- dfemmodel = Enzyme. Compiler. make_zero (Base. Core. Typeof (femmodel), IdDict (), femmodel)
43- Enzyme. autodiff (set_runtime_activity (Enzyme. Reverse), DJUICE. costfunction, Active, Duplicated (x, gx), Duplicated (femmodel,dfemmodel))
44- gx
45- end
40+ # cost function f
41+ f (x) = begin
42+ femmodel= DJUICE. ModelProcessor (md, :StressbalanceSolution )
43+ DJUICE. costfunction (x, femmodel)
44+ end
4645
47- nlp = NLPModel (
48- α,
49- f,
50- grad = g!,
51- )
46+ # Enzyme gradient g
47+ g! (gx, x) = begin
48+ femmodel= DJUICE. ModelProcessor (md, :StressbalanceSolution )
49+ dfemmodel = Enzyme. Compiler. make_zero (Base. Core. Typeof (femmodel), IdDict (), femmodel)
50+ Enzyme. autodiff (set_runtime_activity (Enzyme. Reverse), DJUICE. costfunction, Active, Duplicated (x, gx), Duplicated (femmodel,dfemmodel))
51+ gx
52+ end
5253
53- # output = lbfgs(nlp)
54+ nlp = NLPModel (
55+ α,
56+ f,
57+ grad = g!,
58+ )
5459
55- results_qn = madnlp (
56- nlp;
57- linear_solver= LapackCPUSolver,
58- hessian_approximation= MadNLP. CompactLBFGS,
59- )
60+ # output = lbfgs(nlp)
6061
62+ results_qn = madnlp (
63+ nlp;
64+ linear_solver= LapackCPUSolver,
65+ hessian_approximation= MadNLP. CompactLBFGS,
66+ )
6167
6268
69+ end
6370# use user defined grad, errors!
6471# optprob = OptimizationFunction(DJUICE.costfunction, Optimization.AutoEnzyme(; mode=set_runtime_activity(Enzyme.Reverse)))
6572# prob = Optimization.OptimizationProblem(optprob, α, femmodel, lb=md.inversion.min_parameters, ub=md.inversion.max_parameters)
0 commit comments