Coder Social home page Coder Social logo

Coupled PDEs about neurodiffeq HOT 21 CLOSED

neurodiffgym avatar neurodiffgym commented on May 28, 2024
Coupled PDEs

from neurodiffeq.

Comments (21)

dsondak avatar dsondak commented on May 28, 2024

Hello @katayooneshkofti! Yes, this is possible. We have solved the Navier-Stokes equations, which are a couple set of nonlinear PDEs. However, we have not tried PDEs in thermoelasticity yet, so we cannot guarantee the performance of the method on them. However, we'd certainly be interested to see how it goes! Let us know if you have any questions.

from neurodiffeq.

katayooneshkofti avatar katayooneshkofti commented on May 28, 2024

Hello @katayooneshkofti! Yes, this is possible. We have solved the Navier-Stokes equations, which are a couple set of nonlinear PDEs. However, we have not tried PDEs in thermoelasticity yet, so we cannot guarantee the performance of the method on them. However, we'd certainly be interested to see how it goes! Let us know if you have any questions.

Thanks for your prompt response. So, I am going to work on coupled PDEs in thermoelasticity utilizing this method.

from neurodiffeq.

katayooneshkofti avatar katayooneshkofti commented on May 28, 2024

Hi @dsondak,
I have sort of a challenge defining my problem in NeuroDiffEq. is it possible to upload the code of the Navier Stokes PDEs?

from neurodiffeq.

katayooneshkofti avatar katayooneshkofti commented on May 28, 2024

@dsondak @shuheng-liu
I will be grateful if you help me with this: for example for a system of coupled PDEs, how can I define these boundary and initial conditions?
1

from neurodiffeq.

shuheng-liu avatar shuheng-liu commented on May 28, 2024

For this combination of conditions, it's time to take out the big gun.

  1. install neurodiffeq_conditions which is an extension to neurodiffeq with more initial/boundary conditions.
pip install -U git+https://github.com/NeuroDiffGym/neurodiffeq-conditions.git
  1. Try this: (for simplicity I assumed r_in=0, r_out=1 and the gaussian function is exp(-t^2).)
import torch
from neurodiffeq_conditions import ConditionComponent as Component

r_in, r_out = 0.0, 1.0
# initial condition for T(r, t)
T_t0 = Component(0.0, f_dirichlet=lambda r, t: 0.0, f_neumann=lambda r, t: 0.0, coord_index=1)
# initial condition for u(r, t)
u_t0 = Component(0.0, f_dirichlet=lambda r, t: 0.0, f_neumann=lambda r, t: 0.0, coord_index=1)

# boudnary condition for T(r, t)
T_rout = Component(r_out, f_dirichlet=lambda r, t: 0.0, f_neumann=None, coord_index=0)
T_rin = Component(r_in, f_dirichlet=lambda r, t: torch.exp(-t**2), f_neumann=None, coord_index=0)
# boundary condition for u(r, t)
u_rout = Component(r_out, f_dirichlet=lambda r, t: 0.0, f_neumann=None, coord_index=0)
u_rin = Component(r_in, f_dirichlet=lambda r, t: 0.0, f_neumann=None, coord_index=0)

# put the components together
from neurodiffeq_conditions import ComposedCondition as Condition
condition_T = Condition(components=[T_t0, T_rout, T_rin])
condition_u = Condition(components=[u_t0, u_rout, u_rin])

# the following is standard usage of neurodiffeq
from neurodiffeq.solvers import Solver2D
solver = Solver2D(
    pde_system=lambda r, t: [..., ...], 
    conditions=[condition_T, condition_u],
    ...
)
solver.solve(max_epochs=...)
solution = solver.get_solution(best=True, copy=True)

There's one catch though. While you can enforce the above condition on points arbitrarily close the boundary, it CANNOT be enforced on points ON the boundary. Doing so will result in a division by 0 and give you NaNs. So, make sure your train_generator and valid_generator doesn't sample points exactly on the boundary.

from neurodiffeq.

shuheng-liu avatar shuheng-liu commented on May 28, 2024

@katayooneshkofti There was a bug in the previous neurodiffeq_conditions implementation and I just fixed it. You should be able to use it if you update your package by

pip install -U git+https://github.com/NeuroDiffGym/neurodiffeq-conditions.git

from neurodiffeq.

katayooneshkofti avatar katayooneshkofti commented on May 28, 2024

@shuheng-liu
Thank you so much. That was a great help. I will run the code and inform you as soon as possible.

from neurodiffeq.

katayooneshkofti avatar katayooneshkofti commented on May 28, 2024

@shuheng-liu
Sorry to bother you again, but when I was trying to install the "neurodiffeq_conditions" package in Spyder, I repeatedly encountered this type of error and I have no idea what should I do?

`pip install -U git+https://github.com/NeuroDiffGym/neurodiffeq-conditions
Collecting git+https://github.com/NeuroDiffGym/neurodiffeq-conditions
Cloning https://github.com/NeuroDiffGym/neurodiffeq-conditions to c:\users\katayoon\appdata\local\temp\pip-req-build-l9aet3r1
Note: you may need to restart the kernel to use updated packages.
Running command git clone -q https://github.com/NeuroDiffGym/neurodiffeq-conditions 'C:\Users\Katayoon\AppData\Local\Temp\pip-req-build-l9aet3r1'
ERROR: Command errored out with exit status 1:
command: 'C:\Users\Katayoon\anaconda3\pythonw.exe' -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\Users\Katayoon\AppData\Local\Temp\pip-req-build-l9aet3r1\setup.py'"'"'; file='"'"'C:\Users\Katayoon\AppData\Local\Temp\pip-req-build-l9aet3r1\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' egg_info --egg-base 'C:\Users\Katayoon\AppData\Local\Temp\pip-pip-egg-info-s4_jm8hd'
cwd: C:\Users\Katayoon\AppData\Local\Temp\pip-req-build-l9aet3r1
Complete output (30 lines):
Traceback (most recent call last):
File "C:\Users\Katayoon\anaconda3\lib\site-packages\setuptools\installer.py", line 128, in fetch_build_egg
subprocess.check_call(cmd)
File "C:\Users\Katayoon\anaconda3\lib\subprocess.py", line 363, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['C:\Users\Katayoon\anaconda3\pythonw.exe', '-m', 'pip', '--disable-pip-version-check', 'wheel', '--no-deps', '-w', 'C:\Users\Katayoon\AppData\Local\Temp\tmpvereplk2', '--quiet', 'setuptools_scm']' returned non-zero exit status 1.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\Katayoon\AppData\Local\Temp\pip-req-build-l9aet3r1\setup.py", line 22, in <module>
    include_package_data=True,
  File "C:\Users\Katayoon\anaconda3\lib\site-packages\setuptools\__init__.py", line 143, in setup
    _install_setup_requires(attrs)
  File "C:\Users\Katayoon\anaconda3\lib\site-packages\setuptools\__init__.py", line 138, in _install_setup_requires
    dist.fetch_build_eggs(dist.setup_requires)
  File "C:\Users\Katayoon\anaconda3\lib\site-packages\setuptools\dist.py", line 721, in fetch_build_eggs
    replace_conflicting=True,
  File "C:\Users\Katayoon\anaconda3\lib\site-packages\pkg_resources\__init__.py", line 783, in resolve
    replace_conflicting=replace_conflicting
  File "C:\Users\Katayoon\anaconda3\lib\site-packages\pkg_resources\__init__.py", line 1066, in best_match
    return self.obtain(req, installer)
  File "C:\Users\Katayoon\anaconda3\lib\site-packages\pkg_resources\__init__.py", line 1078, in obtain
    return installer(requirement)
  File "C:\Users\Katayoon\anaconda3\lib\site-packages\setuptools\dist.py", line 777, in fetch_build_egg
    return fetch_build_egg(self, req)
  File "C:\Users\Katayoon\anaconda3\lib\site-packages\setuptools\installer.py", line 130, in fetch_build_egg
    raise DistutilsError(str(e))
distutils.errors.DistutilsError: Command '['C:\\Users\\Katayoon\\anaconda3\\pythonw.exe', '-m', 'pip', '--disable-pip-version-check', 'wheel', '--no-deps', '-w', 'C:\\Users\\Katayoon\\AppData\\Local\\Temp\\tmpvereplk2', '--quiet', 'setuptools_scm']' returned non-zero exit status 1.
----------------------------------------

WARNING: Discarding git+https://github.com/NeuroDiffGym/neurodiffeq-conditions. Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
WARNING: You are using pip version 21.0.1; however, version 21.1 is available.
You should consider upgrading via the 'C:\Users\Katayoon\anaconda3\pythonw.exe -m pip install --upgrade pip' command.`

from neurodiffeq.

shuheng-liu avatar shuheng-liu commented on May 28, 2024

Can you try

pip install -U pip

and

pip install -U setuptools

before install the neurodiffeq-conditions package?

from neurodiffeq.

katayooneshkofti avatar katayooneshkofti commented on May 28, 2024

@katayooneshkofti
Sure, I will try it now.

from neurodiffeq.

katayooneshkofti avatar katayooneshkofti commented on May 28, 2024

@shuheng-liu
Unfortunately, the same error occurs.

from neurodiffeq.

shuheng-liu avatar shuheng-liu commented on May 28, 2024

What if you try this?

git clone https://github.com/NeuroDiffGym/neurodiffeq-conditions.git 
cd neurodiffeq-conditions
pip install -e .

I'm not quite familiar with windows. This is almost certainly a pip/setuptools issue. Unfortunately, I'm not quite familiar with installation on windows. We do have someone in the team who use windows for development; but for now, simply copying all code in this file and inserting them in your code should do the trick. You won't need to import ComposedCondition and ConditionComponent then. But you need to rename occurrences of Condition to ComposedCondition and Component to ConditionComponent in the example code I gave.

from neurodiffeq.

katayooneshkofti avatar katayooneshkofti commented on May 28, 2024

@shuheng-liu
Thank you so much for your prompt response. I am searching in github to find any solution for this error and if I find sth that works for me, I will certainly share it. Again thank you for your suggestions, I will try them.

from neurodiffeq.

katayooneshkofti avatar katayooneshkofti commented on May 28, 2024

@shuheng-liu
I followed your code for the problem and I defined the neural network and train_generator and valid generator as below:
nets=[FCNN(n_hidden_units=64, n_hidden_layers=4, actv=Swish), FCNN(n_hidden_units=128, n_hidden_layers=4, actv=Swish)] train_gen = Generator2D((32, 32), (1.0+1e-06, 0+1e-06), (1.5+1e-06, 1+1e-06), method='equally-spaced') valid_gen = Generator2D((32, 32), (1.0+1e-06, 0+1e-06), (1.5+1e-06, 1+1e-06), method='equally-spaced')

and after that I defined this:
solver = Solver2D( pde_system=lambda u, T, r , t: ..., conditions=[condition_T, condition_u], nets=nets , train_generator=train_gen , valid_generator=valid_gen , xy_min=[1.0 , 0] , xy_max= [1.5 , 0.5] ) solver.fit(max_epochs=1000) solution = solver.get_solution(best=True, copy=True)

but this error appears:

File "C:\Users\Katayoon\anaconda3\lib\site-packages\torch\nn\functional.py", line 1674, in linear
  ret = torch.addmm(bias, input, weight.t())

RuntimeError: mat1 dim 1 must match mat2 dim 0

I don't know what is wrong?

from neurodiffeq.

shuheng-liu avatar shuheng-liu commented on May 28, 2024

Can you paste and share your complete code and the completed error message (including the stack trace)? I don't see any issue here.

One thing you should look out for is that you should subtract EPS from the upper bound to make sure points that lie on the upper boundary are not sampled. But that's not the cause of this issue.

from neurodiffeq.

shuheng-liu avatar shuheng-liu commented on May 28, 2024

On a second look, I found out that you should specify n_input_units=2 in FCNN, otherwise it will use the default value which is 1. This could cause the shape mismatch error you encountered.

from neurodiffeq.

katayooneshkofti avatar katayooneshkofti commented on May 28, 2024

@shuheng-liu
Yes, that was the main issue. Thank you so much. There is one more error, I will work on it and if I don't get it fixed, I will ask you. Sorry I bothered you too many times.

from neurodiffeq.

katayooneshkofti avatar katayooneshkofti commented on May 28, 2024

I found out what was my new issue. the error was:
the function has no attribute shape which I traced it with this:
if len(u.shape) != 2 or len(t.shape) != 2 or u.shape[1] != 1 or t.shape[1] != 1: raise ValueError(f"Input shapes must both be (n_samples, 1) starting from neurodiffeq v0.2.0; \n" f"got {u.shape} (for dependent variable) and {t.shape} (for independent variable)" f"In most scenarios, consider reshaping inputs by x = x.view(-1, 1)\n" f"For legacy usage, try from neurodiffeq.neurodiffeq import unsafe_diff as diff")
I have derivatives of third order with respect to t and r (e.g. d3T_r2t) then to simplify that I used the following script:

def grad2T_r(T , r):
    return diff(T , r , order=2)

and then I coded this in pde_system:
(diff(grad2T_r , t)
and when I omitted this line the error disappeared. Is it possible to define 3rd order derivatives in neurodiffeq?

from neurodiffeq.

shuheng-liu avatar shuheng-liu commented on May 28, 2024

Yes, it's easy to do that.

diff(diff(T, r, order=2), t)

from neurodiffeq.

shuheng-liu avatar shuheng-liu commented on May 28, 2024

I think you misunderstood the diff function. The first argument should be a pytorch tensor (not a python function). Mathematically, the first argument is a function of the independent variable. But in python it's just a torch Tensor that are linked to the input torch Tensor in the computational graph.

from neurodiffeq.

katayooneshkofti avatar katayooneshkofti commented on May 28, 2024

I appriciate your help and thank you so much. That was my misunderstanding.

from neurodiffeq.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.