diff --git a/tests/test_xppbe.py b/tests/test_xppbe.py index ba78111..96852d7 100644 --- a/tests/test_xppbe.py +++ b/tests/test_xppbe.py @@ -59,11 +59,11 @@ def test_scripts(): @pytest.mark.parametrize( ('pinns_method','model','scheme'), ( - #('DCM','linear','direct'), + ('DCM','linear','direct'), ('DCM','linear', 'regularized_scheme_1'), - #('DCM','nonlinear','regularized_scheme_2'), - #('DVM','linear','direct'), - #('DVM','linear','regularized_scheme_1'), + ('DCM','nonlinear','regularized_scheme_2'), + ('DVM','linear','direct'), + ('DVM','linear','regularized_scheme_1'), ('DVM','nonlinear','regularized_scheme_1'), ('DBM','linear','direct') ) @@ -89,141 +89,141 @@ def test_pinns_method_and_schemes(pinns_method,model,scheme): run_checkers(sim,sim_name,temp_dir) -# @pytest.mark.parametrize( -# ('molecule'), -# ( -# ('methanol'), -# ('arg') -# ) -# ) -# def test_other_molecules(molecule): -# with tempfile.TemporaryDirectory() as temp_dir: -# sim_name = f'test_{molecule}' -# yaml_path = os.path.join(os.path.dirname(__file__),'simulations_yaml',sim_name+'.yaml') -# results_path = os.path.join(temp_dir,'results',sim_name) -# sim = Simulation(yaml_path, results_path=results_path, molecule_dir=None) -# sim.create_simulation() -# sim.adapt_model() -# sim.solve_model() -# sim.postprocessing(run_all=True) -# run_checkers(sim,sim_name,temp_dir) - - -# def test_optimizers(): -# with tempfile.TemporaryDirectory() as temp_dir: -# sim_name = f'test_optimizers' -# yaml_path = os.path.join(os.path.dirname(__file__),'simulations_yaml',sim_name+'.yaml') -# yaml_prev_path = os.path.join(os.path.dirname(__file__),'simulations_yaml','test_born_ion.yaml') -# shutil.copy(yaml_prev_path,yaml_path) -# results_path = os.path.join(temp_dir,'results',sim_name) -# sim = Simulation(yaml_path, results_path=results_path, molecule_dir=None) -# sim.options_optimizer2['maxiter'] = 3 -# sim.N_steps_2 = 2 -# sim.create_simulation() -# sim.adapt_model() -# sim.solve_model() -# sim.postprocessing(run_all=True) -# run_checkers(sim,sim_name,temp_dir,num_iters=2) - - -# @pytest.mark.parametrize( -# ('arch'), -# ( -# ('ModMLP'), -# ('ResNet') -# ) -# ) -# def test_other_architectures(arch): -# with tempfile.TemporaryDirectory() as temp_dir: -# sim_name = f'test_{arch}' -# yaml_path = os.path.join(os.path.dirname(__file__),'simulations_yaml',sim_name+'.yaml') -# yaml_prev_path = os.path.join(os.path.dirname(__file__),'simulations_yaml','test_born_ion.yaml') -# shutil.copy(yaml_prev_path,yaml_path) -# results_path = os.path.join(temp_dir,'results',sim_name) -# sim = Simulation(yaml_path, results_path=results_path, molecule_dir=None) -# sim.hyperparameters_in['architecture_Net'] = arch -# sim.create_simulation() -# sim.adapt_model() -# sim.solve_model() -# sim.postprocessing(run_all=True) -# run_checkers(sim,sim_name,temp_dir) - - - -# @pytest.mark.parametrize( -# ('loss'), -# ( -# ('K2'), -# ('E2'), -# ('G') -# ) -# ) -# def test_additional_losses(loss): -# with tempfile.TemporaryDirectory() as temp_dir: -# sim_name = f'test_{loss}' -# yaml_path = os.path.join(os.path.dirname(__file__),'simulations_yaml',sim_name+'.yaml') -# yaml_prev_path = os.path.join(os.path.dirname(__file__),'simulations_yaml','test_born_ion.yaml') -# shutil.copy(yaml_prev_path,yaml_path) -# results_path = os.path.join(temp_dir,'results',sim_name) -# sim = Simulation(yaml_path, results_path=results_path, molecule_dir=None) -# sim.losses.append(loss) -# if loss == 'E2': -# sim.mesh_properties['dR_exterior'] = 5 -# sim.create_simulation() -# sim.adapt_model() -# sim.solve_model() -# sim.postprocessing(run_all=True) -# run_checkers(sim,sim_name,temp_dir) - - -# def test_mesh_post(): -# with tempfile.TemporaryDirectory() as temp_dir: -# sim_name = f'test_mesh_post' -# yaml_path = os.path.join(os.path.dirname(__file__),'simulations_yaml',sim_name+'.yaml') -# yaml_prev_path = os.path.join(os.path.dirname(__file__),'simulations_yaml','test_born_ion.yaml') -# shutil.copy(yaml_prev_path,yaml_path) -# results_path = os.path.join(temp_dir,'results',sim_name) -# sim = Simulation(yaml_path, results_path=results_path, molecule_dir=None) -# sim.create_simulation() -# sim.adapt_model() -# sim.solve_model() -# sim.postprocessing(run_all=True, plot_mesh=True) -# run_checkers(sim,sim_name,temp_dir) - - -# def test_iteration_continuation(): -# with tempfile.TemporaryDirectory() as temp_dir: -# sim_name = 'test_iteration' -# yaml_path = os.path.join(os.path.dirname(__file__),'simulations_yaml',sim_name+'.yaml') -# yaml_prev_path = os.path.join(os.path.dirname(__file__),'simulations_yaml','test_born_ion.yaml') -# shutil.copy(yaml_prev_path,yaml_path) -# results_path = os.path.join(temp_dir,'results',sim_name) -# sim = Simulation(yaml_path, results_path=results_path, molecule_dir=None) -# sim.create_simulation() -# sim.adapt_model() -# sim.solve_model() -# sim.postprocessing(run_all=True) - -# assert os.path.isdir(results_path) -# assert len(os.listdir(results_path)) > 0 - -# iterations_path = os.path.join(results_path,'iterations') -# assert len(os.listdir(iterations_path)) == 1 - -# sim_2 = Simulation(yaml_path, results_path=results_path, molecule_dir=None) -# sim_2.N_iters = sim.N_iters*2 -# sim_2.create_simulation() -# sim_2.adapt_model() -# sim_2.solve_model() -# sim_2.postprocessing(run_all=True) - -# last_iteration = os.path.join(iterations_path,f'iter_{sim_2.N_iters}') - -# for file_name in ['loss.csv','loss_validation.csv','w_hist.csv','G_solv.csv']: -# file = os.path.join(last_iteration,file_name) -# with open(file, 'r') as csvfile: -# reader = csv.reader(csvfile) -# lines = list(reader) -# assert len(lines) == sim.N_iters*2 + 1 -# last_line = lines[-1] -# assert not '' in last_line +@pytest.mark.parametrize( + ('molecule'), + ( + ('methanol'), + ('arg') + ) +) +def test_other_molecules(molecule): + with tempfile.TemporaryDirectory() as temp_dir: + sim_name = f'test_{molecule}' + yaml_path = os.path.join(os.path.dirname(__file__),'simulations_yaml',sim_name+'.yaml') + results_path = os.path.join(temp_dir,'results',sim_name) + sim = Simulation(yaml_path, results_path=results_path, molecule_dir=None) + sim.create_simulation() + sim.adapt_model() + sim.solve_model() + sim.postprocessing(run_all=True) + run_checkers(sim,sim_name,temp_dir) + + +def test_optimizers(): + with tempfile.TemporaryDirectory() as temp_dir: + sim_name = f'test_optimizers' + yaml_path = os.path.join(os.path.dirname(__file__),'simulations_yaml',sim_name+'.yaml') + yaml_prev_path = os.path.join(os.path.dirname(__file__),'simulations_yaml','test_born_ion.yaml') + shutil.copy(yaml_prev_path,yaml_path) + results_path = os.path.join(temp_dir,'results',sim_name) + sim = Simulation(yaml_path, results_path=results_path, molecule_dir=None) + sim.options_optimizer2['maxiter'] = 3 + sim.N_steps_2 = 2 + sim.create_simulation() + sim.adapt_model() + sim.solve_model() + sim.postprocessing(run_all=True) + run_checkers(sim,sim_name,temp_dir,num_iters=2) + + +@pytest.mark.parametrize( + ('arch'), + ( + ('ModMLP'), + ('ResNet') + ) +) +def test_other_architectures(arch): + with tempfile.TemporaryDirectory() as temp_dir: + sim_name = f'test_{arch}' + yaml_path = os.path.join(os.path.dirname(__file__),'simulations_yaml',sim_name+'.yaml') + yaml_prev_path = os.path.join(os.path.dirname(__file__),'simulations_yaml','test_born_ion.yaml') + shutil.copy(yaml_prev_path,yaml_path) + results_path = os.path.join(temp_dir,'results',sim_name) + sim = Simulation(yaml_path, results_path=results_path, molecule_dir=None) + sim.hyperparameters_in['architecture_Net'] = arch + sim.create_simulation() + sim.adapt_model() + sim.solve_model() + sim.postprocessing(run_all=True) + run_checkers(sim,sim_name,temp_dir) + + + +@pytest.mark.parametrize( + ('loss'), + ( + ('K2'), + ('E2'), + ('G') + ) +) +def test_additional_losses(loss): + with tempfile.TemporaryDirectory() as temp_dir: + sim_name = f'test_{loss}' + yaml_path = os.path.join(os.path.dirname(__file__),'simulations_yaml',sim_name+'.yaml') + yaml_prev_path = os.path.join(os.path.dirname(__file__),'simulations_yaml','test_born_ion.yaml') + shutil.copy(yaml_prev_path,yaml_path) + results_path = os.path.join(temp_dir,'results',sim_name) + sim = Simulation(yaml_path, results_path=results_path, molecule_dir=None) + sim.losses.append(loss) + if loss == 'E2': + sim.mesh_properties['dR_exterior'] = 5 + sim.create_simulation() + sim.adapt_model() + sim.solve_model() + sim.postprocessing(run_all=True) + run_checkers(sim,sim_name,temp_dir) + + +def test_mesh_post(): + with tempfile.TemporaryDirectory() as temp_dir: + sim_name = f'test_mesh_post' + yaml_path = os.path.join(os.path.dirname(__file__),'simulations_yaml',sim_name+'.yaml') + yaml_prev_path = os.path.join(os.path.dirname(__file__),'simulations_yaml','test_born_ion.yaml') + shutil.copy(yaml_prev_path,yaml_path) + results_path = os.path.join(temp_dir,'results',sim_name) + sim = Simulation(yaml_path, results_path=results_path, molecule_dir=None) + sim.create_simulation() + sim.adapt_model() + sim.solve_model() + sim.postprocessing(run_all=True, plot_mesh=True) + run_checkers(sim,sim_name,temp_dir) + + +def test_iteration_continuation(): + with tempfile.TemporaryDirectory() as temp_dir: + sim_name = 'test_iteration' + yaml_path = os.path.join(os.path.dirname(__file__),'simulations_yaml',sim_name+'.yaml') + yaml_prev_path = os.path.join(os.path.dirname(__file__),'simulations_yaml','test_born_ion.yaml') + shutil.copy(yaml_prev_path,yaml_path) + results_path = os.path.join(temp_dir,'results',sim_name) + sim = Simulation(yaml_path, results_path=results_path, molecule_dir=None) + sim.create_simulation() + sim.adapt_model() + sim.solve_model() + sim.postprocessing(run_all=True) + + assert os.path.isdir(results_path) + assert len(os.listdir(results_path)) > 0 + + iterations_path = os.path.join(results_path,'iterations') + assert len(os.listdir(iterations_path)) == 1 + + sim_2 = Simulation(yaml_path, results_path=results_path, molecule_dir=None) + sim_2.N_iters = sim.N_iters*2 + sim_2.create_simulation() + sim_2.adapt_model() + sim_2.solve_model() + sim_2.postprocessing(run_all=True) + + last_iteration = os.path.join(iterations_path,f'iter_{sim_2.N_iters}') + + for file_name in ['loss.csv','loss_validation.csv','w_hist.csv','G_solv.csv']: + file = os.path.join(last_iteration,file_name) + with open(file, 'r') as csvfile: + reader = csv.reader(csvfile) + lines = list(reader) + assert len(lines) == sim.N_iters*2 + 1 + last_line = lines[-1] + assert not '' in last_line diff --git a/xppbe/Model/Solutions_utils.py b/xppbe/Model/Solutions_utils.py index eb3f5a3..8db9324 100644 --- a/xppbe/Model/Solutions_utils.py +++ b/xppbe/Model/Solutions_utils.py @@ -139,9 +139,9 @@ def dG_L(self,X,Xp,N): # Compute the derivative of the Green's function with respect to r dg_dr = (-1 / (4 * self.pi)) / (r**2) # Shape: (n, m) # Compute the components of the gradient - grad_x = dg_dr * r_diff[:, :, 0] / r # Shape: (n, m) - grad_y = dg_dr * r_diff[:, :, 1] / r # Shape: (n, m) - grad_z = dg_dr * r_diff[:, :, 2] / r # Shape: (n, m) + grad_x = -1*dg_dr * r_diff[:, :, 0] / r # Shape: (n, m) + grad_y = -1*dg_dr * r_diff[:, :, 1] / r # Shape: (n, m) + grad_z = -1*dg_dr * r_diff[:, :, 2] / r # Shape: (n, m) # Combine the components into the gradient grad = tf.stack([grad_x, grad_y, grad_z], axis=2) # Shape: (n, m, 3) # Compute the dot product of the gradient with the normal vector @@ -159,9 +159,9 @@ def dG_Y(self,X,Xp,N): # Compute the derivative of the Green's function with respect to r dg_dr = (-tf.exp(-self.kappa*r) / (4 * self.pi)) / (r) *(-self.kappa/r - 1/r**2) # Shape: (n, m) # Compute the components of the gradient - grad_x = dg_dr * r_diff[:, :, 0] / r # Shape: (n, m) - grad_y = dg_dr * r_diff[:, :, 1] / r # Shape: (n, m) - grad_z = dg_dr * r_diff[:, :, 2] / r # Shape: (n, m) + grad_x = -1*dg_dr * r_diff[:, :, 0] / r # Shape: (n, m) + grad_y = -1*dg_dr * r_diff[:, :, 1] / r # Shape: (n, m) + grad_z = -1*dg_dr * r_diff[:, :, 2] / r # Shape: (n, m) # Combine the components into the gradient grad = tf.stack([grad_x, grad_y, grad_z], axis=2) # Shape: (n, m, 3) # Compute the dot product of the gradient with the normal vector