AmrexCastro.jl 4.69 KB
Newer Older
William F Godoy's avatar
William F Godoy committed
1

2
include("../helper/helperSystem.jl")
3

William F Godoy's avatar
William F Godoy committed
4 5
import DataFrames
import GLM
William F Godoy's avatar
William F Godoy committed
6 7

struct AmrexCastro <: AbstractAmrex
William F Godoy's avatar
William F Godoy committed
8 9
    app::String
    outputPrefix::String
William F Godoy's avatar
William F Godoy committed
10 11
    inputs::Array{String}
    outputs::Dict{String,Array{String}}
William F Godoy's avatar
William F Godoy committed
12
    runlogFile::String
William F Godoy's avatar
William F Godoy committed
13

William F Godoy's avatar
William F Godoy committed
14
    AmrexCastro(outputPrefix::String, runlogFile::String) = new(
William F Godoy's avatar
William F Godoy committed
15 16 17 18 19 20 21 22 23 24 25
        "AmrexCastro",
        outputPrefix,
        [
            "max_step",
            "amr.check_int",
            "amr.plot_int",
            "amr.n_cell",
            "amr.max_level",
            "castro.cfl",
            "castro.max_grid_size",
        ],
William F Godoy's avatar
William F Godoy committed
26
        Dict{String,Array{String}}(
William F Godoy's avatar
William F Godoy committed
27
            "plots_size" => ["amr.nplot_files", "amr.ncells"],
28 29 30 31 32 33 34
            "checks_size" => [
                "amr.check_int",
                "amr.ncheck_files",
                "amr.ncells",
                "amr.max_level",
                "amr.regrid_int",
            ],
William F Godoy's avatar
William F Godoy committed
35 36
        ),
        runlogFile,
William F Godoy's avatar
William F Godoy committed
37
    )
William F Godoy's avatar
William F Godoy committed
38 39
end

William F Godoy's avatar
William F Godoy committed
40 41
# runs the linear models for each entry in outputs
function run_linear_models(extractor::AmrexCastro)
William F Godoy's avatar
William F Godoy committed
42

William F Godoy's avatar
William F Godoy committed
43
    X::DataFrames.DataFrame = _get_linear_model_X(extractor)
William F Godoy's avatar
William F Godoy committed
44

William F Godoy's avatar
William F Godoy committed
45
    for key in keys(extractor.outputs)
William F Godoy's avatar
William F Godoy committed
46 47 48
        if (key == "plots_size")
            _run_linear_model_plots_size(extractor, X)
        end
William F Godoy's avatar
William F Godoy committed
49 50
    end
end
51

William F Godoy's avatar
William F Godoy committed
52

William F Godoy's avatar
William F Godoy committed
53
function _get_input_file(extractor::AmrexCastro, outputDir::String)::String
William F Godoy's avatar
William F Godoy committed
54

William F Godoy's avatar
William F Godoy committed
55 56
    # hardcoded input...find out if this is always the case with Castro   
    inputFiles::Array{String} = helper_get_prefix_files("input", outputDir)
William F Godoy's avatar
William F Godoy committed
57

William F Godoy's avatar
William F Godoy committed
58
    if isempty(inputFiles)
William F Godoy's avatar
William F Godoy committed
59 60 61 62
        throw(NoSuchFieldException(
            "jexio AmrexCastro: Can't find Castro input files in directories:",
            outputDirs,
        ))
William F Godoy's avatar
William F Godoy committed
63
        # use length, instead of size as it returns tuples
William F Godoy's avatar
William F Godoy committed
64 65 66 67 68
    elseif length(inputFiles) != 1
        print("Number of files: ", size(inputFiles))
        throw(DimensionMismatch("jexio AmrexCastro: found more than one input file"))
    else
    end
69

William F Godoy's avatar
William F Godoy committed
70
    # print(inputFiles)
William F Godoy's avatar
William F Godoy committed
71 72
    return inputFiles[1]
end
William F Godoy's avatar
William F Godoy committed
73 74 75 76 77 78



function _get_linear_model_X(extractor::AmrexCastro)::DataFrames.DataFrame
    # get directories runs output location
    outputDirs = helper_get_prefix_directories(extractor.outputPrefix)
William F Godoy's avatar
William F Godoy committed
79
    println(outputDirs)
William F Godoy's avatar
William F Godoy committed
80 81 82

    X::DataFrames.DataFrame = DataFrames.DataFrame()
    isXInit::Bool = false
William F Godoy's avatar
William F Godoy committed
83

William F Godoy's avatar
William F Godoy committed
84 85
    for outputDir in outputDirs
        inputFile::String = _get_input_file(extractor, outputDir)
William F Godoy's avatar
William F Godoy committed
86 87
        # get relevant input parameters
        parameters = _input_parser(extractor, inputFile)
William F Godoy's avatar
William F Godoy committed
88 89 90
        independentVariables = _get_independent_variables(extractor, parameters)
        # get outputDir name
        independentVariables["caseID"] = helper_get_relative_path(outputDir)
William F Godoy's avatar
William F Godoy committed
91 92

        if !isXInit
William F Godoy's avatar
William F Godoy committed
93
            X = DataFrames.DataFrame(independentVariables)
William F Godoy's avatar
William F Godoy committed
94 95
            isXInit = true
        else
William F Godoy's avatar
William F Godoy committed
96
            DataFrames.push!(X, independentVariables)
William F Godoy's avatar
William F Godoy committed
97 98 99 100
        end
    end

    println(X)
William F Godoy's avatar
William F Godoy committed
101
    return X
102 103 104
end


William F Godoy's avatar
William F Godoy committed
105
function _run_linear_model_plots_size(extractor::AmrexCastro, X::DataFrames.DataFrame)
106

William F Godoy's avatar
William F Godoy committed
107
    outputDirs = helper_get_prefix_directories(extractor.outputPrefix)
108

William F Godoy's avatar
William F Godoy committed
109
    # single column DataFrame Y in the linear model
110
    plotsSizesData = DataFrames.DataFrame(plots_sizes = Int64[])
William F Godoy's avatar
William F Godoy committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

    for outputDir in outputDirs

        # TODO refactor this later
        inputFile::String = _get_input_file(extractor, outputDir)
        parameters = _input_parser(extractor, inputFile)
        rootPlotName = get(parameters, "amr.plot_file", "")

        # find all directories with rootPlotName and get its size
        plotFileDirs = helper_get_prefix_directories(string(outputDir, "/", rootPlotName))

        plotsSize::Int64 = 0
        for plotFileDir in plotFileDirs
            sizeDir::Int64 = helper_get_directory_size(plotFileDir)
            plotsSize += sizeDir
        end
127

William F Godoy's avatar
William F Godoy committed
128
        push!(plotsSizesData, [plotsSize])
William F Godoy's avatar
William F Godoy committed
129
    end
130

William F Godoy's avatar
William F Godoy committed
131 132
    println(plotsSizesData)
    
William F Godoy's avatar
William F Godoy committed
133 134
    # Prepare the X independent variables in the linear model
    XNames::Array{String} = extractor.outputs["plots_size"]
William F Godoy's avatar
William F Godoy committed
135 136 137 138 139 140 141 142 143 144 145
      
   col1 = Symbol("amr.nplot_files")
   col2 = Symbol("amr.ncells")
   
   insert!(plotsSizesData, 2 ,X[!,col1] .* X[!,col2], :new_data) 
   #plotsSizesData[!,col1] = 
    
                                    

    #for XName in XNames
        #columnName = Symbol(XName)
146
        # this syntax [!, symbol] does not make a copy, use [:, symbol] for copies
William F Godoy's avatar
William F Godoy committed
147 148
        #plotsSizesData[!, columnName] = X[!, columnName]
    #end
149

William F Godoy's avatar
William F Godoy committed
150 151 152
    # formula from https://discourse.julialang.org/t/glm-jl-with-unknown-column-names/20692/5
    response = Symbol(names(plotsSizesData)[1])
    predictors = Symbol.(names(plotsSizesData)[2:end])
153

William F Godoy's avatar
William F Godoy committed
154
    println(plotsSizesData)
William F Godoy's avatar
William F Godoy committed
155
    f = @eval(GLM.@formula($response ~ (+)( $(predictors...))))
156 157


William F Godoy's avatar
William F Godoy committed
158
    ols = GLM.lm(f, plotsSizesData)
159 160 161
    println("I/O linear model formula:")
    println(ols)

William F Godoy's avatar
William F Godoy committed
162 163
    #import Plots
    #display(Plots.plot(X, Y))
William F Godoy's avatar
William F Godoy committed
164
end