-
Notifications
You must be signed in to change notification settings - Fork 4
/
testoptimization.jl
executable file
·42 lines (34 loc) · 1.47 KB
/
testoptimization.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using Enzyme
using DJUICE
using MAT
using Test
using Optimization, OptimizationOptimJL
#Load model from MATLAB file
file = matopen(joinpath(@__DIR__, "..", "data","temp.mat")) #SMALL model (35 elements)
mat = read(file, "md")
close(file)
md = model(mat)
#make model run faster
md.stressbalance.maxiter = 20
#Now call AD!
md.inversion.iscontrol = 1
md.inversion.onlygrad = 1
md.inversion.independent = md.friction.coefficient
md.inversion.min_parameters = ones(md.mesh.numberofvertices)*(0.0)
md.inversion.max_parameters = ones(md.mesh.numberofvertices)*(1.0e3)
md.inversion.independent_string = "FrictionCoefficient"
md.inversion.dependent_string = ["SurfaceAbsVelMisfit"]
α = md.inversion.independent
∂J_∂α = zero(α)
femmodel=DJUICE.ModelProcessor(md, :StressbalanceSolution)
n = length(α)
# test Enzyme autodiff only
dfemmodel = Enzyme.Compiler.make_zero(Base.Core.Typeof(femmodel), IdDict(), femmodel)
autodiff(Enzyme.Reverse, DJUICE.costfunction, Active, Duplicated(α, ∂J_∂α), Duplicated(femmodel,dfemmodel))
# use user defined grad, errors!
optprob = OptimizationFunction(DJUICE.costfunction, Optimization.AutoEnzyme())
#prob = Optimization.OptimizationProblem(optprob, α, femmodel, lb=md.inversion.min_parameters, ub=md.inversion.max_parameters)
prob = Optimization.OptimizationProblem(optprob, α, femmodel)
sol = Optimization.solve(prob, Optimization.LBFGS())
#sol = Optimization.solve(prob, Optim.GradientDescent())
#sol = Optimization.solve(prob, Optim.NelderMead())