Skip to content

Commit

Permalink
Merge branch 'main' of github.com:IBM/simulai
Browse files Browse the repository at this point in the history
  • Loading branch information
Joao-L-S-Almeida committed Apr 14, 2023
2 parents bea3f0f + e9b435d commit f89a813
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
SimulAI
=======

.. image:: https://zenodo.org/badge/561364034.svg
:target: https://zenodo.org/badge/latestdoi/561364034
.. image:: https://badge.fury.io/py/simulai-toolkit.svg
Expand Down
23 changes: 12 additions & 11 deletions examples/DeepONet/Flame/eval_flame.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,38 @@

save_path = args.save_path

initial_state_test = np.array([0.6, 8e-5, 0.3])
n_outputs = 3
n_times = 500
initial_state_test = np.array([1e-3])
n_outputs = 1
Delta_t = 0.05
n_times = int(2/(initial_state_test[0]*Delta_t))
Q = 1000

# Testing to reload from disk
saver = SPFile(compact=False)
rober_net = saver.read(model_path=save_path)
flame_net = saver.read(model_path=save_path)

branch_input_test = np.tile(initial_state_test[None, :], (Q, 1))
trunk_input_test = np.linspace(0, 1, Q)[:, None]
trunk_input_test = np.linspace(0, Delta_t, Q)[:, None]

eval_list = list()

for i in range(0, n_times):
for i in range(0, 2):
branch_input_test = np.tile(initial_state_test[None, :], (Q, 1))

approximated_data = rober_net.eval(
approximated_data = flame_net.eval(
trunk_data=trunk_input_test, branch_data=branch_input_test
)
initial_state_test = approximated_data[-1]

eval_list.append(approximated_data[0])
eval_list.append(approximated_data)

evaluation = np.vstack(eval_list) * np.array([1, 1e4, 1])
evaluation = np.vstack(eval_list)
time = np.linspace(0, n_times, evaluation.shape[0])

np.save("evaluation.npy", evaluation)
plt.plot(time, evaluation, label="Approximated")
plt.xlabel("t (s)")
plt.savefig("rober_approximation.png")
plt.savefig("flame_approximation.png")
plt.close()

plt.figure(figsize=(15, 6))
Expand All @@ -69,4 +70,4 @@
plt.yticks(np.linspace(0, 1, 5))
plt.legend()
plt.grid(True)
plt.savefig(f"rober_approximation_custom.png")
plt.savefig(f"flame_approximation_custom.png")
26 changes: 9 additions & 17 deletions examples/DeepONet/Flame/timeint_DeepONet_flame_pinn.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@

Q = 1_000
N = int(5e4)
Delta_t = 1

initial_state_test = np.array([1e-3])

t_intv = [0, 1]
t_intv = [0, Delta_t]
u_intv = np.stack([[0], [1]], axis=0)

# The expression we aim at minimizing
Expand All @@ -51,9 +50,6 @@
branch_input_train = np.tile(U_u[:, None, :], (1, Q, 1)).reshape(N * Q, -1)
trunk_input_train = np.tile(U_t[:, None], (N, 1))

branch_input_test = np.tile(initial_state_test[None, :], (Q, 1))
trunk_input_test = np.sort(U_t[:, None], axis=0)

initial_states = U_u

input_labels = ["t"]
Expand All @@ -64,7 +60,7 @@

lambda_1 = 0.0 # Penalty for the L¹ regularization (Lasso)
lambda_2 = 0.0 # Penalty factor for the L² regularization
n_epochs = 20_000 # Maximum number of iterations for ADAM
n_epochs = 200_000 # Maximum number of iterations for ADAM
lr = 1e-3 # Initial learning rate for the ADAM algorithm

def model():
Expand Down Expand Up @@ -195,20 +191,16 @@ def model():

initial_state_test = np.array([1e-3])
n_outputs = 1
n_times = 2
n_times = int(2/(initial_state_test[0]*Delta_t))
Q = 1000

# Testing to reload from disk
saver = SPFile(compact=False)
flame_net = saver.read(model_path=save_path)

branch_input_test = np.tile(initial_state_test[None, :], (Q, 1))
trunk_input_test = np.linspace(0, 1, Q)[:, None]
trunk_input_test = np.linspace(0, Delta_t, Q)[:, None]

eval_list = list()

for i in range(0, n_times):
branch_in:put_test = np.tile(initial_state_test[None, :], (Q, 1))
branch_input_test = np.tile(initial_state_test[None, :], (Q, 1))

approximated_data = flame_net.eval(
trunk_data=trunk_input_test, branch_data=branch_input_test
Expand All @@ -217,8 +209,8 @@ def model():

eval_list.append(approximated_data[0])

evaluation = np.vstack(eval_list) * np.array([1, 1e4, 1])
time = np.linspace(0, n_times, evaluation.shape[0])
evaluation = np.vstack(eval_list)
time = np.linspace(0, n_times*Delta_t, evaluation.shape[0])

np.save("evaluation.npy", evaluation)
plt.plot(time, evaluation, label="Approximated")
Expand All @@ -229,7 +221,7 @@ def model():
plt.figure(figsize=(15, 6))

for i in range(n_outputs):
plt.plot(time, evaluation[:, i], label=f"s_{i+1}")
plt.plot(time, evaluation[:, i], label=f"u")
plt.xlabel("t (s)")

plt.yticks(np.linspace(0, 1, 5))
Expand Down

0 comments on commit f89a813

Please sign in to comment.