AmrexCastro.jl 6.38 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
6
7
import Plots
import CSV
William F Godoy's avatar
William F Godoy committed
8
9

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

William F Godoy's avatar
William F Godoy committed
16
    AmrexCastro(outputPrefix::String, runlogFile::String) = new(
William F Godoy's avatar
William F Godoy committed
17
18
19
20
21
22
23
24
25
26
27
        "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
28
        Dict{String,Array{String}}(
William F Godoy's avatar
William F Godoy committed
29
            "plots_size" => ["amr.nplot_files", "amr.ncells"],
30
31
32
33
34
35
36
            "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
37
38
        ),
        runlogFile,
William F Godoy's avatar
William F Godoy committed
39
    )
William F Godoy's avatar
William F Godoy committed
40
41
end

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

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

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

William F Godoy's avatar
William F Godoy committed
55

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

William F Godoy's avatar
William F Godoy committed
58
59
    # 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
60

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

William F Godoy's avatar
William F Godoy committed
73
    # print(inputFiles)
William F Godoy's avatar
William F Godoy committed
74
75
    return inputFiles[1]
end
William F Godoy's avatar
William F Godoy committed
76
77
78
79
80
81



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
82
    println(outputDirs)
William F Godoy's avatar
William F Godoy committed
83
84
85

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

William F Godoy's avatar
William F Godoy committed
87
88
    for outputDir in outputDirs
        inputFile::String = _get_input_file(extractor, outputDir)
William F Godoy's avatar
William F Godoy committed
89
90
        # get relevant input parameters
        parameters = _input_parser(extractor, inputFile)
William F Godoy's avatar
William F Godoy committed
91
92
93
        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
94
95

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

    println(X)
William F Godoy's avatar
William F Godoy committed
104
    return X
105
106
107
end


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
function _run_linear_models_plots_size_step(extractor::AmrexCastro, X::DataFrames.DataFrame) 

  # using Flux ML for 1st order fitting
  # input with total_cells * output_number
  Xd::Array{Int64,1} = []
  # output directory size
  Yd::Array{Float64,1} = []
  # output Level0 directory size
  Yd_Level0::Array{Float64,1} = []
    
  caseIDs::Array{String,1} = []

  outputDirs = helper_get_prefix_directories(extractor.outputPrefix)
      
  for (index,outputDir) in enumerate(outputDirs)
    
    inputFile::String = _get_input_file(extractor, outputDir)
    parameters = _input_parser(extractor, inputFile)
    rootPlotName = get(parameters, "amr.plot_file", "")
    # rootCheckpointName = get(parameters, "amr.plot_file", "")

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

    plotsSize::Int64 = 0
    counter::Int32 = 1
    
    # extract ncells
    ncells = X["amr.ncells"][index]
    
    # caseID
    caseID = helper_get_relative_path(outputDir)
    
    for plotFileDir in plotFileDirs
      # Xd
      push!( Xd, counter*ncells)
      
      # Yd
      sizeDir::Int64 = helper_get_directory_size(plotFileDir)
      plotsSize += sizeDir
      push!( Yd, plotsSize)
      
      # caseID
      push!(caseIDs, string(caseID,"/",helper_get_relative_path(plotFileDir) ) )
      
      counter = counter + 1
    end
    
  end

  data = [(Xd,Yd)]
  
  df::DataFrames.DataFrame = DataFrames.DataFrame( plots_cells = Xd, plots_size = Yd, caseID = caseIDs)
  println(df)
  
  CSV.write("plot_size.csv",  df, header=true)
  #display(Plots.scatter(Xd, Yd))
  #readline()
end



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

William F Godoy's avatar
William F Godoy committed
173
    outputDirs = helper_get_prefix_directories(extractor.outputPrefix)
174

William F Godoy's avatar
William F Godoy committed
175
    # single column DataFrame Y in the linear model
176
    plotsSizesData = DataFrames.DataFrame(plots_sizes = Int64[])
William F Godoy's avatar
William F Godoy committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

    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
193

William F Godoy's avatar
William F Godoy committed
194
        push!(plotsSizesData, [plotsSize])
William F Godoy's avatar
William F Godoy committed
195
    end
196

William F Godoy's avatar
William F Godoy committed
197
    println(plotsSizesData)
198

William F Godoy's avatar
William F Godoy committed
199
200
    # Prepare the X independent variables in the linear model
    XNames::Array{String} = extractor.outputs["plots_size"]
201
202
203
204
205
206

    col1 = Symbol("amr.nplot_files")
    col2 = Symbol("amr.ncells")

    insert!(plotsSizesData, 2, X[!, col1] .* X[!, col2], :new_data)
    #plotsSizesData[!,col1] = 
William F Godoy's avatar
William F Godoy committed
207
208

    #for XName in XNames
209
210
211
    #columnName = Symbol(XName)
    # this syntax [!, symbol] does not make a copy, use [:, symbol] for copies
    #plotsSizesData[!, columnName] = X[!, columnName]
William F Godoy's avatar
William F Godoy committed
212
    #end
213

William F Godoy's avatar
William F Godoy committed
214
215
216
    # 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])
217

William F Godoy's avatar
William F Godoy committed
218
    println(plotsSizesData)
219
    f = @eval(GLM.@formula($response ~ (+)($(predictors...))))
220
221


William F Godoy's avatar
William F Godoy committed
222
    ols = GLM.lm(f, plotsSizesData)
223
224
225
    println("I/O linear model formula:")
    println(ols)

William F Godoy's avatar
William F Godoy committed
226
227
    #import Plots
    #display(Plots.plot(X, Y))
William F Godoy's avatar
William F Godoy committed
228
end