Skip to content

Cavity flow PINNs example #8

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Cavity flow PINNs example #8

wants to merge 2 commits into from

Conversation

conordaly0
Copy link
Member

No description provided.

Copy link
Collaborator

@mmarkows17 mmarkows17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Conor, thank you for putting this together!

My biggest suggestion is to add more description/explanation of what is happening in the architecture setup. The rest are smaller comments for your consideration.


% Add anchor functions to strictly enforce boundary conditions on the
% streamfunction.
net = addLayers(net, [functionLayer(@(x)4.*x.*(1-x), Name="anchorX", Acceleratable=true); multiplicationLayer(3, Name="psi")]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly here, I think this needs some more explanation. This function is chosen to satisfy the boundary conditions? Can you give a short description about what the multiplicationLayer and connectLayers do?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I'll add some more explanation here. The main idea is to ensure that the boundary conditions for psi are always satisfied.


% Compute gradients.
psisum = sum(psi,1);
u = dlgradient(psisum, yeq, EnableHigherDerivatives=true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use dljacobian?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose it would mean you'd need at least R2024b. Otherwise I expect it should be ok - it'll do the sum(psi,1) line and the EnableHigherDerivatives=true by default, at the expense of the extra dimension argument dljacobian(psi,yeq,2).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think dljacobian is cleaner here -- I'll switch to that.

Comment on lines 266 to 267
u = dlgradient(psisum, y);
v = -1.*dlgradient(psisum, x);
Copy link
Collaborator

@bwdGitHub bwdGitHub Jun 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be worth making this one dlgradient call, something like

[u,v] = dlgradient(psisum,y,x);
v = -v;

I had some feeling this was faster, although I'm not completely confident. I'm sure I found it faster when I did something like batching the interior points and boundary points together and performing one dlgradient, then splitting the output back into interior and boundary gradients. There could be a few places to try that out in the loss above.

Or maybe it was the forward pass, so something like:

allx = cat(1, xeq, xyBoundary(:,1));
ally = cat(1,yeq,xyBoundary(:,2));
psi = forward(net,allx,ally);
psibd = psi(size(xeq,1)+1:end,:);
psi = psi(1:size(xeq,1), :);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting -- I like both of these ideas. I think it is best practice to mimimize the number of network executions where possible, though it comes at the expense of readability.

I'll toy around with this, but I'd like to make the loss function nice and easy to read.

@conordaly0
Copy link
Member Author

Hi Conor, thank you for putting this together!

My biggest suggestion is to add more description/explanation of what is happening in the architecture setup. The rest are smaller comments for your consideration.

Thanks Mae!



In order to automatically satisfy the continuity equation we use the stream function psi such that `u=psi_y` and `v=-psi_x`. The boundary conditions are `(u,v)=(1,0)` and the top boundary and `(u,v)=(0,0)` at the other boundaries. The Reynolds number `Re=100`.
In order to automatically satisfy the continuity equation we use the stream function $\psi$, such that $u=\partial \psi /\partial y$ and $v=-\partial \psi /\partial x$. The boundary conditions are $(u,v)=(1,0)$ at the top boundary and $(u,v)=(0,0)$ at the other boundaries. Additionally, $\psi =0$ is assumed on all the boundaries. The Reynolds number is $Re=100$.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should say something like "the domain is $[0,1] \times [0,1]$" before referring to boundaries so that it's clear what the boundary is.

psum = sum(p,1);
px = dlgradient(psum, xeq, EnableHigherDerivatives=true);
py = dlgradient(psum, yeq, EnableHigherDerivatives=true);
u = dljacobian(psi', yeq, 1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If psi and yeq are both "batch x features" I think u = dljacobian(psi, yeq, 2) would work here. But then u is "hidden x batch x features" where hidden==size(psi,2), and since you end up wanting more derivatives along that "hidden" dimension, I guess you have to permute or swap to dljacobian(u,xeq,1) anyway. I think having stuff as "features x batch" works out more nicely for me typically, which means replacing featureInputLayer with inputLayer if you want to use unlabelled dlarray.

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

Successfully merging this pull request may close these issues.

3 participants