Closed jiweiqi closed 4 years ago
The error is due to lambda x: initial_values[0]
. Here x
is a Nx5 array, and the function should return a Nx1 array. So, use lambda x: np.ones((len(x), 1)) * initial_values[0])
Xiexie Lushen. The solution works as ic = dde.IC(timedomain, lambda x: initial_values, boundary)
, instead of setting it up component by component.
Another quick question is how to choose the number of boundary points, i.e., the last argument in data = dde.data.PDE(timedomain, ode_system, ic, 2500, 2)
. Is the number of boundary points related to the weighting factor for the (soft) loss at the boundary?
Are you sure that ic = dde.IC(timedomain, lambda x: initial_values, boundary)
works without modifying the source code?
There is only a time domain with left and right boundary points, i.e., 2 boundary points. This is not related to the weights of the loss.
@lululxvi No, ic = dde.IC(timedomain, lambda x: initial_values, boundary)
not works. Do you know why?
For your initial comment of
The error is due to lambda x: initial_values[0]. Here x is a Nx5 array, and the function should return a Nx1 array. So, use lambda x: np.ones((len(x), 1)) * initial_values[0])
lambda x: initial_values[0]
works well if you don't use it in for loop. The problem is that you can not put lambda
function inside for loop, otherwise, it will be evaluated for every iteration. Such that ic[0], ic[1], ic[2], ic[3] = ic[4] =0. But ic[0] is supposed to be not 0. There are some tricks to get around it. But we didn't succeed.
lambda x: initial_values[0]
works without the for loop.ic[i] = dde.IC(timedomain, lambda x, index=i: initial_values[index], boundary, component = i)
Got it. It makes sense to define ic separately.
ic[i] = dde.IC(timedomain, lambda x, index=i: initial_values[index], boundary, component = i)
This one is not working, with the error of
TypeError: only integer scalar arrays can be converted to a scalar index
What about other solutions in the stackoverflow, e.g.,
def make_func(i):
initial_values = ...
return lambda x: initial_values[i]
The make_func
works! Cool. I post the code here, in case someone else in interested into it.
def make_func(i):
return lambda x: initial_values[i]
for i in range(5):
ic[i] = dde.IC(timedomain, make_func(i), boundary, component = i)
# Print the ic for the first species to see whether it is set correctly.
print(ic[0].func(x=0))
Hi,
I am playing with PINN for a toy ODE problem of a reaction network model. It has five state variables, i.e., five species. Therefore, we have to set five initial conditions as
We try to set
ic
in a for loop to make life easier, especially when you have hundreds of species. Something likeHowever, this will not work since define a lambda function inside a for loop will make all of the ics being the value of
i=5
. We have tried the suggestions from https://stackoverflow.com/questions/19837486/python-lambda-in-a-loop.But it gives another error
TypeError: only integer scalar arrays can be converted to a scalar index
.Any suggestion?
Below is the entire code