lululxvi / deepxde

A library for scientific machine learning and physics-informed learning
https://deepxde.readthedocs.io
GNU Lesser General Public License v2.1
2.76k stars 758 forks source link

How to solve inverse problem with parameters in the boundary condition? #1612

Open qgs17 opened 10 months ago

qgs17 commented 10 months ago

Dear Dr.Lulu and developers, I want to solve for a parameter inversely based on some data. This parameter is not in the differential equation but is expressed in the boundary conditions.Can DeepXDE solve this problem?

Part of my code is below:

C1 = dde.Variable(0.1) #The parameter to be solved

def pde(x,y):
    dy_xx = dde.grad.hessian(y,x)
    return dy_xx - y/L**2

def boundary(x, on_boundary):
    return on_boundary and dde.utils.isclose(x[0], 0)

bc = dde.icbc.NeumannBC(geom, lambda x: C1/(2*D), boundary) # C1 is embeded in this boundary condition

external_trainable_variables = [C1]
variable = dde.callbacks.VariableValue(
    external_trainable_variables, period=500, filename="variables.dat"
)

model.compile("adam", lr=0.001, external_trainable_variables=external_trainable_variables)

losshistory, train_state = model.train(iterations=10000, callbacks=[variable])

error info: RuntimeError: Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved tensors after calling backward.

lululxvi commented 10 months ago

The parameter should be in the PDE. You can first solve the PDE without BC, and then use the PDE solution at boundary to compute C1.

tsarikahin commented 9 months ago

@lululxvi Actually the parameter does not have to be in the PDE, I solved an example where the unknown parameter is in the set of a BC.

lululxvi commented 9 months ago

@tsarikahin This is interesting and good to know. Could you show me a simple demo about how you do it?

tsarikahin commented 9 months ago

@lululxvi We generated a repository (namely pinns_for_comp_mech) based on DeepXDE with a focus on computational mechanics. Here's an example of the Lame problem in elasticity. We define inlet pressure (pressure_inlet_predicted) as a tensor Variable (line 60) and use that in the boundary condition function (line 67 and line 97).

FIY: @maxdanwitz

tsarikahin commented 9 months ago

Here is the outcome. Pressure (defined in BC), Youngs modulus (defined in PDE) and Poissons (defined in PDE) ratio are set as variables and calculated by solving the inverse problem.

image

jdellag commented 8 months ago

@tsarikahin Could you explain how the predicted values align if we examine the data more closely, particularly ~30,000 iterations and beyond? I am also solving an inverse problem with a trainable parameter in my boundary conditions. Despite the predicted values being reasonably close, I'm not observing the perfect overlap of the two lines that typically occurs with conventional numerical methods. image