Skip to content

Commit

Permalink
CI Changes & test file linting
Browse files Browse the repository at this point in the history
Changed from local pip install to pip install from requirements
changed name of integration tests

Changed linting of test files.
  • Loading branch information
thivinanandh committed Apr 24, 2024
1 parent 5a9367c commit 8ec72d8
Show file tree
Hide file tree
Showing 11 changed files with 518 additions and 404 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Unit tests
name: Integration tests

on:
push:
Expand Down Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Install dependencies
run: |
python3 -m pip install --upgrade pip
python3 -m pip install -e .
pip install -r requirements.txt
- name: Run unit tests
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Install dependencies
run: |
python3 -m pip install --upgrade pip
python3 -m pip install -e .
pip install -r requirements.txt
- name: Run unit tests
run: |
Expand Down
158 changes: 91 additions & 67 deletions tests/integration/test_accuracy_cd2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

import numpy as np
import pytest
import yaml
import sys
import copy
import time
from pathlib import Path
import tensorflow as tf

Expand All @@ -16,9 +12,8 @@
from fastvpinns.data.datahandler2d import DataHandler2D
from fastvpinns.model.model import DenseModel
from fastvpinns.physics.cd2d import pde_loss_cd2d
from fastvpinns.utils.plot_utils import plot_contour, plot_loss_function, plot_test_loss_function
from fastvpinns.utils.compute_utils import compute_errors_combined
from fastvpinns.utils.print_utils import print_table


@pytest.fixture
def cd2d_test_data_internal():
Expand Down Expand Up @@ -47,18 +42,24 @@ def cd2d_test_data_internal():
bilinear_params = lambda: {"eps": 1.0, "b_x": 0.2, "b_y": -0.1, "c": 0.0}

def rhs(x, y):
result = 0.2*np.sin(2*np.pi*y)*np.sinh(2*np.pi*x)
result += 4.0*np.pi*np.cos(2*np.pi*y)*np.sinh(2*np.pi*x)
result += 4.0*np.pi*np.cos(2*np.pi*y)*np.tanh(np.pi*x)
result += 0.4*np.cos(2*np.pi*y)
result = (np.pi*result)/(np.cosh(2*np.pi*x) + 1)
result = 0.2 * np.sin(2 * np.pi * y) * np.sinh(2 * np.pi * x)
result += 4.0 * np.pi * np.cos(2 * np.pi * y) * np.sinh(2 * np.pi * x)
result += 4.0 * np.pi * np.cos(2 * np.pi * y) * np.tanh(np.pi * x)
result += 0.4 * np.cos(2 * np.pi * y)
result = (np.pi * result) / (np.cosh(2 * np.pi * x) + 1)
return result

forcing_function = rhs

exact_solution = lambda x, y: np.tanh(np.pi * x) * np.cos(2 * np.pi * y)

return boundary_functions, boundary_conditions, bilinear_params, forcing_function, exact_solution
return (
boundary_functions,
boundary_conditions,
bilinear_params,
forcing_function,
exact_solution,
)


def cd2d_learning_rate_static_data():
Expand All @@ -77,9 +78,10 @@ def cd2d_learning_rate_static_data():
learning_rate_dict["decay_steps"] = decay_steps
learning_rate_dict["decay_rate"] = decay_rate
learning_rate_dict["staircase"] = staircase

return learning_rate_dict


@pytest.fixture
def cd2d_test_data_circle():
"""
Expand All @@ -88,32 +90,35 @@ def cd2d_test_data_circle():
Tuple: boundary_functions, boundary_conditions, bilinear_params, forcing_function, exact_solution
"""
omega = 4.0 * np.pi

circle_boundary = lambda x, y: np.tanh(np.pi * x) * np.cos(2 * np.pi * y)

boundary_functions = {
1000: circle_boundary,
}
boundary_functions = {1000: circle_boundary}

boundary_conditions = {
1000: "dirichlet",
}
boundary_conditions = {1000: "dirichlet"}

bilinear_params = lambda: {"eps": 1.0, "b_x": 0.2, "b_y": -0.1, "c": 0.0}

def rhs(x, y):
result = 0.2*np.sin(2*np.pi*y)*np.sinh(2*np.pi*x)
result += 4.0*np.pi*np.cos(2*np.pi*y)*np.sinh(2*np.pi*x)
result += 4.0*np.pi*np.cos(2*np.pi*y)*np.tanh(np.pi*x)
result += 0.4*np.cos(2*np.pi*y)
result = (np.pi*result)/(np.cosh(2*np.pi*x) + 1)
result = 0.2 * np.sin(2 * np.pi * y) * np.sinh(2 * np.pi * x)
result += 4.0 * np.pi * np.cos(2 * np.pi * y) * np.sinh(2 * np.pi * x)
result += 4.0 * np.pi * np.cos(2 * np.pi * y) * np.tanh(np.pi * x)
result += 0.4 * np.cos(2 * np.pi * y)
result = (np.pi * result) / (np.cosh(2 * np.pi * x) + 1)
return result

forcing_function = rhs

exact_solution = lambda x, y: np.tanh(np.pi * x) * np.cos(2 * np.pi * y)

return boundary_functions, boundary_conditions, bilinear_params, forcing_function, exact_solution
return (
boundary_functions,
boundary_conditions,
bilinear_params,
forcing_function,
exact_solution,
)


def test_cd2d_accuracy_internal(cd2d_test_data_internal):
"""
Expand All @@ -123,11 +128,13 @@ def test_cd2d_accuracy_internal(cd2d_test_data_internal):
"""

# obtain the test data
bound_function_dict, bound_condition_dict, bilinear_params, rhs, exact_solution = cd2d_test_data_internal
bound_function_dict, bound_condition_dict, bilinear_params, rhs, exact_solution = (
cd2d_test_data_internal
)

output_folder = "tests/test_dump"

#use pathlib to create the directory
# use pathlib to create the directory
Path(output_folder).mkdir(parents=True, exist_ok=True)

# generate a internal mesh
Expand Down Expand Up @@ -167,31 +174,38 @@ def test_cd2d_accuracy_internal(cd2d_test_data_internal):
# get the learning rate dictionary
lr_dict = cd2d_learning_rate_static_data()

# generate a model
model = DenseModel(layer_dims=[2,30,30,30,1],
learning_rate_dict=lr_dict,
params_dict=params_dict,
loss_function=pde_loss_cd2d,
input_tensors_list=[datahandler.x_pde_list, train_dirichlet_input, train_dirichlet_output],
orig_factor_matrices = [datahandler.shape_val_mat_list , datahandler.grad_x_mat_list, datahandler.grad_y_mat_list],
force_function_list=datahandler.forcing_function_list,
tensor_dtype = tf.float32,
use_attention=False,
hessian=False)
# generate a model
model = DenseModel(
layer_dims=[2, 30, 30, 30, 1],
learning_rate_dict=lr_dict,
params_dict=params_dict,
loss_function=pde_loss_cd2d,
input_tensors_list=[datahandler.x_pde_list, train_dirichlet_input, train_dirichlet_output],
orig_factor_matrices=[
datahandler.shape_val_mat_list,
datahandler.grad_x_mat_list,
datahandler.grad_y_mat_list,
],
force_function_list=datahandler.forcing_function_list,
tensor_dtype=tf.float32,
use_attention=False,
hessian=False,
)

test_points = domain.get_test_points()
y_exact = exact_solution(test_points[:,0], test_points[:,1])
y_exact = exact_solution(test_points[:, 0], test_points[:, 1])

# train the model
for epoch in range(6000):
model.train_step(beta=10, bilinear_params_dict=bilinear_params_dict)
# check the l2 error l1 error of the model

# check the l2 error l1 error of the model
y_pred = model(test_points).numpy()
y_pred = y_pred.reshape(-1,)
y_pred = y_pred.reshape(-1)

l2_error, linf_error, l2_error_relative, linf_error_relative, \
l1_error, l1_error_relative = compute_errors_combined(y_exact, y_pred)
l2_error, linf_error, l2_error_relative, linf_error_relative, l1_error, l1_error_relative = (
compute_errors_combined(y_exact, y_pred)
)

print(f"l2_error = {l2_error}, l1_error = {l1_error}")
assert l2_error < 6e-2 and l1_error < 6e-2
Expand All @@ -205,7 +219,9 @@ def test_cd2d_accuracy_external(cd2d_test_data_circle):
"""

# Obtain the test data
bound_function_dict, bound_condition_dict, bilinear_params, rhs, exact_solution = cd2d_test_data_circle
bound_function_dict, bound_condition_dict, bilinear_params, rhs, exact_solution = (
cd2d_test_data_circle
)

output_folder = "tests/test_dump"

Expand All @@ -214,8 +230,9 @@ def test_cd2d_accuracy_external(cd2d_test_data_circle):

# Generate an internal mesh
domain = Geometry_2D("quadrilateral", "external", 100, 100, output_folder)
cells, boundary_points = domain.read_mesh("tests/support_files/circle_quad.mesh",
2, "uniform", refinement_level=1)
cells, boundary_points = domain.read_mesh(
"tests/support_files/circle_quad.mesh", 2, "uniform", refinement_level=1
)

# Create the fespace
fespace = Fespace2D(
Expand Down Expand Up @@ -249,30 +266,37 @@ def test_cd2d_accuracy_external(cd2d_test_data_circle):
# Get the learning rate dictionary
lr_dict = cd2d_learning_rate_static_data()

# Generate a model
model = DenseModel(layer_dims=[2,50,50,50,1],
learning_rate_dict=lr_dict,
params_dict=params_dict,
loss_function=pde_loss_cd2d,
input_tensors_list=[datahandler.x_pde_list, train_dirichlet_input, train_dirichlet_output],
orig_factor_matrices=[datahandler.shape_val_mat_list, datahandler.grad_x_mat_list, datahandler.grad_y_mat_list],
force_function_list=datahandler.forcing_function_list,
tensor_dtype=tf.float32,
use_attention=False,
hessian=False)
# Generate a model
model = DenseModel(
layer_dims=[2, 50, 50, 50, 1],
learning_rate_dict=lr_dict,
params_dict=params_dict,
loss_function=pde_loss_cd2d,
input_tensors_list=[datahandler.x_pde_list, train_dirichlet_input, train_dirichlet_output],
orig_factor_matrices=[
datahandler.shape_val_mat_list,
datahandler.grad_x_mat_list,
datahandler.grad_y_mat_list,
],
force_function_list=datahandler.forcing_function_list,
tensor_dtype=tf.float32,
use_attention=False,
hessian=False,
)

test_points = domain.get_test_points()
y_exact = exact_solution(test_points[:,0], test_points[:,1])
y_exact = exact_solution(test_points[:, 0], test_points[:, 1])

# Train the model
for epoch in range(6000):
model.train_step(beta=10, bilinear_params_dict=bilinear_params_dict)
# Check the L2 error, L1 error of the model

# Check the L2 error, L1 error of the model
y_pred = model(test_points).numpy()
y_pred = y_pred.reshape(-1,)
y_pred = y_pred.reshape(-1)

l2_error, linf_error, l2_error_relative, linf_error_relative, \
l1_error, l1_error_relative = compute_errors_combined(y_exact, y_pred)
l2_error, linf_error, l2_error_relative, linf_error_relative, l1_error, l1_error_relative = (
compute_errors_combined(y_exact, y_pred)
)

assert l2_error < 6e-2 and l1_error < 6e-2
Loading

0 comments on commit 8ec72d8

Please sign in to comment.