Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set up different materials in the electrical field? #480

Closed
Blackrobin15 opened this issue Jan 12, 2022 · 18 comments
Closed

How to set up different materials in the electrical field? #480

Blackrobin15 opened this issue Jan 12, 2022 · 18 comments

Comments

@Blackrobin15
Copy link

Blackrobin15 commented Jan 12, 2022

Hello, Lu Lu
I am trying to set up different materials in the electrical field. But I have no idea how to set the permittivity in different regions. Could you please give me some advice?
Thanks a lot

def perm(x):
    if geom_cir.inside(x):
        return 6
    else:
        return 1
    
def pde(x,u):
    du_x = dde.grad.jacobian(u, x, i=0, j=0)
    du_y = dde.grad.jacobian(u, x, i=0, j=1)
    #du_xx=geom_cir.inside(x)*1*dde.grad.jacobian(du_x, x, i=0, j=0)+(~geom_cir.inside(x)*4*dde.grad.jacobian(du_x, x, i=0, j=0))
    #du_yy=geom_cir.inside(x)*1*dde.grad.jacobian(du_y, x, i=0, j=1)+(~geom_cir.inside(x)*4*dde.grad.jacobian(du_y, x, i=0, j=1))
    #du_xx=perm(x)*dde.grad.jacobian(du_x, x, i=0, j=0)
    du_xx=np.heaviside(x[:,0:1],1)*dde.grad.jacobian(du_x, x, i=0, j=0)
    du_yy=np.heaviside(x[:,1:2],1)*dde.grad.jacobian(du_x, x, i=0, j=1)
    return du_xx+du_yy
    

NotImplementedError: Cannot convert a symbolic Tensor (strided_slice_2:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported

The error occurred in the code:
model.compile("adam", lr=1.0e-3)

The details are as follows :

NotImplementedError Traceback (most recent call last)
in
2 model = dde.Model(data, net)
3
----> 4 model.compile("adam", lr=1.0e-3)
5 #model.train(epochs=10000)
6 #model.compile("L-BFGS")

D:\Anaconda3\lib\site-packages\deepxde\utils\internal.py in wrapper(*args, **kwargs)
20 def wrapper(*args, **kwargs):
21 ts = timeit.default_timer()
---> 22 result = f(*args, **kwargs)
23 te = timeit.default_timer()
24 print("%r took %f s\n" % (f.name, te - ts))

D:\Anaconda3\lib\site-packages\deepxde\model.py in compile(self, optimizer, lr, loss, metrics, decay, loss_weights, external_trainable_variables)
105
106 if backend_name == "tensorflow.compat.v1":
--> 107 self._compile_tensorflow_compat_v1(lr, loss_fn, decay, loss_weights)
108 elif backend_name == "tensorflow":
109 self._compile_tensorflow(lr, loss_fn, decay, loss_weights)

D:\Anaconda3\lib\site-packages\deepxde\model.py in _compile_tensorflow_compat_v1(self, lr, loss_fn, decay, loss_weights)
125
126 # Data losses
--> 127 losses = self.data.losses(self.net.targets, self.net.outputs, loss_fn, self)
128 if not isinstance(losses, list):
129 losses = [losses]

D:\Anaconda3\lib\site-packages\deepxde\data\pde.py in losses(self, targets, outputs, loss, model)
127 if self.pde is not None:
128 if get_num_args(self.pde) == 2:
--> 129 f = self.pde(model.net.inputs, outputs)
130 elif get_num_args(self.pde) == 3:
131 if self.auxiliary_var_fn is None:

in pde(x, u)
11 #du_yy=geom_cir.inside(x)1dde.grad.jacobian(du_y, x, i=0, j=1)+(~geom_cir.inside(x)4dde.grad.jacobian(du_y, x, i=0, j=1))
12 #du_xx=perm(x)*dde.grad.jacobian(du_x, x, i=0, j=0)
---> 13 du_xx=np.heaviside(x[:,:1],1)*dde.grad.jacobian(du_x, x, i=0, j=0)
14 #return du_xx+du_yy
15 return du_xx

D:\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in array(failed resolving arguments)
918 def array(self, dtype=None):
919 del dtype
--> 920 raise NotImplementedError(
921 "Cannot convert a symbolic Tensor ({}) to a numpy array."
922 " This error may indicate that you're trying to pass a Tensor to"

NotImplementedError: Cannot convert a symbolic Tensor (strided_slice_2:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported

@lululxvi
Copy link
Owner

Use auxiliary_var_function, see example https://github.com/lululxvi/deepxde/blob/master/examples/Lorenz_inverse_forced.ipynb

@Blackrobin15
Copy link
Author

Thanks for your reply!
I've read your comments #79 (comment)
I think the reason may be that x in pde(x,u) is using tensorflow, but geometry.inside(x) function is using numpy. I am trying to using tf.where() to solve this problem. It seems like that I need to define the geometric areas of different materials in pde(x,u).

@Blackrobin15
Copy link
Author

Hello, Dr. Lu @lululxvi
It seems like I have solve different materials setting problem with tf.where(). But I found that the pde() function did not work as as expected. The physical scenario is as follows:
image
My pde function setting is:

def pde(x,u):
    du_x = dde.grad.jacobian(u, x, i=0, j=0)
    du_y = dde.grad.jacobian(u, x, i=0, j=1)
    #the center of the circle (1,1), radius 0.5 
    condition=tf.less((x[:,0:1]-1.0)**2+(x[:,1:2]-1.0)**2,0.25)
    pe=tf.where(condition,4.0,1.0)
    result=dde.grad.jacobian(pe*du_x, x, i=0, j=0)+dde.grad.jacobian(pe*du_x, x, i=0, j=1)
    return result

What might be the problem with this representation?

@lululxvi
Copy link
Owner

The code looks OK. What doesn't work as expected?

@Blackrobin15
Copy link
Author

Thank you for your reply! I use a FEM work for comparison, The FEM result is as follow:

image
The DeepXDE result is as follow:
image

It seems like the change happens into y in [1.75,2] not in the circle area. This confuses me.

@lululxvi
Copy link
Owner

Instead of tf.where, could you try auxiliary_var_function?

@Blackrobin15
Copy link
Author

Thanks for your help!
I am going to try to code with auxiliary_var_function right now and I uploaded my datas and codes in https://github.com/Blackrobin15/Questions in folder Question1.
If you have time, could you please take a look?

Repository owner deleted a comment from hchoiz Mar 18, 2022
@aminetazarine
Copy link

aminetazarine commented May 18, 2022

Use auxiliary_var_function, see example https://github.com/lululxvi/deepxde/blob/master/examples/Lorenz_inverse_forced.ipynb

Hello lululxvi
Can you please send the code for this function (auxiliary_var_function) because this link does not work.
Another question: can we use np.where instead of tf.where.
One more
Thank you.

@lululxvi
Copy link
Owner

You can find the examples at https://deepxde.readthedocs.io/en/latest/demos/pinn_inverse.html

Use np.where for numpy array; use tf.where for tensor.

@aminetazarine
Copy link

aminetazarine commented May 19, 2022

Hello Lululxvi
Please i need your help how can i use auxiliary_var_function Instead of tf.where, because i want to set the permittivity in different regions this is my pde:
def pde(x, y):

ep*dEz/dt -dHy/dx+dHx/dy=0

Ez, Hx, Hy = y[:, 0:1], y[:, 1:2], y[:, 2:3]
dEz_x = dde.grad.jacobian(y, x, i=0, j=0) #dEz/dx
dEz_y = dde.grad.jacobian(y, x, i=0, j=1) #dEz/dy
dEz_t = dde.grad.jacobian(y, x, i=0, j=2) #dEz/dt


dHx_x = dde.grad.jacobian(y, x, i=1, j=0) #dHx/dx
dHx_y = dde.grad.jacobian(y, x, i=1, j=1) #dHx/dy
dHx_t = dde.grad.jacobian(y, x, i=1, j=2) #dHx/dt

dHy_x = dde.grad.jacobian(y, x, i=2, j=0) #dHy/dx
dHy_y = dde.grad.jacobian(y, x, i=2, j=1) #dHy/dy
dHy_t = dde.grad.jacobian(y, x, i=2, j=2) #dHy/dt

#this is the condtion of ep ===> ep=2 if 0<=x<=0.5 and ep=1 if 0.5<=x<=1

cond=tf.less(x[:,0:1],0.5)
epsilon_1=dde.Variable(1.0)
epsilon_2=dde.Variable(2.0)
f1 =epsilon_2*dEz_t-dHy_x+dHx_y 
f2 =epsilon_1*dEz_t-dHy_x+dHx_y 
f=tf.where(cond,f1,f2)
return f

It is true?/??

@lululxvi
Copy link
Owner

epsilon_1 = 1
epsilon_2 = 2

@chang-change
Copy link

@lululxvi
Hello Lulu,
I use auxiliary_var_function Instead of tf.where, but I don't know if it's right?
This is my code:

def epsilon(x):
    #the center of the circle (1,1), radius 0.5
    if (x[:,0:1]-1)**2 + (x[:,1:2]-1)**2 <0.25:
        ep = 4
    else:
        ep = 1
    return ep 


def pde(x,u,ex):
    du_x = dde.grad.jacobian(u, x, i=0, j=0)
    du_y = dde.grad.jacobian(u, x, i=0, j=1)
     

    result=dde.grad.jacobian(epsilon*du_x, x, i=0, j=0)+dde.grad.jacobian(epsilon*du_x, x, i=0, j=1)
    return result

data = dde.data.PDE(
    geom,
    pde,
    [bc],
    num_domain=400,
    num_boundary=2,
    auxiliary_var_function=epsilon,
)

Thank you.

@lululxvi
Copy link
Owner

Looks good.

@ach14012022
Copy link

@chang-change Please let me know if your code is run without any error.

@chang-change
Copy link

@ach14012022 I don't have the complete code, I just modified this piece of code. Hence, I don't know if it will work successfully.

@mengxia123
Copy link

感谢您的帮助!我现在要尝试编码,我将我的数据和代码上传到 https://github.com/Blackrobin15/Questions 文件夹中的问题 1。如果你有时间,可以看看吗?auxiliary_var_function

感谢您的帮助!我现在要尝试编码,我将我的数据和代码上传到 https://github.com/Blackrobin15/Questions 文件夹中的问题 1。如果你有时间,可以看看吗?auxiliary_var_function
您好,同学。我最近也在做电气领域不同介质的PDE,遇到的问题和你一样,能否借鉴下你的代码,是怎么处理。万分感谢

@Blackrobin15
Copy link
Author

In fact, I used a little trick that I saw in a paper 10.1021/acsphotonics.0c01202. You can read it if you are interested. Another method is to use tf.where as the corresponding dielectric constant for residual points in different positions as mentioned above.

感谢您的帮助!我现在要尝试编码,我将我的数据和代码上传到 https://github.com/Blackrobin15/Questions 文件夹中的问题 1。如果你有时间,可以看看吗?auxiliary_var_function

感谢您的帮助!我现在要尝试编码,我将我的数据和代码上传到 https://github.com/Blackrobin15/Questions 文件夹中的问题 1。如果你有时间,可以看看吗?auxiliary_var_function
您好,同学。我最近也在做电气领域不同介质的PDE,遇到的问题和你一样,能否借鉴下你的代码,是怎么处理。万分感谢

@mengxia123
Copy link

Thank you for your reply! I use a FEM work for comparison, The FEM result is as follow:

image The DeepXDE result is as follow: image

It seems like the change happens into y in [1.75,2] not in the circle area. This confuses me.

I'm not sure if your problem has been solved? I am currently working in this direction and need to be confused, such as how to handle the same domain, different PDEs, interfaces, etc. Could you please refer to your code for me to learn. obliged to sb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants