AmrexCastro.jl 7.47 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
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} = []
117
118
  Yd_Level1::Array{Float64,1} = []
  Yd_Level2::Array{Float64,1} = []
119
120
    
  caseIDs::Array{String,1} = []
William F Godoy's avatar
William F Godoy committed
121
  caseDirs::Array{String,1} = []
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

  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
137
138
139
140
    plotsSizeLevel0::Int64 = 0
    plotsSizeLevel1::Int64 = 0
    plotsSizeLevel2::Int64 = 0
    
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
    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)
      
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
      # Yd_Level0
      sizeDirLevel0::Int64 = helper_get_directory_size(string(plotFileDir, "/Level_0"))
      plotsSizeLevel0 += sizeDirLevel0
      push!( Yd_Level0, plotsSizeLevel0)
      
      # Yd_Level1
      sizeDirLevel1::Int64 = helper_get_directory_size(string(plotFileDir, "/Level_1"))
      plotsSizeLevel1 += sizeDirLevel1
      push!( Yd_Level1, plotsSizeLevel1)
      
      # Yd_Level2
      sizeDirLevel2::Int64 = helper_get_directory_size(string(plotFileDir, "/Level_2"))
      plotsSizeLevel2 += sizeDirLevel2
      push!( Yd_Level2, plotsSizeLevel2)
      
      
174
      # caseID
William F Godoy's avatar
William F Godoy committed
175
176
177
178
      push!(caseIDs, caseID )
      
      # caseDir
      push!(caseDirs, helper_get_relative_path(plotFileDir) )
179
180
181
182
183
184
185
186
      
      counter = counter + 1
    end
    
  end

  data = [(Xd,Yd)]
  
187
188
189
190
191
192
193
194
  df::DataFrames.DataFrame = 
      DataFrames.DataFrame( plots_cells = Xd, 
                            plots_size = Yd, 
                            plots_size_L0 = Yd_Level0,
                            plots_size_L1 = Yd_Level1,
                            plots_size_L2 = Yd_Level2,
                            caseID = caseIDs, 
                            caseDir = caseDirs)
195
196
197
198
199
200
201
202
  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
203
# dead function
William F Godoy's avatar
William F Godoy committed
204
function _run_linear_model_plots_size(extractor::AmrexCastro, X::DataFrames.DataFrame)
205

William F Godoy's avatar
William F Godoy committed
206
    outputDirs = helper_get_prefix_directories(extractor.outputPrefix)
207

William F Godoy's avatar
William F Godoy committed
208
    # single column DataFrame Y in the linear model
209
    plotsSizesData = DataFrames.DataFrame(plots_sizes = Int64[])
William F Godoy's avatar
William F Godoy committed
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225

    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
226

William F Godoy's avatar
William F Godoy committed
227
        push!(plotsSizesData, [plotsSize])
William F Godoy's avatar
William F Godoy committed
228
    end
229

William F Godoy's avatar
William F Godoy committed
230
    println(plotsSizesData)
231

William F Godoy's avatar
William F Godoy committed
232
233
    # Prepare the X independent variables in the linear model
    XNames::Array{String} = extractor.outputs["plots_size"]
234
235
236
237
238
239

    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
240
241

    #for XName in XNames
242
243
244
    #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
245
    #end
246

William F Godoy's avatar
William F Godoy committed
247
248
249
    # 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])
250

William F Godoy's avatar
William F Godoy committed
251
    println(plotsSizesData)
252
    f = @eval(GLM.@formula($response ~ (+)($(predictors...))))
253
254


William F Godoy's avatar
William F Godoy committed
255
    ols = GLM.lm(f, plotsSizesData)
256
257
258
    println("I/O linear model formula:")
    println(ols)

William F Godoy's avatar
William F Godoy committed
259
260
    #import Plots
    #display(Plots.plot(X, Y))
William F Godoy's avatar
William F Godoy committed
261
end