@@ -36,36 +36,36 @@ class TestTrainPINN(unittest.TestCase):
3636 - Different optimizers and loss functions: Ensures the function works correctly with various optimizers and loss functions.
3737 - Multiple layers and nodes: Tests the function with different feed-forward neural network architectures.
3838 """
39-
39+
4040 def setUp (self ):
4141 """
4242 Sets up the test environment and initializes common test parameters.
43- The `setUp` method is called before every test function in this class.
44- It initializes the parameters, optimizer, and other inputs used by
45- the `train_PINN` function.
43+ Ensures `weights` and `colloc` are not equal to test loss properly.
4644 """
47- # Initialize common test parameters
45+ # Initialize weights and collocation points to be different
4846 self .params = [{
4947 'weights' : jnp .array ([1.0 , 2.0 ]), # Initial weights
50- 'biases' : jnp .array ([0.5 ]) # Initial biases
48+ 'biases' : jnp .array ([0.5 ])
5149 }]
52- self .epochs = 100 # Number of training epochs
53- self .optimizer = optax .adam (
54- 0.001 ) # Adam optimizer with learning rate 0.001
50+ self .colloc = jnp .array ([3.0 , - 1.0 ]) # Different from weights to ensure meaningful loss
51+ assert not jnp .array_equal (self .params [0 ]['weights' ], self .colloc ), \
52+ "Initial weights and collocation points should not be equal."
53+
54+ self .epochs = 100
55+ self .optimizer = optax .adam (0.1 )
5556 self .loss_fun = lambda params , colloc , conds , norm_coeff : jnp .mean (
56- (params [0 ]['weights' ] - colloc )** 2 ) # Simple loss function
57- self .colloc = jnp .array ([1.0 , 2.0 ]) # Collocation points
58- self .conds = [jnp .array ([1.0 , 2.0 ])] # Boundary conditions
59- self .norm_coeff = jnp .array ([1.0 ]) # Normalization coefficient
60- self .hidden_layers = 2 # Number of hidden layers in the neural network
61- self .hidden_nodes = 10 # Number of hidden nodes for each layer
62- self .lr = 0.001 # Learning rate for the optimizer
63- self .results_dir = './results/' # Results directory to save the .pkl file
64-
65- # Create results directory if it doesn't exist
57+ (params [0 ]['weights' ] - colloc ) ** 2 )
58+ self .conds = [jnp .array ([1.0 , 2.0 ])]
59+ self .norm_coeff = jnp .array ([1.0 ])
60+ self .hidden_layers = 2
61+ self .hidden_nodes = 10
62+ self .lr = 0.001
63+ self .results_dir = './results/'
64+
6665 if not os .path .exists (self .results_dir ):
6766 os .makedirs (self .results_dir )
6867
68+
6969 def test_train_PINN_output (self ):
7070 """
7171 Test the output of the `train_PINN` function.
@@ -296,14 +296,19 @@ def test_train_PINN_multiple_layers_nodes(self):
296296 best_params ) # Ensure best_params is not None
297297 self .assertLessEqual (best_loss ,
298298 float ('inf' )) # Ensure loss is finite
299+
300+
299301
300302
301- def main ():
302- """
303- Run the unit tests when script is executed `python test_train_pinn.py`.
304- """
303+ if __name__ == '__main__' :
304+ # Option 1: Run through standard unittest (preserves CLI usability)
305305 unittest .main ()
306306
307-
308- if __name__ == '__main__' :
309- main ()
307+ # Option 2: Also make individual test methods REPL-executable
308+ print ("\n Manual REPL-friendly execution of each test case:" )
309+ t = TestTrainPINN ()
310+ t .setUp ()
311+ for item in dir (t ):
312+ if item .startswith ('test_' ):
313+ print (f'\n Running test: { item } ' )
314+ getattr (t , item )()
0 commit comments