|
| 1 | +using ModelingToolkit |
| 2 | +using DynamicQuantities; |
| 3 | +using Unitful |
| 4 | +using LinearAlgebra |
| 5 | + |
| 6 | + |
| 7 | +""" |
| 8 | + no_of_fundamental_dims(mp::Vector{Any}) |
| 9 | + Finds number of fundamental dimensions |
| 10 | +""" |
| 11 | +function no_of_fundamental_dims(mp) |
| 12 | + fundamental_dimensions = 0 |
| 13 | + for val in mp |
| 14 | + if val == 1 |
| 15 | + fundamental_dimensions += 1 |
| 16 | + end |
| 17 | + end |
| 18 | + |
| 19 | + return fundamental_dimensions |
| 20 | +end |
| 21 | + |
| 22 | + |
| 23 | +""" |
| 24 | + get_dims_of_vars(Vector{Any}, Number, Vector{Any}) |
| 25 | +
|
| 26 | + Gets units of each variable in an array. Returns a matrix where each row corresponds |
| 27 | + to binary |
| 28 | + Example : kg*m^3 |
| 29 | + Each index corresponds to length, mass, time, current, luminosity and temperature |
| 30 | + respectively. |
| 31 | +""" |
| 32 | +function get_dims_of_vars(dims_vars, total_vars, dim_map) |
| 33 | + # For every single variable it contains row of 1s and 0s mentioning which unit is present |
| 34 | + dims_of_all_vars = zeros(total_vars, 6) |
| 35 | + |
| 36 | + for (ind, dim) in enumerate(dims_vars) |
| 37 | + temp_dims = 0 |
| 38 | + temp_dims_arr = zeros(6) |
| 39 | + if ulength(dim) != 0 |
| 40 | + dim_map[1] = 1 |
| 41 | + temp_dims_arr[1] = ulength(dim) |
| 42 | + temp_dims += 1 |
| 43 | + end |
| 44 | + if umass(dim) != 0 |
| 45 | + dim_map[2] = 1 |
| 46 | + temp_dims_arr[2] = umass(dim) |
| 47 | + temp_dims += 1 |
| 48 | + end |
| 49 | + if utime(dim) != 0 |
| 50 | + dim_map[3] = 1 |
| 51 | + temp_dims_arr[3] = utime(dim) |
| 52 | + temp_dims +=1 |
| 53 | + end |
| 54 | + if ucurrent(dim) != 0 |
| 55 | + dim_map[4] = 1 |
| 56 | + temp_dims_arr[4] = ucurrent(dim) |
| 57 | + temp_dims +=1 |
| 58 | + end |
| 59 | + if utemperature(dim) != 0 |
| 60 | + dim_map[5] = 1 |
| 61 | + temp_dims_arr[5] = utemperature(dim) |
| 62 | + |
| 63 | + temp_dims +=1 |
| 64 | + end |
| 65 | + if uluminosity(dim) != 0 |
| 66 | + dim_map[6] = 1 |
| 67 | + temp_dims_arr[6] = uluminosity(dim) |
| 68 | + temp_dims +=1 |
| 69 | + end |
| 70 | + dims_of_all_vars[ind,:] = temp_dims_arr' |
| 71 | + end |
| 72 | + |
| 73 | + return dims_of_all_vars |
| 74 | +end |
| 75 | + |
| 76 | + |
| 77 | +""" |
| 78 | + # find_pi_term_exponents(Matrix{Float64}, Number, Number) |
| 79 | +
|
| 80 | + Finds PI terms and returns the exponents and indices of repeating variables and |
| 81 | + non repeating index in the form of a dictionary |
| 82 | +""" |
| 83 | +function find_pi_term_exponents(dims_of_all_vars, total_vars, fundamental_dimensions) |
| 84 | + pi_terms_data = [] |
| 85 | + |
| 86 | + # We are considering the repeating variables as starting variables for now. |
| 87 | + repeating_idx = [] |
| 88 | + for i in 1:fundamental_dimensions |
| 89 | + push!(repeating_idx, i) |
| 90 | + end |
| 91 | + non_repeating_idx = [] # V1 |
| 92 | + for i in fundamental_dimensions+1:total_vars |
| 93 | + push!(non_repeating_idx, i) |
| 94 | + end |
| 95 | + |
| 96 | + for idx in non_repeating_idx |
| 97 | + # Form system of equations for exponents |
| 98 | + A = dims_of_all_vars[repeating_idx, 1:fundamental_dimensions]' # Transpose for solving |
| 99 | + b = -dims_of_all_vars[idx, 1:fundamental_dimensions] |
| 100 | + |
| 101 | + exponents = A \ b # Linear solve |
| 102 | + |
| 103 | + pi_term = Dict("var_idx" => idx, "exponents" => exponents, "repeating_idx" => repeating_idx) |
| 104 | + push!(pi_terms_data, pi_term) |
| 105 | + end |
| 106 | + |
| 107 | + return pi_terms_data |
| 108 | +end |
| 109 | + |
| 110 | + |
| 111 | +""" |
| 112 | + retrieve_pi_terms(Dict{Any}, Vector{Num}) |
| 113 | +
|
| 114 | + Gets the actual PI term provided non repeating index, repeating indices and the original variables |
| 115 | +""" |
| 116 | +function retrieve_pi_terms(pi_term, var_names) |
| 117 | + repeating_idx = pi_term["repeating_idx"] |
| 118 | + exp_arr = pi_term["exponents"] |
| 119 | + final_pi_term = 1 |
| 120 | + |
| 121 | + for (ind, val) in enumerate(exp_arr) |
| 122 | + exp_arr[ind] = round(val, digits=2) |
| 123 | + end |
| 124 | + |
| 125 | + for (ind, val) in enumerate(repeating_idx) |
| 126 | + final_pi_term *= var_names[val]^exp_arr[ind] |
| 127 | + end |
| 128 | + |
| 129 | + # multiplying with non repeating variable for the pi term |
| 130 | + final_pi_term *= var_names[pi_term["var_idx"]] |
| 131 | + |
| 132 | + return final_pi_term |
| 133 | +end |
| 134 | + |
| 135 | + |
| 136 | +""" |
| 137 | + buckinghumFun(Vector{DynamicQuantities.Quantity}, Vector{Num}) |
| 138 | +
|
| 139 | + Takes an array of DynamicQuantities.Quantity type and variable names separately. Gets the buckinghum |
| 140 | + PI terms and returns the array of PI terms |
| 141 | +""" |
| 142 | +function buckinghumFun(vars_quants, var_names) |
| 143 | + |
| 144 | + # vars_quants : [m^3/kg, s^-1, ms^-1] |
| 145 | + # var_names : [ρ, μ, u, v] |
| 146 | + |
| 147 | + # Number of variables |
| 148 | + total_vars = length(vars_quants) |
| 149 | + |
| 150 | + # Required for counting fundamental dimensions |
| 151 | + # says whicheever units are in picture. In this case : length, mass, time |
| 152 | + # [1, 1, 1, 0, 0, 0] |
| 153 | + dim_map = zeros(6) |
| 154 | + |
| 155 | + |
| 156 | + # [kg^-3* m, kg*s, s^-1] |
| 157 | + dims_vars = [] |
| 158 | + for u_arr in vars_quants |
| 159 | + push!(dims_vars, u_arr.dimensions) |
| 160 | + end |
| 161 | + |
| 162 | + dims_of_all_vars = get_dims_of_vars(dims_vars, total_vars, dim_map) |
| 163 | + |
| 164 | + fundamental_dimensions = no_of_fundamental_dims(dim_map) |
| 165 | + |
| 166 | + pi_terms = find_pi_term_exponents(dims_of_all_vars, total_vars, fundamental_dimensions) |
| 167 | + |
| 168 | + pis_arr = [] |
| 169 | + for val in pi_terms |
| 170 | + push!(pis_arr, retrieve_pi_terms(val, var_names)) |
| 171 | + end |
| 172 | + |
| 173 | + return pis_arr |
| 174 | +end |
0 commit comments