|
| 1 | +import pytest |
| 2 | +import math |
| 3 | +from pina.solver import PINN |
| 4 | +from pina.loss import ScalarWeighting |
| 5 | +from pina.trainer import Trainer |
| 6 | +from pina.model import FeedForward |
| 7 | +from pina.problem.zoo import Poisson2DSquareProblem as Poisson |
| 8 | +from pina.callback import LinearWeightUpdate |
| 9 | + |
| 10 | + |
| 11 | +# Define the problem |
| 12 | +poisson_problem = Poisson() |
| 13 | +poisson_problem.discretise_domain(50, "grid") |
| 14 | +cond_name = list(poisson_problem.conditions.keys())[0] |
| 15 | + |
| 16 | +# Define the model |
| 17 | +model = FeedForward( |
| 18 | + input_dimensions=len(poisson_problem.input_variables), |
| 19 | + output_dimensions=len(poisson_problem.output_variables), |
| 20 | + layers=[32, 32], |
| 21 | +) |
| 22 | + |
| 23 | +# Define the weighting schema |
| 24 | +weights_dict = {key: 1 for key in poisson_problem.conditions.keys()} |
| 25 | +weighting = ScalarWeighting(weights=weights_dict) |
| 26 | + |
| 27 | +# Define the solver |
| 28 | +solver = PINN(problem=poisson_problem, model=model, weighting=weighting) |
| 29 | + |
| 30 | +# Value used for testing |
| 31 | +epochs = 10 |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.parametrize("initial_value", [1, 5.5]) |
| 35 | +@pytest.mark.parametrize("target_value", [10, 25.5]) |
| 36 | +def test_constructor(initial_value, target_value): |
| 37 | + LinearWeightUpdate( |
| 38 | + target_epoch=epochs, |
| 39 | + condition_name=cond_name, |
| 40 | + initial_value=initial_value, |
| 41 | + target_value=target_value, |
| 42 | + ) |
| 43 | + |
| 44 | + # Target_epoch must be int |
| 45 | + with pytest.raises(ValueError): |
| 46 | + LinearWeightUpdate( |
| 47 | + target_epoch=10.0, |
| 48 | + condition_name=cond_name, |
| 49 | + initial_value=0, |
| 50 | + target_value=1, |
| 51 | + ) |
| 52 | + |
| 53 | + # Condition_name must be str |
| 54 | + with pytest.raises(ValueError): |
| 55 | + LinearWeightUpdate( |
| 56 | + target_epoch=epochs, |
| 57 | + condition_name=100, |
| 58 | + initial_value=0, |
| 59 | + target_value=1, |
| 60 | + ) |
| 61 | + |
| 62 | + # Initial_value must be float or int |
| 63 | + with pytest.raises(ValueError): |
| 64 | + LinearWeightUpdate( |
| 65 | + target_epoch=epochs, |
| 66 | + condition_name=cond_name, |
| 67 | + initial_value="0", |
| 68 | + target_value=1, |
| 69 | + ) |
| 70 | + |
| 71 | + # Target_value must be float or int |
| 72 | + with pytest.raises(ValueError): |
| 73 | + LinearWeightUpdate( |
| 74 | + target_epoch=epochs, |
| 75 | + condition_name=cond_name, |
| 76 | + initial_value=0, |
| 77 | + target_value="1", |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.parametrize("initial_value, target_value", [(1, 10), (10, 1)]) |
| 82 | +def test_training(initial_value, target_value): |
| 83 | + callback = LinearWeightUpdate( |
| 84 | + target_epoch=epochs, |
| 85 | + condition_name=cond_name, |
| 86 | + initial_value=initial_value, |
| 87 | + target_value=target_value, |
| 88 | + ) |
| 89 | + trainer = Trainer( |
| 90 | + solver=solver, |
| 91 | + callbacks=[callback], |
| 92 | + accelerator="cpu", |
| 93 | + max_epochs=epochs, |
| 94 | + ) |
| 95 | + trainer.train() |
| 96 | + |
| 97 | + # Check that the final weight value matches the target value |
| 98 | + final_value = solver.weighting.weights[cond_name] |
| 99 | + assert math.isclose(final_value, target_value) |
| 100 | + |
| 101 | + # Target_epoch must be greater than 0 |
| 102 | + with pytest.raises(ValueError): |
| 103 | + callback = LinearWeightUpdate( |
| 104 | + target_epoch=0, |
| 105 | + condition_name=cond_name, |
| 106 | + initial_value=0, |
| 107 | + target_value=1, |
| 108 | + ) |
| 109 | + trainer = Trainer( |
| 110 | + solver=solver, |
| 111 | + callbacks=[callback], |
| 112 | + accelerator="cpu", |
| 113 | + max_epochs=5, |
| 114 | + ) |
| 115 | + trainer.train() |
| 116 | + |
| 117 | + # Target_epoch must be less than or equal to max_epochs |
| 118 | + with pytest.raises(ValueError): |
| 119 | + callback = LinearWeightUpdate( |
| 120 | + target_epoch=epochs, |
| 121 | + condition_name=cond_name, |
| 122 | + initial_value=0, |
| 123 | + target_value=1, |
| 124 | + ) |
| 125 | + trainer = Trainer( |
| 126 | + solver=solver, |
| 127 | + callbacks=[callback], |
| 128 | + accelerator="cpu", |
| 129 | + max_epochs=epochs - 1, |
| 130 | + ) |
| 131 | + trainer.train() |
| 132 | + |
| 133 | + # Condition_name must be a problem condition |
| 134 | + with pytest.raises(ValueError): |
| 135 | + callback = LinearWeightUpdate( |
| 136 | + target_epoch=epochs, |
| 137 | + condition_name="not_a_condition", |
| 138 | + initial_value=0, |
| 139 | + target_value=1, |
| 140 | + ) |
| 141 | + trainer = Trainer( |
| 142 | + solver=solver, |
| 143 | + callbacks=[callback], |
| 144 | + accelerator="cpu", |
| 145 | + max_epochs=epochs, |
| 146 | + ) |
| 147 | + trainer.train() |
| 148 | + |
| 149 | + # Weighting schema must be ScalarWeighting |
| 150 | + with pytest.raises(ValueError): |
| 151 | + callback = LinearWeightUpdate( |
| 152 | + target_epoch=epochs, |
| 153 | + condition_name=cond_name, |
| 154 | + initial_value=0, |
| 155 | + target_value=1, |
| 156 | + ) |
| 157 | + unweighted_solver = PINN(problem=poisson_problem, model=model) |
| 158 | + trainer = Trainer( |
| 159 | + solver=unweighted_solver, |
| 160 | + callbacks=[callback], |
| 161 | + accelerator="cpu", |
| 162 | + max_epochs=epochs, |
| 163 | + ) |
| 164 | + trainer.train() |
0 commit comments