# This is a refinement study application for ICEM CFD requested by Kevin Hogan for Bettis Laboratory

# Author: Matthew Middleton (matthew.middleton@ansys.com)

# Launch with:

# icemcfd -app full-path-to-refinement-folder

# Or place refinement folder in the lib/ai_env/scripts folder of ICEM CFD.
# Then launch with:

# icemcfd -app refinement

# To run in batch from a parameter file, insert -batch before -app and append the file
# name after "refinement":

# icemcfd -batch -app full-path-to-refinement-folder params_file
# icemcfd -batch -app refinement params_file

# The parameter file can be written out from the GUI (File->Save application parameters), or can be made up of
# any of the variables in the ::Refinement namespace at the beginning of refinement/init.tcl
# The -batch argument is only needed on Windows if you want to see messages printed

namespace eval ::Refinement {
    variable data

    set data(dir) [pwd]
    set data(nproc) 1
    set data(geomfile) ""
    set data(refinefact) 2
    set data(numrefines) 2
    set data(docoarsemesh) 1
    # refinement law can be linear or exponential
    set data(refinelaw) exponential
    set data(savetetraunsmoothed) 0
    set data(tetsmooth) 1
    set data(tetsmoothiter) 5
    set data(tetsmoothquality) 0.4
    set data(tetnodemerge) 0
    set data(qualitymetrics) [list {Quality} {Aspect ratio}]
    set data(savetetrasmoothed) 0
    set data(runprism) 1
    set data(prismparamsfile) ""
    set data(adjhgts) 0
    set data(adjlayers) 0
    set data(prismsmooth) 1
    set data(prismsmoothiter) 5
    set data(prismsmoothquality) 0.2
    set data(prismnodemerge) 0
    set data(solveformat) Fluent
    set data(acusolveflow) {Steady laminar}
    set data(solvedir) [pwd]
    set data(quality2file) 0
    set data(doperiodiccheck) 0
    set data(periodicparts) ""
}

proc ::Refinement::SetPrismParams {} {
    variable prism

    catch {unset prism}
# .prism_params defaults (used if no .prism_params file specified or parameter doesn't exist in file)
    # law can be exponential, linear, or wb-exponential
    set prism(law) exponential
    set prism(height) 0
    set prism(layers) 6
    set prism(ratio) 1.2
    set prism(total_height) 0
    set prism(smoothing_steps) 12
    set prism(first_layer_smoothing_steps) 1
    set prism(fillet) 0.35
    set prism(max_prism_angle) 176
    set prism(min_prism_quality) 0.00001
    set prism(triangle_quality) inscribed_ratio
}

namespace eval ::Refinement {
    variable appdata

    # Selections available in dropdowns
    set appdata(solvetypes) "AcuSolve CFD++ CFX Fluent"
    set appdata(acusolvetypes) [list {Steady laminar} {Steady turbulent} {Steady turbulent thermal} \
	{Transient laminar} {Transient turbulent} {Transient turbulent thermal}]

    if [info exists ::AIEnvProductDirectory] {
	set appdata(appdir) $::AIEnvProductDirectory
    } else {
	set appdata(appdir) $::env(ICEM_ACN)/lib/ai_env/scripts/refinement
    }
}

option add *Entry.background white
option add *Listbox.background white
source $::Refinement::appdata(appdir)/scripts/refinement.tcl

set paramsfile [lindex $tool_arguments 0]
if [file exists $paramsfile] {
    # License check is after processing this application
    # So need to use InitializeLicense now
    # or set command to aie_guiOpt(initAfterCmd) which is executed after license check
    set aie_guiOpt(initAfterCmd) "::Refinement::RunBatch $paramsfile;	exit 0"
} elseif !$batch_mode {
    ::Refinement::CreateApp
}

# Setting batch_mode in the init.tcl file prevents the regular ICEM CFD gui from being made.
# But if done at this point, the Tcl and Tk files and ICEM CFD procedures will have already
# been sourced so we don't have to source them in this script, and can create a completely
# new gui.
set batch_mode 1
batch_interp eval {set batch_mode 1}
# This line prevents additional arguments from being processed after this application
set tool_arguments ""
# tool_arguments will not contain dash (-) arguments
