Skip to content

Commit

Permalink
Merge pull request #26 from NREL/float32
Browse files Browse the repository at this point in the history
fix float 32 json dump and format
  • Loading branch information
malihass authored Apr 6, 2024
2 parents cf22239 + 4c27d48 commit 1a259a8
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 28 deletions.
17 changes: 14 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ['3.10']
python-version: ['3.11']
defaults:
run:
working-directory: ${{github.workspace}}
Expand All @@ -45,7 +45,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ['3.10']
python-version: ['3.11']
defaults:
run:
working-directory: ${{github.workspace}}
Expand All @@ -65,7 +65,7 @@ jobs:
python main.py -nosimp -opt -lean -p 0.5 1
cd ../preProcess
python makeDataset_spm.py -nosimp -df ../integration_spm -frt 1 -p 0.5 1
- name: Test SPM
- name: Test SPM training
run: |
cd pinn_spm_param/tests
pytest -v test_spm.py
Expand All @@ -82,3 +82,14 @@ jobs:
run: |
cd BayesianCalibration_spm
bash exec_test.sh
- name: Test SPM train and post process float 32
run: |
cd pinn_spm_param
bash convert_to_float32.sh
cd integration_spm
python main.py -nosimp -opt -lean -p 0.5 1
cd ../preProcess
python makeDataset_spm.py -nosimp -df ../integration_spm -frt 1 -p 0.5 1
cd ../tests
pytest -v test_spm.py::test_sgd_lbfgs
pytest -v test_spm_postProc.py
48 changes: 27 additions & 21 deletions pinn_spm_param/integration_spm/spm_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ def make_sim_config(t_dom, r_dom):


def init_arrays(n_t, n_r):
phie = np.zeros(n_t)
phis_c = np.zeros(n_t)
cs_a = np.zeros((n_t, n_r))
cs_c = np.zeros((n_t, n_r))
Ds_a = np.zeros(n_r)
Ds_c = np.zeros(n_r)
rhs_a = np.zeros(n_r)
rhs_c = np.zeros(n_r)
A = np.zeros((n_r, n_r))
B_a = np.zeros(n_r)
B_c = np.zeros(n_r)
phie = np.zeros(n_t, dtype=np.float64)
phis_c = np.zeros(n_t, dtype=np.float64)
cs_a = np.zeros((n_t, n_r), dtype=np.float64)
cs_c = np.zeros((n_t, n_r), dtype=np.float64)
Ds_a = np.zeros(n_r, dtype=np.float64)
Ds_c = np.zeros(n_r, dtype=np.float64)
rhs_a = np.zeros(n_r, dtype=np.float64)
rhs_c = np.zeros(n_r, dtype=np.float64)
A = np.zeros((n_r, n_r), dtype=np.float64)
B_a = np.zeros(n_r, dtype=np.float64)
B_c = np.zeros(n_r, dtype=np.float64)

return {
"ce": 0,
Expand Down Expand Up @@ -98,7 +98,7 @@ def tridiag(ds, dt, dr):


def rhs(dt, r, ddr_cs, ds, ddDs_cs, cs, bound_grad):
rhs_col = np.zeros(len(r))
rhs_col = np.zeros(len(r), dtype=np.float64)
rhs_col = (
dt
* (np.float64(2.0) / np.clip(r, a_min=1e-12, a_max=None))
Expand Down Expand Up @@ -259,7 +259,7 @@ def integration(
grad_ds_a_cs_a(params["T"], params["R"])
)
else:
gradDs_a_cs_a = np.zeros(len(sol["Ds_a"]))
gradDs_a_cs_a = np.zeros(len(sol["Ds_a"]), dtype=np.float64)

ddr_csa = np.gradient(
sol["cs_a"][i_t - 1, :], r_a, axis=0, edge_order=2
Expand Down Expand Up @@ -289,7 +289,8 @@ def integration(
params["T"],
params["R"],
params["cscamax"],
deg_ds_c * np.ones(sol["cs_c"][i_t - 1, :].shape),
deg_ds_c
* np.ones(sol["cs_c"][i_t - 1, :].shape, dtype=np.float64),
)
if EXACT_GRAD_DS_CS:
gradDs_c_cs_c = grad_ds_c_cs_c(
Expand All @@ -302,26 +303,30 @@ def integration(
Ds_c_tmp1 = params["D_s_c"](
np.clip(
sol["cs_c"][i_t - 1, :]
+ np.ones(sol["cs_c"].shape[1]) * GRAD_STEP,
+ np.ones(sol["cs_c"].shape[1], dtype=np.float64)
* GRAD_STEP,
a_min=0,
a_max=params["cscamax"],
),
params["T"],
params["R"],
params["cscamax"],
deg_ds_c * np.ones(sol["cs_c"][i_t - 1, :].shape),
deg_ds_c
* np.ones(sol["cs_c"][i_t - 1, :].shape, dtype=np.float64),
)
Ds_c_tmp2 = params["D_s_c"](
np.clip(
sol["cs_c"][i_t - 1, :]
- np.ones(sol["cs_c"].shape[1]) * GRAD_STEP,
- np.ones(sol["cs_c"].shape[1], dtype=np.float64)
* GRAD_STEP,
a_min=0,
a_max=params["cscamax"],
),
params["T"],
params["R"],
params["cscamax"],
deg_ds_c * np.ones(sol["cs_c"][i_t - 1, :].shape),
deg_ds_c
* np.ones(sol["cs_c"][i_t - 1, :].shape, dtype=np.float64),
)
gradDs_c_cs_c = (
(Ds_c_tmp1 - Ds_c_tmp2) / (2 * GRAD_STEP)
Expand Down Expand Up @@ -357,7 +362,7 @@ def integration(
)
ddr_csa[0] = 0
ddr_csa[-1] = -sol["j_a"] / sol["Ds_a"][-1]
ddr2_csa = np.zeros(n_r)
ddr2_csa = np.zeros(n_r, dtype=np.float64)
ddr2_csa[1 : n_r - 1] = (
sol["cs_a"][i_t - 1, : n_r - 2]
- 2 * sol["cs_a"][i_t - 1, 1 : n_r - 1]
Expand Down Expand Up @@ -392,14 +397,15 @@ def integration(
params["T"],
params["R"],
params["cscamax"],
deg_ds_c * np.ones(sol["cs_c"][i_t - 1, :].shape),
deg_ds_c
* np.ones(sol["cs_c"][i_t - 1, :].shape, dtype=np.float64),
)
ddr_csc = np.gradient(
sol["cs_c"][i_t - 1, :], r_c, axis=0, edge_order=2
)
ddr_csc[0] = 0
ddr_csc[-1] = -sol["j_c"] / sol["Ds_c"][-1]
ddr2_csc = np.zeros(n_r)
ddr2_csc = np.zeros(n_r, dtype=np.float64)
ddr2_csc[1 : n_r - 1] = (
sol["cs_c"][i_t - 1, : n_r - 2]
- 2 * sol["cs_c"][i_t - 1, 1 : n_r - 1]
Expand Down
9 changes: 5 additions & 4 deletions pinn_spm_param/util/forwardPass.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def var_from_x(x_arr, dummyVal=np.float64(0.0)):
if x_arr.shape[1] == 2:
var_full[1] = var_simp[1]
else:
var_full[1] = dummyVal * np.ones(x_arr.shape[0])
var_full[1] = dummyVal * np.ones(x_arr.shape[0], dtype=np.float64)
return var_simp, var_full


Expand All @@ -62,7 +62,7 @@ def rescale_var_list(nn, var_list, dummyVal=np.float64(0.0)):
else:
var_list_resc_full[nn.ind_r] = (
dummyVal
* np.ones(var_list_resc_full[nn.ind_t].shape)
* np.ones(var_list_resc_full[nn.ind_t].shape, dtype=np.float64)
/ nn.params["rescale_R"]
)
return var_list_resc, var_list_resc_full
Expand Down Expand Up @@ -276,11 +276,12 @@ def pinn_pred_struct(nn, params_list):
(n_t * n_r * n_par * n_par, 1),
).astype("float64")
params_r_unr = [
np.ones((n_t * n_r, 1)) * entry
np.ones((n_t * n_r, 1), dtype=np.float64) * entry
for ientry, entry in enumerate(params_list)
]
params_r = [
np.ones((n_t * n_r, 1)) * nn.rescale_param(entry, ientry)
np.ones((n_t * n_r, 1), dtype=np.float64)
* nn.rescale_param(entry, ientry)
for ientry, entry in enumerate(params_list)
]

Expand Down
11 changes: 11 additions & 0 deletions pinn_spm_param/util/myNN.py
Original file line number Diff line number Diff line change
Expand Up @@ -3199,6 +3199,17 @@ def prepareLog(self):
with open(
os.path.join(self.modelFolder, "config.json"), "w+"
) as outfile:
# float 32 is not supported by Json
for key in self.config:
ent_type = str(type(self.config[key]))
if "numpy.float" in ent_type and "32" in ent_type:
self.config[key] = float(self.config[key])
elif key == "params_min" or key == "params_max":
for ientry, entry in enumerate(self.config[key]):
ent_type = str(type(self.config[key][ientry]))
if "numpy.float" in ent_type and "32" in ent_type:
self.config[key][ientry] = float(entry)

json.dump(self.config, outfile, indent=4, sort_keys=True)

# Make log headers
Expand Down

0 comments on commit 1a259a8

Please sign in to comment.