Skip to content

Commit

Permalink
Linting of Example files
Browse files Browse the repository at this point in the history
  • Loading branch information
thivinanandh committed Apr 25, 2024
1 parent 5d2903a commit 1f97ac0
Show file tree
Hide file tree
Showing 8 changed files with 505 additions and 278 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
import numpy as np
import tensorflow as tf


def inner_boundary(x, y):
"""
This function will return the boundary value for given component of a boundary
"""
return 0.0


def outer_boundary(x, y):
"""
This function will return the boundary value for given component of a boundary
"""


return 0.0


Expand All @@ -28,27 +29,30 @@ def rhs(x, y):
# f_temp =
return 50 * np.sin(x) + np.cos(x)


def exact_solution(x, y):
"""
This function will return the exact solution at a given point
"""
r = np.sqrt(x**2 + y**2)


return np.ones_like(x) * 0


def get_boundary_function_dict():
"""
This function will return a dictionary of boundary functions
"""
return {1000: outer_boundary, 1001: inner_boundary}


def get_bound_cond_dict():
"""
This function will return a dictionary of boundary conditions
"""
return {1000: "dirichlet", 1001: "dirichlet"}


def get_bilinear_params_dict():
"""
This function will return a dictionary of bilinear parameters
Expand All @@ -60,10 +64,11 @@ def get_bilinear_params_dict():

return {"eps": eps, "b_x": b_x, "b_y": b_y, "c": c}

def get_inverse_params_actual_dict(x,y):

def get_inverse_params_actual_dict(x, y):
"""
This function will return a dictionary of inverse parameters
"""
eps = np.cos(x) + np.cos(y)

return {"eps": eps}
return {"eps": eps}
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
y_exact = exact_solution(test_points[:, 0], test_points[:, 1])
else:
exact_db = pd.read_csv(f"{i_exact_solution_file_name}", header=None, delimiter=",")
y_exact = exact_db.iloc[:,2].values.reshape(-1,)
y_exact = exact_db.iloc[:, 2].values.reshape(-1)

# plot the exact solution
num_epochs = i_epochs # num_epochs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
bilinear_params_dict = datahandler.get_bilinear_params_dict_as_tensors(get_bilinear_params_dict)

# Setup the hard constraints
@tf.function
@tf.function
def apply_hard_boundary_constraints(inputs, x):
"""This method applies hard boundary constraints to the model.
:param inputs: Input tensor
Expand All @@ -163,9 +163,14 @@ def apply_hard_boundary_constraints(inputs, x):
:return: Output tensor with hard boundary constraints
:rtype: tf.Tensor
"""
ansatz = tf.tanh(4.0*np.pi*inputs[:,0:1])*tf.tanh(4.0*np.pi*inputs[:,1:2])*tf.tanh(4.0*np.pi*(inputs[:,0:1]-1.0))*tf.tanh(4.0*np.pi*(inputs[:,1:2]-1.0))
ansatz = (
tf.tanh(4.0 * np.pi * inputs[:, 0:1])
* tf.tanh(4.0 * np.pi * inputs[:, 1:2])
* tf.tanh(4.0 * np.pi * (inputs[:, 0:1] - 1.0))
* tf.tanh(4.0 * np.pi * (inputs[:, 1:2] - 1.0))
)
ansatz = tf.cast(ansatz, i_dtype)
return ansatz*x
return ansatz * x

model = DenseModel_Hard(
layer_dims=[2, 30, 30, 30, 1],
Expand All @@ -183,7 +188,7 @@ def apply_hard_boundary_constraints(inputs, x):
use_attention=i_use_attention,
activation=i_activation,
hessian=False,
hard_constraint_function = apply_hard_boundary_constraints,
hard_constraint_function=apply_hard_boundary_constraints,
)

test_points = domain.get_test_points()
Expand Down
17 changes: 11 additions & 6 deletions examples/inverse_problems_2d/cd2d/cd2d_inverse_circle_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,31 @@ def left_boundary(x, y):
val = np.ones_like(x) * 0.0
return val


def right_boundary(x, y):
"""
This function will return the boundary value for given component of a boundary
"""
val = np.ones_like(x) * 0.0
return val


def top_boundary(x, y):
"""
This function will return the boundary value for given component of a boundary
"""
val = np.ones_like(x) * 0.0
return val


def bottom_boundary(x, y):
"""
This function will return the boundary value for given component of a boundary
"""
val = np.ones_like(x) * 0.0
return val


def rhs(x, y):
"""
This function will return the value of the rhs at a given point
Expand All @@ -42,49 +46,50 @@ def rhs(x, y):
# return -y * np.sin(x * y) * np.tanh(8 * x * y) + 8 * y * np.cos(x * y) / np.cosh(8 * x * y)**2 + 10 * (x**2 + y**2) * \
# (16 * np.sin(x * y) / np.cosh(8 * x * y)**2 + np.cos(x * y) * np.tanh(8 * x * y) + 128 * np.cos(x * y) * np.tanh(8 * x * y) / np.cosh(8 * x * y)**2) * np.sin(x) * np.cos(y)


return 10.0 * np.ones_like(x)


def exact_solution(x, y):
"""
This function will return the exact solution at a given point
"""

val = np.ones_like(x) * 0.0

return val


def get_boundary_function_dict():
"""
This function will return a dictionary of boundary functions
"""
return {1000: bottom_boundary, 1001: right_boundary, 1002: top_boundary, 1003: left_boundary}


def get_bound_cond_dict():
"""
This function will return a dictionary of boundary conditions
"""
return {1000: "dirichlet", 1001: "dirichlet", 1002: "dirichlet", 1003: "dirichlet"}


def get_bilinear_params_dict():
"""
This function will return a dictionary of bilinear parameters
"""
eps = 0.1 # will not be used in the loss function, as it will be replaced by the predicted value of NN

eps = 0.1 # will not be used in the loss function, as it will be replaced by the predicted value of NN
b1 = 1
b2 = 0
c = 0.0

return {"eps": eps, "b_x": b1, "b_y": b2, "c": c}



def get_inverse_params_actual_dict(x, y):
"""
This function will return a dictionary of inverse parameters
"""
# Initial Guess
eps = 0.5 * (np.sin(x) + np.cos(y))
return {"eps": eps}
return {"eps": eps}
Original file line number Diff line number Diff line change
Expand Up @@ -8,74 +8,86 @@

EPS = 0.3


def left_boundary(x, y):
"""
This function will return the boundary value for given component of a boundary
"""
val = np.sin(x) * np.tanh(x) * np.exp(-1.0*EPS *(x**2)) * 10
val = np.sin(x) * np.tanh(x) * np.exp(-1.0 * EPS * (x**2)) * 10
return val


def right_boundary(x, y):
"""
This function will return the boundary value for given component of a boundary
"""
val = np.sin(x) * np.tanh(x) * np.exp(-1.0*EPS *(x**2)) * 10
val = np.sin(x) * np.tanh(x) * np.exp(-1.0 * EPS * (x**2)) * 10
return val


def top_boundary(x, y):
"""
This function will return the boundary value for given component of a boundary
"""
val = np.sin(x) * np.tanh(x) * np.exp(-1.0*EPS *(x**2)) * 10
val = np.sin(x) * np.tanh(x) * np.exp(-1.0 * EPS * (x**2)) * 10
return val


def bottom_boundary(x, y):
"""
This function will return the boundary value for given component of a boundary
"""
val = np.sin(x) * np.tanh(x) * np.exp(-1.0*EPS *(x**2)) * 10
val = np.sin(x) * np.tanh(x) * np.exp(-1.0 * EPS * (x**2)) * 10
return val


def rhs(x, y):
"""
This function will return the value of the rhs at a given point
"""

X =x
Y =y
X = x
Y = y
eps = EPS

return -EPS * (
40.0 * X * eps * (np.tanh(X)**2 - 1) * np.sin(X)
- 40.0 * X * eps * np.cos(X) * np.tanh(X)
+ 10 * eps * (4.0 * X**2 * eps - 2.0) * np.sin(X) * np.tanh(X)
+ 20 * (np.tanh(X)**2 - 1) * np.sin(X) * np.tanh(X)
- 20 * (np.tanh(X)**2 - 1) * np.cos(X)
- 10 * np.sin(X) * np.tanh(X)
) * np.exp(-1.0 * X**2 * eps)
return (
-EPS
* (
40.0 * X * eps * (np.tanh(X) ** 2 - 1) * np.sin(X)
- 40.0 * X * eps * np.cos(X) * np.tanh(X)
+ 10 * eps * (4.0 * X**2 * eps - 2.0) * np.sin(X) * np.tanh(X)
+ 20 * (np.tanh(X) ** 2 - 1) * np.sin(X) * np.tanh(X)
- 20 * (np.tanh(X) ** 2 - 1) * np.cos(X)
- 10 * np.sin(X) * np.tanh(X)
)
* np.exp(-1.0 * X**2 * eps)
)


def exact_solution(x, y):
"""
This function will return the exact solution at a given point
"""
val = np.sin(x) * np.tanh(x) * np.exp(-1.0*EPS *(x**2)) * 10

val = np.sin(x) * np.tanh(x) * np.exp(-1.0 * EPS * (x**2)) * 10

return val


def get_boundary_function_dict():
"""
This function will return a dictionary of boundary functions
"""
return {1000: bottom_boundary, 1001: right_boundary, 1002: top_boundary, 1003: left_boundary}


def get_bound_cond_dict():
"""
This function will return a dictionary of boundary conditions
"""
return {1000: "dirichlet", 1001: "dirichlet", 1002: "dirichlet", 1003: "dirichlet"}


def get_bilinear_params_dict():
"""
This function will return a dictionary of bilinear parameters
Expand Down Expand Up @@ -103,4 +115,4 @@ def get_inverse_params_actual_dict():
# Initial Guess
eps = EPS

return {"eps": eps}
return {"eps": eps}
Loading

0 comments on commit 1f97ac0

Please sign in to comment.