Amrex.jl 3.91 KB
Newer Older
William F Godoy's avatar
William F Godoy committed
1
2
3
4
5
6

abstract type AbstractAmrex <: AbstractExtractor end

mutable struct Amrex <: AbstractAmrex
    app::String
    outputPrefix::String
William F Godoy's avatar
William F Godoy committed
7
8
    inputs::Array{String}
    outputs::Array{String}
William F Godoy's avatar
William F Godoy committed
9
    runlogFile::String
William F Godoy's avatar
William F Godoy committed
10

William F Godoy's avatar
William F Godoy committed
11
    Amrex(outputPrefix::String, runlogFile::String) = new(
William F Godoy's avatar
William F Godoy committed
12
13
        "Amrex",
        outputPrefix,
14
15
16
17
18
19
20
21
        [
            "max_step",
            "amr.check_int",
            "amr.plot_int",
            "amr.n_cell",
            "amr.max_level",
            "caseID",
        ],
William F Godoy's avatar
William F Godoy committed
22
        ["plots_size", "checkpoints_size"],
William F Godoy's avatar
William F Godoy committed
23
        runlogFile,
William F Godoy's avatar
William F Godoy committed
24
    )
William F Godoy's avatar
William F Godoy committed
25
26
27
28
29
30
31
32
33
end


"""
    Parses an input file with entries key = value, returns all entries in a Dict
    # Arguments
    - `extractor::AbstractAmrex` : input type extending AbstractAmrex
    - `inputFile::String` : input file to be parsed, absolute path is preferred
"""
William F Godoy's avatar
William F Godoy committed
34
function _input_parser(extractor::AbstractAmrex, inputFile::String)::Dict{String,String}
William F Godoy's avatar
William F Godoy committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

    parameters = Dict{String,String}()
    # get file contents in a single iterable type, \n newline is removed 
    fileContents = readlines(inputFile)

    # go through each line
    for line in fileContents
        if isempty(line)
            continue
        end

        # remove comments marked with #
        result = findfirst("#", line)

        if isnothing(result) == false
            line = SubString(line, 1, result[1] - 1)
            if isempty(line)
                continue
            end
        end

        parameter = split(line, "=")
        parameters[strip(parameter[1])] = strip(parameter[2])

    end

    return parameters
end
63
64
65
66
67
68
69

"""
    Get actual independent variables for I/O characterization: 
    number of cells or ncells, output frequency or nplots, ncheckpoints, etc.    
    from input parameters 
    # Arguments
    - `extractor::AbstractAmrex` : input type extending AbstractAmrex
William F Godoy's avatar
William F Godoy committed
70
    - `parameters::Dict{Any,Any}` : input file to be parsed, absolute path is preferred
71
"""
William F Godoy's avatar
William F Godoy committed
72
function _get_independent_variables(extractor::AbstractAmrex, parameters)::Dict{String,Any}
73

William F Godoy's avatar
William F Godoy committed
74
    independentVariables = Dict{String,Any}()
75
76
77
78
79
80

    # Number of output events, 
    # get is the safe way. 3rd argument is a default fallback if key not found
    maxStepStr = get(parameters, "max_step", "")

    if maxStepStr != ""
William F Godoy's avatar
William F Godoy committed
81
82

        # safe string conversion to a type
83
84
85
86
87
        maxStep = parse(Int64, maxStepStr)

        plotIntStr = get(parameters, "amr.plot_int", "")
        if plotIntStr != ""
            plotFrequency = parse(Int64, plotIntStr)
William F Godoy's avatar
William F Godoy committed
88
            independentVariables["amr.plot_int"] = plotFrequency
89
90
91
92
93
94
95
96
            independentVariables["amr.nplot_files"] = floor(Int32, maxStep / plotFrequency)
        else
            throw(NoSuchFieldException("jexio Amrex: Can't find amr.plot_int"))
        end

        checkIntStr = get(parameters, "amr.check_int", "")
        if checkIntStr != ""
            checkpointFrequency = parse(Int64, checkIntStr)
William F Godoy's avatar
William F Godoy committed
97
98
99
            independentVariables["amr.check_int"] = checkpointFrequency
            independentVariables["amr.ncheck_files"] =
                floor(Int32, maxStep / checkpointFrequency)
100
101
102
103
        else
            throw(NoSuchFieldException("jexio Amrex: Can't find amr.check_int"))
        end

104
    end
105
106
107
108
109
110
111
112
113
114
115
116
117

    # Using the dot "." operator to apply it on an element-by-element basis (like a dot product)
    # input is a dictionary with key="amr.ncell" and value = {"nx" "ny" "nz"},  
    # output is nx*ny*nz
    # Example:  
    # input Dict[ "amr.n_cell" => ["16" "16"] ], if key not found return 0
    # >ncells
    #   256
    ncells = prod(parse.(Int64, split(get(parameters, "amr.n_cell", ["0"]))))
    independentVariables["amr.ncells"] = ncells

    maxLevel = get(parameters, "amr.max_level", "")
    independentVariables["amr.max_level"] = maxLevel == "" ? 1 : parse(Int32, maxLevel)
William F Godoy's avatar
William F Godoy committed
118

119
120
    regrid = get(parameters, "amr.regrid_int", "")
    independentVariables["amr.regrid_int"] = regrid == "" ? 1 : parse(Int32, regrid)
William F Godoy's avatar
William F Godoy committed
121

122
123
    return independentVariables

124
end
William F Godoy's avatar
William F Godoy committed
125
126
127
128
129


function _get_inputs_X(extractor::AbstractAmrex)

end