1+ import numpy as np
2+ import copy
3+ from epde .interface .interface import EpdeSearch
4+ from epde .interface .equation_translator import translate_equation
5+ from epde .structure .main_structures import SoEq , Chromosome
6+ from epde .interface .token_family import TFPool
7+ from epde .operators .utils .default_parameter_loader import EvolutionaryParams
8+ from epde .solver .factory import SolverFactory
9+
10+ def create_equation_from_str (eq_str , target_var , base_pool , all_vars ):
11+ families_copy = [copy .deepcopy (fam ) for fam in base_pool .families ]
12+ for fam in families_copy :
13+ if hasattr (fam , 'variable' ) and fam .variable is not None and fam .variable != target_var :
14+ fam .status ['demands_equation' ] = False
15+ temp_pool = TFPool (families_copy )
16+ soeq = translate_equation (eq_str , temp_pool , all_vars = [target_var ])
17+ return soeq .vals [target_var ]
18+
19+ def solve_with_solver (solver_type , solver_config , system , grids , data ):
20+ adapter = SolverFactory .create (solver_type , ** solver_config )
21+ solutions , loss = adapter .solve (system , grids , data )
22+ return solutions , loss
23+
24+ # ----------------------------------------------------------------------
25+ # Волновое уравнение (PDE) – только DeepXDE
26+ # ----------------------------------------------------------------------
27+ print ("=" * 60 )
28+ print ("Wave equation (PDE) – DeepXDE PINN solver" )
29+ print ("=" * 60 )
30+
31+ # Генерация данных (аналитическое решение)
32+ nx , nt = 50 , 50
33+ x = np .linspace (0 , 1 , nx )
34+ t = np .linspace (0 , 2 , nt )
35+ X_grid , T_grid = np .meshgrid (t , x , indexing = 'ij' )
36+ exact = np .sin (np .pi * X_grid ) * np .cos (np .pi * T_grid )
37+ data_wave = exact + 0.01 * np .random .normal (size = exact .shape )
38+
39+ # Создание пула EPDE
40+ search_wave = EpdeSearch (
41+ use_solver = False ,
42+ coordinate_tensors = (T_grid , X_grid ),
43+ verbose_params = {'show_iter_idx' : False },
44+ device = 'cpu'
45+ )
46+ search_wave .set_preprocessor (default_preprocessor_type = 'FD' , preprocessor_kwargs = {})
47+ search_wave .create_pool (
48+ data = data_wave ,
49+ variable_names = ['u' ],
50+ max_deriv_order = (2 , 2 ),
51+ additional_tokens = []
52+ )
53+
54+ # Уравнение волновое
55+ eq_str = '1.0 * d^2u/dx1^2{power: 1.0} = d^2u/dx0^2{power: 1.0}'
56+ soeq_wave = translate_equation (eq_str , search_wave .pool , all_vars = ['u' ])
57+ eq_wave = soeq_wave .vals ['u' ]
58+ eq_wave .main_var_to_explain = 'u'
59+ eq_wave .weights_internal = np .ones (len (eq_wave .structure ) - 1 )
60+ eq_wave .weights_internal_evald = True
61+ eq_wave .weights_final_evald = True
62+
63+ system_wave = SoEq (search_wave .pool , {})
64+ system_wave .vals = Chromosome ({'u' : eq_wave }, {})
65+ system_wave .moeadd_set = True
66+
67+ # Конфигурация DeepXDE (увеличиваем параметры для PDE)
68+ solver_config_deepxde = EvolutionaryParams ().get_default_params_for_operator ('DeepXDEBasedFitness' )
69+ solver_config_deepxde ['num_domain' ] = 2000
70+ solver_config_deepxde ['num_boundary' ] = 500
71+ solver_config_deepxde ['num_initial' ] = 500
72+ solver_config_deepxde ['epochs' ] = 3000
73+
74+ solutions_wave , loss_wave = solve_with_solver (
75+ "deepxde" ,
76+ solver_config_deepxde ,
77+ system_wave ,
78+ [T_grid , X_grid ],
79+ [data_wave .flatten ()]
80+ )
81+
82+ soln_wave = solutions_wave [0 ].reshape (data_wave .shape )
83+ print (f"Loss (RMSE): { loss_wave :.6f} " )
84+ print (f"Max error: { np .max (np .abs (soln_wave - exact )):.6f} " )
0 commit comments