### Create and activate a conda environment Source: https://github.com/benmoseley/fbpinns/blob/main/README.md Recommended setup for a new Python environment before installing FBPINNs. ```bash conda create -n fbpinns python=3 # Using conda conda activate fbpinns ``` -------------------------------- ### Install FBPINNs in editable mode Source: https://github.com/benmoseley/fbpinns/blob/main/README.md Install the package and its dependencies in editable mode, allowing source code changes to take effect immediately. ```bash pip install -e . ``` -------------------------------- ### Train a PINN for Comparison Source: https://github.com/benmoseley/fbpinns/blob/main/README.md Trains a standard PINN using the same hyperparameters as the FBPINN example, with a modified network size. ```python from fbpinns.trainers import PINNTrainer c["network_init_kwargs"] = dict(layer_sizes=[1,64,64,1])# use a larger neural network run = PINNTrainer(c) run.train()# start training a PINN on the same problem ``` -------------------------------- ### Initialize and train the FBPINN Source: https://github.com/benmoseley/fbpinns/blob/main/examples/1. Defining your own problem - 1D harmonic oscillator.ipynb Instantiate the FBPINNTrainer with a configuration object 'c' and execute the training process. ```python from fbpinns.trainers import FBPINNTrainer run = FBPINNTrainer(c) all_params = run.train() ``` -------------------------------- ### Initialize Rectangular Domain Source: https://github.com/benmoseley/fbpinns/blob/main/examples/1. Defining your own problem - 1D harmonic oscillator.ipynb Sets up the spatial domain for the problem using RectangularDomainND. ```python import numpy as np from fbpinns.domains import RectangularDomainND domain = RectangularDomainND domain_init_kwargs = dict( xmin=np.array([0.,]), xmax=np.array([1.,]),# solve the problem over the domain [0, 1] ) ``` -------------------------------- ### Clone the FBPINNs repository Source: https://github.com/benmoseley/fbpinns/blob/main/README.md Download the source code from the official GitHub repository. ```bash git clone git@github.com:benmoseley/FBPINNs.git ``` -------------------------------- ### Create Constants object Source: https://github.com/benmoseley/fbpinns/blob/main/examples/1. Defining your own problem - 1D harmonic oscillator.ipynb Instantiates the Constants object with the defined domain, problem, decomposition, and network parameters, followed by printing the configuration. ```python from fbpinns.constants import Constants c = Constants( domain=domain, domain_init_kwargs=domain_init_kwargs, problem=problem, problem_init_kwargs=problem_init_kwargs, decomposition=decomposition, decomposition_init_kwargs=decomposition_init_kwargs, network=network, network_init_kwargs=network_init_kwargs, ns=((200,),),# use 200 collocation points for training n_test=(500,),# use 500 points for testing n_steps=20000,# number of training steps clear_output=True, ) print(c) ``` ```text Output: run: test domain: domain_init_kwargs: {'xmin': array([0.]), 'xmax': array([1.])} problem: problem_init_kwargs: {'d': 2, 'w0': 80} decomposition: decomposition_init_kwargs: {'subdomain_xs': [array([0. , 0.07142857, 0.14285714, 0.21428571, 0.28571429, 0.35714286, 0.42857143, 0.5 , 0.57142857, 0.64285714, 0.71428571, 0.78571429, 0.85714286, 0.92857143, 1. ])], 'subdomain_ws': [array([0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15])], 'unnorm': (0.0, 1.0)} network: network_init_kwargs: {'layer_sizes': [1, 32, 1]} n_steps: 20000 scheduler: scheduler_kwargs: {} ns: ((200,),) n_test: (500,) sampler: grid optimiser: optimiser_kwargs: {'learning_rate': 0.001} seed: 0 summary_freq: 1000 test_freq: 1000 model_save_freq: 10000 show_figures: True save_figures: False clear_output: True hostname: staff-net-ox-dock-1-a-421.intern.ethz.ch ``` -------------------------------- ### Train FBPINN with HarmonicOscillator1DInverse Source: https://github.com/benmoseley/fbpinns/blob/main/examples/3. Solving inverse problems - 1D harmonic oscillator.ipynb Configures and trains a FBPINN using the custom Problem class. Uses a 1D domain [0,1] with 15 subdomains, a fully-connected network with 2 hidden layers, 200 collocation points, and 20000 training steps. Prints the learned mu value after training. ```python import numpy as np from fbpinns.domains import RectangularDomainND from fbpinns.decompositions import RectangularDecompositionND from fbpinns.networks import FCN from fbpinns.constants import Constants from fbpinns.trainers import FBPINNTrainer c = Constants( domain=RectangularDomainND,# use a 1D problem domain [0, 1] domain_init_kwargs=dict( xmin=np.array([0,]), xmax=np.array([1,]), ), problem=HarmonicOscillator1DInverse, problem_init_kwargs=dict( d=2, w0=80,# define the ODE parameters ), decomposition=RectangularDecompositionND,# use a rectangular domain decomposition decomposition_init_kwargs=dict( subdomain_xs=[np.linspace(0,1,15)],# use 15 equally spaced subdomains subdomain_ws=[0.15*np.ones((15,))],# with widths of 0.15 unnorm=(0.,1.),# define unnormalisation of the subdomain networks ), network=FCN,# place a fully-connected network in each subdomain network_init_kwargs=dict( layer_sizes=[1,32,1],# with 2 hidden layers ), ns=((200,),),# use 200 collocation points for training n_test=(500,),# use 500 points for testing n_steps=20000,# number of training steps clear_output=True, ) run = FBPINNTrainer(c) all_params = run.train() print(all_params["trainable"]["problem"])# print out learned value of mu ``` -------------------------------- ### Train FBPINN with HarmonicOscillator1DHardBC Source: https://github.com/benmoseley/fbpinns/blob/main/examples/2. Using hard constraints - 1D harmonic oscillator.ipynb Sets up and trains an FBPINN using the custom HarmonicOscillator1DHardBC problem. It configures a 1D domain [0,1], a rectangular decomposition with 15 subdomains of width 0.15, fully-connected networks with 2 hidden layers, 200 collocation points, 500 test points, and 20000 training steps. The training output shows completion after 20000 steps. ```python import numpy as np from fbpinns.domains import RectangularDomainND from fbpinns.decompositions import RectangularDecompositionND from fbpinns.networks import FCN from fbpinns.constants import Constants from fbpinns.trainers import FBPINNTrainer c = Constants( domain=RectangularDomainND,# use a 1D problem domain [0, 1] domain_init_kwargs=dict( xmin=np.array([0,]), xmax=np.array([1,]), ), problem=HarmonicOscillator1DHardBC, problem_init_kwargs=dict( d=2, w0=80,# define the ODE parameters ), decomposition=RectangularDecompositionND,# use a rectangular domain decomposition decomposition_init_kwargs=dict( subdomain_xs=[np.linspace(0,1,15)],# use 15 equally spaced subdomains subdomain_ws=[0.15*np.ones((15,))],# with widths of 0.15 unnorm=(0.,1.),# define unnormalisation of the subdomain networks ), network=FCN,# place a fully-connected network in each subdomain network_init_kwargs=dict( layer_sizes=[1,32,1],# with 2 hidden layers ), ns=((200,),),# use 200 collocation points for training n_test=(500,),# use 500 points for testing n_steps=20000,# number of training steps clear_output=True, ) run = FBPINNTrainer(c) all_params = run.train() ``` -------------------------------- ### Train FBPINN with ActiveScheduler for Burgers' Equation Source: https://github.com/benmoseley/fbpinns/blob/main/examples/4. Using subdomain scheduling - (1+1)D Burgers' equation.ipynb Sets up and trains a FBPINN for the (1+1)D Burgers' equation using a custom ActiveScheduler. The code imports required modules, defines subdomain grid points, configures the FBPINN constants (domain, problem, decomposition, network, scheduler, and training parameters), and runs the training. The output is a matplotlib figure. ```python import numpy as np from fbpinns.domains import RectangularDomainND from fbpinns.problems import BurgersEquation2D from fbpinns.decompositions import RectangularDecompositionND from fbpinns.networks import FCN from fbpinns.schedulers import LineSchedulerRectangularND from fbpinns.constants import Constants, get_subdomain_ws from fbpinns.trainers import FBPINNTrainer subdomain_xs=[np.linspace(-1,1,20), np.linspace(0,1,10)] c = Constants( domain=RectangularDomainND, domain_init_kwargs = dict( xmin=np.array([-1,0.]), xmax=np.array([1.,1.]) ), problem=BurgersEquation2D, problem_init_kwargs = dict(), decomposition=RectangularDecompositionND, decomposition_init_kwargs=dict( subdomain_xs=subdomain_xs, subdomain_ws=get_subdomain_ws(subdomain_xs, 2.9), unnorm=(0.,3.), ), network=FCN, network_init_kwargs = dict( layer_sizes = (2, 16, 1), ), scheduler = LineSchedulerRectangularND, scheduler_kwargs = dict( point=[0.], iaxis=0, ), ns=((200,100),), n_test=(200,100), n_steps=100000, clear_output=True, ) run = FBPINNTrainer(c) all_params = run.train() ``` ```text Result:
``` -------------------------------- ### Solve 1D Harmonic Oscillator with FBPINN Source: https://github.com/benmoseley/fbpinns/blob/main/README.md Configures and trains an FBPINN for a 1D harmonic oscillator problem using RectangularDomainND and FCN networks. ```python import numpy as np from fbpinns.domains import RectangularDomainND from fbpinns.problems import HarmonicOscillator1D from fbpinns.decompositions import RectangularDecompositionND from fbpinns.networks import FCN from fbpinns.constants import Constants from fbpinns.trainers import FBPINNTrainer c = Constants( domain=RectangularDomainND,# use a 1D problem domain [0, 1] domain_init_kwargs=dict( xmin=np.array([0,]), xmax=np.array([1,]), ), problem=HarmonicOscillator1D,# solve the 1D harmonic oscillator problem problem_init_kwargs=dict( d=2, w0=80,# define the ODE parameters ), decomposition=RectangularDecompositionND,# use a rectangular domain decomposition decomposition_init_kwargs=dict( subdomain_xs=[np.linspace(0,1,15)],# use 15 equally spaced subdomains subdomain_ws=[0.15*np.ones((15,))],# with widths of 0.15 unnorm=(0.,1.),# define unnormalisation of the subdomain networks ), network=FCN,# place a fully-connected network in each subdomain network_init_kwargs=dict( layer_sizes=[1,32,1],# with 2 hidden layers ), ns=((200,),),# use 200 collocation points for training n_test=(500,),# use 500 points for testing n_steps=20000,# number of training steps optimiser_kwargs=dict(learning_rate=1e-3), show_figures=True,# display plots during training ) run = FBPINNTrainer(c) run.train()# start training the FBPINN ``` -------------------------------- ### Training completion log output Source: https://github.com/benmoseley/fbpinns/blob/main/examples/1. Defining your own problem - 1D harmonic oscillator.ipynb The console output indicating that the training process has finished successfully. ```text [INFO] 2023-07-30 15:42:25 - [i: 20000/20000] Training complete ``` -------------------------------- ### BibTeX Citations for FBPINNs Source: https://github.com/benmoseley/fbpinns/blob/main/README.md Use these BibTeX entries to cite the FBPINNs project and related research in your own work. ```bibtex @article{Moseley2023, author = {Moseley, Ben and Markham, Andrew and Nissen-Meyer, Tarje}, doi = {10.1007/S10444-023-10065-9}, journal = {Advances in Computational Mathematics 2023 49:4}, month = {jul}, number = {4}, pages = {1--39}, publisher = {Springer}, title = {{Finite basis physics-informed neural networks (FBPINNs): a scalable domain decomposition approach for solving differential equations}}, url = {https://link.springer.com/article/10.1007/s10444-023-10065-9}, volume = {49}, year = {2023} } @article{Dolean2024, author = {Dolean, Victorita and Heinlein, Alexander and Mishra, Siddhartha and Moseley, Ben}, doi = {https://doi.org/10.1016/j.cma.2024.117116}, issn = {0045-7825}, journal = {Computer Methods in Applied Mechanics and Engineering}, pages = {117116}, title = {{Multilevel domain decomposition-based architectures for physics-informed neural networks}}, url = {https://www.sciencedirect.com/science/article/pii/S0045782524003724}, volume = {429}, year = {2024} } @article{Anderson2026, author = {Anderson, Samuel and Dolean, Victorita and Moseley, Ben and Pestana, Jennifer}, doi = {10.1007/S44379-026-00071-1}, isbn = {0123456789}, issn = {3005-1436}, journal = {Machine Learning for Computational Science and Engineering}, publisher = {Springer}, title = {{ELM-FBPINNs: an efficient multilevel random feature method}}, url = {https://link.springer.com/article/10.1007/s44379-026-00071-1}, volume = {2}, year = {2026} } @article{VanBeek2025, author = {van Beek, Jan Willem and Dolean, Victorita and Moseley, Ben}, doi = {10.1016/J.CMA.2025.118583}, issn = {0045-7825}, journal = {Computer Methods in Applied Mechanics and Engineering}, pages = {118583}, title = {{Local feature filtering for scalable and well-conditioned Random Feature Methods}}, url = {https://www.sciencedirect.com/science/article/pii/S0045782525008552}, volume = {449}, year = {2025} } ``` -------------------------------- ### Define HarmonicOscillator1DHardBC Problem Class Source: https://github.com/benmoseley/fbpinns/blob/main/examples/2. Using hard constraints - 1D harmonic oscillator.ipynb Defines a custom Problem class for the damped harmonic oscillator with hard boundary conditions. It overrides init_params, sample_constraints, constraining_fn, and loss_fn. The constraining_fn applies the hard BCs by multiplying the network output with tanh(x/sd)^2 and adding 1. The sample_constraints only returns physics loss constraints, removing boundary constraints. ```python import jax.numpy as jnp from fbpinns.problems import HarmonicOscillator1D class HarmonicOscillator1DHardBC(HarmonicOscillator1D): """Solves the time-dependent damped harmonic oscillator using hard boundary conditions d^2 u du m ----- + mu -- + ku = 0 dt^2 dt Boundary conditions: u (0) = 1 u'(0) = 0 """ @staticmethod def init_params(d=2, w0=20, sd=0.1): mu, k = 2*d, w0**2 static_params = { "dims":(1,1), "d":d, "w0":w0, "mu":mu, "k":k, "sd":sd, } return static_params, {} @staticmethod def sample_constraints(all_params, domain, key, sampler, batch_shapes): # physics loss x_batch_phys = domain.sample_interior(all_params, key, sampler, batch_shapes[0]) required_ujs_phys = ( (0,()), (0,(0,)), (0,(0,0)) ) return [[x_batch_phys, required_ujs_phys],]# only physics loss required in this case @staticmethod def constraining_fn(all_params, x_batch, u): sd = all_params["static"]["problem"]["sd"] x, tanh = x_batch[:,0:1], jnp.tanh u = 1 + (tanh(x/sd)**2) * u# applies hard BCs return u @staticmethod def loss_fn(all_params, constraints): mu, k = all_params["static"]["problem"]["mu"], all_params["static"]["problem"]["k"] # physics loss _, u, ut, utt = constraints[0] phys = jnp.mean((utt + mu*ut + k*u)**2) return phys ``` -------------------------------- ### Define RectangularDecompositionND Source: https://github.com/benmoseley/fbpinns/blob/main/examples/1. Defining your own problem - 1D harmonic oscillator.ipynb Initializes the domain decomposition using RectangularDecompositionND with specified subdomain spacing and widths. ```python from fbpinns.decompositions import RectangularDecompositionND decomposition = RectangularDecompositionND# use a rectangular domain decomposition decomposition_init_kwargs=dict( subdomain_xs=[np.linspace(0,1,15)],# use 15 equally spaced subdomains subdomain_ws=[0.15*np.ones((15,))],# with widths of 0.15 unnorm=(0.,1.),# define unnormalisation of the subdomain networks ) ``` -------------------------------- ### Define HarmonicOscillator1D Problem Class Source: https://github.com/benmoseley/fbpinns/blob/main/examples/1. Defining your own problem - 1D harmonic oscillator.ipynb Implements the HarmonicOscillator1D class by defining parameters, constraints, loss functions, and the exact solution. ```python class HarmonicOscillator1D(Problem): """Solves the time-dependent damped harmonic oscillator d^2 u du m ----- + mu -- + ku = 0 dt^2 dt Boundary conditions: u (0) = 1 u'(0) = 0 """ @staticmethod def init_params(d=2, w0=20): mu, k = 2*d, w0**2 static_params = { "dims":(1,1), "d":d, "w0":w0, "mu":mu, "k":k, } return static_params, {} @staticmethod def sample_constraints(all_params, domain, key, sampler, batch_shapes): # physics loss x_batch_phys = domain.sample_interior(all_params, key, sampler, batch_shapes[0]) required_ujs_phys = ( (0,()), (0,(0,)), (0,(0,0)) ) # boundary loss x_batch_boundary = jnp.array([0.]).reshape((1,1)) u_boundary = jnp.array([1.]).reshape((1,1)) ut_boundary = jnp.array([0.]).reshape((1,1)) required_ujs_boundary = ( (0,()), (0,(0,)), ) return [[x_batch_phys, required_ujs_phys], [x_batch_boundary, u_boundary, ut_boundary, required_ujs_boundary]] @staticmethod def loss_fn(all_params, constraints): mu, k = all_params["static"]["problem"]["mu"], all_params["static"]["problem"]["k"] # physics loss _, u, ut, utt = constraints[0] phys = jnp.mean((utt + mu*ut + k*u)**2) # boundary loss _, uc, utc, u, ut = constraints[1] boundary = 1e6*jnp.mean((u-uc)**2) + 1e2*jnp.mean((ut-utc)**2) return phys + boundary @staticmethod def exact_solution(all_params, x_batch, batch_shape=None): d, w0 = all_params["static"]["problem"]["d"], all_params["static"]["problem"]["w0"] w = jnp.sqrt(w0**2-d**2) phi = jnp.arctan(-d/w) A = 1/(2*jnp.cos(phi)) cos = jnp.cos(phi + w * x_batch) exp = jnp.exp(-d * x_batch) u = exp * 2 * A * cos return u problem = HarmonicOscillator1D problem_init_kwargs=dict( d=2, w0=80,# define the ODE parameters ) ``` -------------------------------- ### Import Problem Base Class Source: https://github.com/benmoseley/fbpinns/blob/main/examples/1. Defining your own problem - 1D harmonic oscillator.ipynb Imports necessary modules to define a custom problem class. ```python import jax.numpy as jnp from fbpinns.problems import Problem ``` -------------------------------- ### Training visualization figure Source: https://github.com/benmoseley/fbpinns/blob/main/examples/1. Defining your own problem - 1D harmonic oscillator.ipynb The resulting figure generated after the training process completes. ```text
``` -------------------------------- ### Define FCN network architecture Source: https://github.com/benmoseley/fbpinns/blob/main/examples/1. Defining your own problem - 1D harmonic oscillator.ipynb Configures the fully-connected neural network (FCN) to be placed in each subdomain. ```python from fbpinns.networks import FCN network = FCN# place a fully-connected network in each subdomain network_init_kwargs=dict( layer_sizes=[1,32,1],# with 2 hidden layers ) ``` -------------------------------- ### Define HarmonicOscillator1DInverse Problem class Source: https://github.com/benmoseley/fbpinns/blob/main/examples/3. Solving inverse problems - 1D harmonic oscillator.ipynb Defines a custom Problem class for the inverse damped harmonic oscillator. It sets mu as a trainable parameter, samples physics and data constraints (13 data points), and computes a combined loss with a 1e6 weight on the data term. ```python import jax.numpy as jnp from fbpinns.problems import HarmonicOscillator1D class HarmonicOscillator1DInverse(HarmonicOscillator1D): """Solves the time-dependent damped harmonic oscillator inverse problem d^2 u du m ----- + mu -- + ku = 0 dt^2 dt Boundary conditions: u (0) = 1 u'(0) = 0 """ @staticmethod def init_params(d=2, w0=20): mu, k = 2*d, w0**2 static_params = { "dims":(1,1), "d":d, "w0":w0, "mu_true":mu, "k":k, } trainable_params = { "mu":jnp.array(0.),# learn mu from constraints } return static_params, trainable_params @staticmethod def sample_constraints(all_params, domain, key, sampler, batch_shapes): # physics loss x_batch_phys = domain.sample_interior(all_params, key, sampler, batch_shapes[0]) required_ujs_phys = ( (0,()), (0,(0,)), (0,(0,0)) ) # data loss x_batch_data = jnp.linspace(0,1,13).astype(float).reshape((13,1))# use 13 observational data points u_data = HarmonicOscillator1DInverse.exact_solution(all_params, x_batch_data) required_ujs_data = ( (0,()), ) return [[x_batch_phys, required_ujs_phys], [x_batch_data, u_data, required_ujs_data]] @staticmethod def loss_fn(all_params, constraints): mu, k = all_params["trainable"]["problem"]["mu"], all_params["static"]["problem"]["k"] # physics loss _, u, ut, utt = constraints[0] phys = jnp.mean((utt + mu*ut + k*u)**2) # data loss _, uc, u = constraints[1] data = 1e6*jnp.mean((u-uc)**2) return phys + data ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.