Skip to content

Commit

Permalink
renaming, reorganization, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
juddmehr committed Apr 9, 2024
1 parent dfd2eaf commit d82fc86
Show file tree
Hide file tree
Showing 14 changed files with 585 additions and 166 deletions.
9 changes: 6 additions & 3 deletions src/DuctAPE.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ using ImplicitAD # used for all solves
using LinearAlgebra # linear solve and LU decomposition

# General Nonlinear solves
using SimpleNonlinearSolve
using SimpleNonlinearSolve # SimpleDFSane and SimpleNewtonRaphson

# Quasi-Newton
using SIAMFANLEquations

# Fixed-Point Iteration Solvers
# using FixedPointAcceleration
using SpeedMapping
using FixedPoint

# For using NLsolve
using NLsolve #for newton solver
using NLsolve #Includes Anderson Solver
using LineSearches # used in newton solver
using ForwardDiff # used for jacobian for newton solver

Expand Down
1 change: 1 addition & 0 deletions src/analysis/analyses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ function process(
solve_parameter_cache_dims,
# Cache(s)
solve_container_caching...,
solve_parameter_tuple..., # contains SIAMFANLE containers needed for solver definition
)

# - Solve with ImplicitAD - #
Expand Down
93 changes: 46 additions & 47 deletions src/preprocess/geometry/elliptic_grid_residuals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,48 +129,48 @@ function elliptic_grid_residual!(r, y, x, p)
return r
end

function solve_elliptic_grid_iad(x, p)
function solve_elliptic_grid(grid, constants)

# - Wrap residual - #
function rwrap!(r, y)
return elliptic_grid_residual!(r, y, x, p)
function rwrap!(resid, states)
return elliptic_grid_residual!(resid, states, constants)
end

wake_grid = reshape(x, p.gridshape)

# - Call NLsolve - #
# df = OnceDifferentiable(rwrap!, x, similar(x))
result = NLsolve.nlsolve(
# df,
rwrap!,
reshape(@view(wake_grid[:, 2:end, 2:(end - 1)]), :);
# method=:newton,
# autodiff=:forward,
method=p.wake_nlsolve_method,
autodiff=p.wake_nlsolve_autodiff,
linsolve=(x, A, b) -> x .= ImplicitAD.implicit_linear(A, b),
ftol=p.wake_nlsolve_ftol,
iterations=p.wake_max_iter,
show_trace=p.verbose,
wake_grid = reshape(grid, constants.gridshape)

# build problem object
prob = SimpleNonlinearSolve.NonlinearProblem(
rwrap!, reshape(@view(wake_grid[:, 2:end, 2:(end - 1)]), :);
)

# - SOLVE - #
if verbose
println(" " * "Nonlinear Solve Trace:")
end
sol = SimpleNonlinearSolve.solve(
prob, # problem
constants.algorithm();
abstol=constants.atol,
maxiters=constants.iteration_limit,
)

p.converged[1] = NLsolve.converged(result)
# update convergence flag
constants.converged[1] = SciMLBase.successful_retcode(sol)

# - overwrite output in place - #
wake_grid[:, 2:end, 2:(end - 1)] .= reshape(result.zero, p.itshape)
wake_grid[:, 2:end, 2:(end - 1)] .= reshape(sol.u, constants.itshape)

return x
return grid
end

"""
TODO: will want to pass a convergence flag that get's updated so we can break out if the wake geometry did not converge and pass a fail flag to the optimizer
"""
function solve_elliptic_grid!(
wake_grid;
wake_nlsolve_method=:newton,
wake_nlsolve_autodiff=:forward,
wake_nlsolve_ftol=1e-14,
wake_max_iter=10,
algorithm=SimpleNonlinearSolve.SimpleDFSane,
atol=1e-14,
iteration_limit=10,
converged=[false],
verbose=false,
)
Expand All @@ -194,16 +194,15 @@ function solve_elliptic_grid!(
gridshape,
proposed_grid_cache=DiffCache(wake_grid),
itshape=(gridshape[1], gridshape[2] - 1, gridshape[3] - 2),
wake_nlsolve_method,
wake_nlsolve_autodiff,
wake_nlsolve_ftol,
wake_max_iter,
algorithm,
atol,
iteration_limit,
converged,
verbose,
)

# - solve - #
y = implicit(solve_elliptic_grid_iad, elliptic_grid_residual!, reshape(wake_grid, :), p)
y = implicit(solve_elliptic_grid, elliptic_grid_residual!, reshape(wake_grid, :), p)

for g in eachrow(view(y, :))
if g[1] < eps()
Expand All @@ -221,7 +220,7 @@ end
# DFDC-like wake relaxation #
#---------------------------------#
"""
relax_grid!(xg, rg, nxi, neta; max_wake_relax_iter, wake_relax_tol)
relax_grid!(xg, rg, nxi, neta; relaxation_iteration_limit, relaxation_atol)
Relax wake_grid using elliptic wake_grid solver.
Expand All @@ -232,17 +231,17 @@ Relax wake_grid using elliptic wake_grid solver.
- `neta::Int` : number of eta (r) stations in the wake_grid
# Keyword Arguments:
- `max_wake_relax_iter::Int` : maximum number of iterations to run, default=100
- `wake_relax_tol::Float` : convergence tolerance, default = 1e-9
- `relaxation_iteration_limit::Int` : maximum number of iterations to run, default=100
- `relaxation_atol::Float` : convergence tolerance, default = 1e-9
# Returns:
- `x_relax_points::Matrix{Float64}` : Relaxed x wake_grid points
- `r_relax_points::Matrix{Float64}` : Relaxed r wake_grid points
"""
function relax_grid!(
wake_grid;
max_wake_relax_iter=100,
wake_relax_tol=1e-9,
relaxation_iteration_limit=100,
relaxation_atol=1e-9,
converged,
verbose=false,
silence_warnings=true,
Expand All @@ -263,15 +262,15 @@ function relax_grid!(

#set up relaxation factors
#TODO: how are these decided?
if max_wake_relax_iter > 0
if relaxation_iteration_limit > 0
relaxfactor1 = 1.0
relaxfactor2 = 1.1
relaxfactor3 = 1.4
else
relaxfactor1 = 1.0
relaxfactor2 = 1.0
relaxfactor3 = 1.0
max_wake_relax_iter = 1
relaxation_iteration_limit = 1
end

relaxfactor = relaxfactor1
Expand Down Expand Up @@ -307,9 +306,9 @@ function relax_grid!(
#next dfdc goes to AXELL function
#skip over most of the stuff since the intlet and walls don't get relaxed

for iterate in 1:max_wake_relax_iter
for iterate in 1:relaxation_iteration_limit
if verbose
println(tabchar^(ntab)*"iteration $iterate")
println(tabchar^(ntab) * "iteration $iterate")
end

#initialize max step
Expand Down Expand Up @@ -513,11 +512,11 @@ function relax_grid!(

# -- Update relaxation factors
#TODO: need to figure out how the dset numbers and relaxation factors are chosen. Why are they the values that they are? Does it have something to do with the SLOR setup?
if dmax < wake_relax_tol * dxy
if dmax < relaxation_atol * dxy
if verbose
println(tabchar^(ntab)*"Total iterations: $iterate")
println(tabchar^(ntab) * "Total iterations: $iterate")
end
converged[1]=true
converged[1] = true
return wake_grid
end

Expand All @@ -533,19 +532,19 @@ function relax_grid!(

if dmax < dset3 * dxy
if verbose
println(tabchar^(ntab)*"Total iterations = $iterate")
println(tabchar^(ntab) * "Total iterations = $iterate")
end
converged[1]=true
converged[1] = true
return wake_grid
end
end

if verbose
println(tabchar^(ntab)*"Total iterations = ", max_wake_relax_iter, "\n")
println(tabchar^(ntab) * "Total iterations = ", relaxation_iteration_limit, "\n")
end

if !silence_warnings
@warn "Wake grid relaxation did not converge, iteration limit of $(max_wake_relax_iter) met."
@warn "Wake grid relaxation did not converge, iteration limit of $(relaxation_iteration_limit) met."
end

return wake_grid
Expand Down
42 changes: 21 additions & 21 deletions src/preprocess/geometry/wake_geometry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,10 @@ function generate_wake_grid(
tip_gap1,
zwake;
grid_solver_options=GridSolverOptions(),
# wake_nlsolve_ftol=1e-14,
# wake_max_iter=100,
# max_wake_relax_iter=3,
# wake_relax_tol=1e-14,
# atol=1e-14,
# iteration_limit=100,
# relaxation_iteration_limit=3,
# relaxation_atol=1e-14,
verbose=false,
silence_warnings=true,
)
Expand All @@ -215,10 +215,10 @@ function generate_wake_grid(
Rtip1,
tip_gap1,
zwake;
# wake_nlsolve_ftol=wake_nlsolve_ftol,
# wake_max_iter=wake_max_iter,
# max_wake_relax_iter=max_wake_relax_iter,
# wake_relax_tol=wake_relax_tol,
# atol=atol,
# iteration_limit=iteration_limit,
# relaxation_iteration_limit=relaxation_iteration_limit,
# relaxation_atol=relaxation_atol,
verbose=verbose,
silence_warnings=silence_warnings,
)
Expand All @@ -233,10 +233,10 @@ function generate_wake_grid!(
tip_gap1,
zwake;
grid_solver_options=grid_solver_options,
# wake_nlsolve_ftol=1e-14,
# wake_max_iter=100,
# max_wake_relax_iter=3,
# wake_relax_tol=1e-14,
# atol=1e-14,
# iteration_limit=100,
# relaxation_iteration_limit=3,
# relaxation_atol=1e-14,
verbose=false,
silence_warnings=true,
)
Expand Down Expand Up @@ -281,8 +281,8 @@ function relax_grid!(
# - Relax grid to allow Newton solve a tractable starting point - #
relax_grid!(
wake_grid;
max_wake_relax_iter=grid_solver_options.max_wake_relax_iter,
wake_relax_tol=grid_solver_options.wake_relax_tol,
relaxation_iteration_limit=grid_solver_options.relaxation_iteration_limit,
relaxation_atol=grid_solver_options.relaxation_atol,
converged=grid_solver_options.converged,
verbose=verbose,
tabchar="\t",
Expand All @@ -300,10 +300,10 @@ function relax_grid!(
# solve
solve_elliptic_grid!(
wake_grid;
wake_nlsolve_method=grid_solver_options.wake_nlsolve_method,
wake_nlsolve_autodiff=grid_solver_options.wake_nlsolve_autodiff,
wake_nlsolve_ftol=grid_solver_options.wake_nlsolve_ftol,
wake_max_iter=grid_solver_options.wake_max_iter,
algorithm=grid_solver_options.algorithm,
autodiff=grid_solver_options.autodiff,
atol=grid_solver_options.atol,
iteration_limit=grid_solver_options.iteration_limit,
grid_solver_options.converged,
verbose=verbose,
)
Expand All @@ -324,8 +324,8 @@ function relax_grid!(
end
relax_grid!(
wake_grid;
max_wake_relax_iter=grid_solver_options.max_wake_relax_iter,
wake_relax_tol=grid_solver_options.wake_relax_tol,
relaxation_iteration_limit=grid_solver_options.relaxation_iteration_limit,
relaxation_atol=grid_solver_options.relaxation_atol,
converged=grid_solver_options.converged,
verbose=verbose,
tabchar="\t",
Expand All @@ -344,7 +344,7 @@ Generate paneling for each wake line emanating from the rotor blade elements.
- `rgrid::Matrix{Float}` : r-location of each wake_grid point
# Keyword Arguments:
- `method::FLOWFoil.AxisymmetricProblem` : default = AxisymmetricProblem(Vortex(Constant()), Dirichlet(), [false, true]),
- `algorithm::FLOWFoil.AxisymmetricProblem` : default = AxisymmetricProblem(Vortex(Constant()), Dirichlet(), [false, true]),
# Returns:
- `wake_panels::Vector{FLOWFoil.AxisymmetricPanel}` : vector of panel objects describing the wake lines
Expand Down
Loading

0 comments on commit d82fc86

Please sign in to comment.