Deep XD is a library that is designed for pinns computation.
Of course, using a library is not as flexible as using PyTorch in order to achieve our like solving
the pens.
However.
It is sometimes quite useful for, let's say, sample testing or just somebody who's trying to learn
pens will just use it as a as a reference for our computation, or just to compare between our belt
code and the library.
So this library is built on.
Top of TensorFlow and actually PyTorch.
And this is its main backend and of course with, with other like other libraries that is back into
this deep.
So what we will learn today or this class is how to solve heat equation using deep.
So the first thing is well just a quick thing is here of course again like it's a temporal difference
temporal component and diffusion component.
The spatial component of of you changing with x square and k is diffusion coefficient or kinematic diffusion
coefficient.
So this is a constant.
And this is we're trying to solve you with an input of t and x.
And we need to solve this equation.
So.
How to do it the same as we always do.
So we do.
We start by, we say deep XD.
Um.
Heat or even 1d.
1D.
Heat and main, let's say.
Of course you can just name it anything you want.
And we start, as always, by importing the needed libraries.
So import of course deep x XD as d and from deep x d.
But.
Back end.
Back end import.
TensorFlow.
And import numpy as NP as always and import matplotlib dot pi.
Plot.
For plotting.
Plotting if needed.
And of course, you have to enter.
So this is basically the needed libraries for us.
We take them.
And now what we will do is we will have a first defining the constant.
And this diffusion coefficient this k.
So k equals 0.4.
Length of the domain is one, length of time is one, and this is what we are going to use like soon.
So geometry geom equals e dot.
Of course this is we have to follow the rules and how we write the code for deep.
So the geo.
Mitre.
Dot interval from zero to L.
And time domain equals dee dee dee.
Of course.
This dot.
Geometry.
Dot.
Time.
Capital letter domain also capital letter domain.
And it goes from zero to this in.
And sorry.
Not in.
Jim.
Time, which is geometry and time dot.
In geome geometry.
Basically dot.
Another method.
Geome Mitre.
X.
Time.
And Jim and the time domain.
So here we defined our geometry and we put a push shift enter and we are done.
That's it.
It's very quick very kind of shortcuts using this library however.
We need to define the boundary condition and the initial condition.
So.
And defining the boundary condition or the initial condition will require us to have a lambda function.
So well, for example, I would say I will define the initial condition.
But and then I will explain how to deal with lambda function.
And then I will define the boundary condition based on a function lambda function but also based on
external function.
So we can play with it whatever we want or however we want to define.
So.
I c equals d dot I c.
Initial condition.
And.
And initial condition and boundary condition.
Dot c means also initial condition and then we pass on the geometry.
And we define the lambda function.
And lambda.
Lambda Lambda.
And X.
NP dot sign.
In multiplied.
In p dot.
Pie.
And of course, is the length of the time domain.
Multiplied x.
Everything to.
021.
And divide everything by L.
Which is this one?
And we have to put another function lambda as do nothing function.
Uh, this way.
And the last one on.
A nickel.
On initial.
So this way what what do we have is a sine function in which we are going to apply this function.
Basically it's x its only initial condition.
And it will take x and multiply it well based on the end value and the value with a pi.
And we will take the sine of the whole thing.
And this is going to be our function.
So basically what we have so far is we have the domain.
It looks like it's a little bit small.
We have the domain that maybe if there are bigger.
Okay, this we have the domain that looks like this.
This is x and this is T.
And it goes like this.
X is from.
X is from zero to here one and t is from also zero.
So here to one here this is t.
Now the.
The the initial condition it will go it's going to be a sine wave.
So it will look like.
It.
This.
And then we will define also a boundary condition that is going to be only one at the beginning.
And at the end we will set it as.
To.
So basically this is our boundary condition.
And what does it mean.
It means that a.
Well, it's basically the.
We start the boundary condition from here, it will be zero and.
But say this.
This is the initial condition.
And the boundary condition is going to be two here.
So also at the end of this thing.
The initial condition.
The boundary condition will be zero and the here the boundary condition is going to be two.
So this is going to all the boundary condition.
And basically also here.
So we have a line.
The line in between we have to calculate.
But the boundary condition between the the beginning and the end is basically two.
So it's in the left side.
This one is always going to be 000.
And on the right side the boundary condition is going to be two, two, two.
So this is what we will do.
For the initial condition.
We already defined this as a function.
A sine function that will deal that will be related to n and l.
Of course we can remove n.
It doesn't really matter.
We can do whatever we want and as we define here is initial condition.
He will apply this equation only on the initial condition.
So he will take the whole domain and only take the boundary condition or sorry, the initial condition,
and then apply it to the equation.
And then he will get his initial condition.
Now what we need to do of course, to consider the lambda.
It's not very common for us to use lambda function.
So what we will do is or now just define using lambda function only the initial condition.
But for the boundary condition I will define it using a lambda function, but also a normal function
to basically do whatever we want, we change whatever we want.
So here we shift enter and it's already entered.
Now before that let's understand a little bit about lambda functions.
So let's imagine we have an input.
Just a little light.
Take some spaces.
So input.
Array.
Of NP dot array.
And let's consider it as let's say it's like a grid so or a mesh.
Here we have like kind of or it is 2D and.
This one.
Will be.
This four, five six.
And that we have this.
The last one will be, let's say seven, eight, nine.
Yeah.
So we have now a.
An array.
And we will define a lambda function.
So first lambda.
Function equals.
Lambda.
And then we define what we will change.
So x we multiply it by.
X only taking.
The first column.
So we take everything.
We take.
Everything.
This column and it will change from 0 to 1 means 0 to 1 is basically zero because.
While we cannot take one.
So in this case.
Results of this lambda function.
Result.
Uh.
Well.
Array.
Equals the lambda function.
And when we pass the input array.
And let's say, print.
Results.
So what's happening here?
The input array.
This part of the code we already know.
This is what db db is going to provide our to our Lambda function.
And it will pass it to basically our lambda function.
So what will happen is it will take this input array and we'll consider it.
This is going to be the x.
So we said lambda x.
So it's going to be x.
And then what will happen is we will take this x and then do this operation which is multiply.
First we we we extract the component we want which is the first column from 0 to 1 because it will change
from 0 to 1.
Let's say like the just the the first, basically the first component.
And then what we will do is we multiply it by two and that's it.
So the result will be two.
And this four multiplied by two, it will become eight and seven multiplied by two it becomes 14.
If we can do another things with lambda functions.
So if we have a conditional lambda function.
So conditional a conditional.
Well.
Lambda.
Equals.
The same thing, same structure.
So lambda x.
Lambda x, and then we change the what we want if let's say.
Uh, even, even.
If.
X.
The remaining of dividing by two is zero.
Else it's, let's say, an odd number.
Odd number and print.
Conditional lambda we pass for.
And do the same with passing.
Uh, three, let's say.
So.
He takes the four and then apply it.
So this is his X, this is his.
The value he cares about is x.
And then he will pass it through the through the system through lambda function.
And then if it's of course, if it's you know, the remainder is zero, it will just do whatever the
if condition is there and then it will just give us what, what it whatever we want.
So this is what we can say like a conditional lambda function.
Now what we want to do is we want to use the lambda function to actually pass on.
Well, basically.
Boundary condition.
It through a function.
So in order to do that.
We first define the function define function.
Let's say it is double.
We said about the from 0 to 2 the first.
Column.
Input.
Ray.
Of course, this lambda function is quite not usual for engineers and maybe scientists to use, but
this is how this is what the people who wrote the well, basically the code the deep want us to do.
So we have to to do it.
So here, for example, is.
The input array.
Input equals to input array from 0 to 1.
So we we write the same function here and.
Well, basically boundary condition equals.
The will use the same like just will change some what what is needed.
So DDE means dxt.
And here we have Di.
Ric lit.
Boundary condition.
It's also syntax geometry.
Lambda function has to be.
Well, I'll try to do it this way.
It's a little bit cleaner.
So the lambda function we need to have input array rather than x input array.
And.
Basically we pass on the function of input array, and here on we have to change it also syntax bound
dairy.
And here also on boundary.
Yeah.
This way.
So now we define the initial condition and the boundary condition using of course lambda function.
One thing to note regarding how I treated this regarding 0 to 1 is we need to to understand it fully.
We need to print what the things we are writing.
So the first thing is this is the input array that is going to be from the boundary.
The first thing is input array.
Let's see what kind of input array they are providing to this function.
So the input array we put it in here which is going to be of course our boundary condition.
And next thing is the input array.
We need to see the shape of it.
In case if it's big.
Of course it's not so big.
It's it's 80.
Multiply two.
We will say that shape and.
What we will do is also.
We put the input array in this way.
The where we are passing it to our system.
And here is the same.
And the last thing, what if we didn't do that and we just simply providing this kind of input array?
Um, yeah.
We put here zero and here zero.
So this is going to be printed as we run the our computation and.
Yeah, let's let's just keep it this way until we have our code ready and we can see how these are changing.
So basically what we need to understand or I can provide let's say the Rand software.
Is this is going to be the result.
Of course.
And this is input array.
So this is how it will look.
The input array of the the input array.
The our input array is going to look like a like this.
Basically you have a the value and you have the time.
So this is this is all a boundary values.
Of course this is we will see it when the when the program runs.
We just will not it.
But it's very important to explain it here in the in the section we are talking about the boundary condition
and of course the initial condition.
So we have the input array which is going to be all like this.
And then the input shape is 80 and to why it's 80.
Well, because again this is later we will decide how many number of boundary points we will put.
Of course these are also random points.
We are also going to see that now input.
When we when we put this thing like this we will have the input array to be looks like this.
Basically you have a list.
And inside the list we have another list and all the values that we need to change.
Basically apply the to multiply them by two.
And this is going to be the dimension is 8180 columns and 180 rows and one column, as you can see here.
However, if we put this way, if we put input array this one and zero, what we will get is we will
get a list that looks like this, which is not what we want.
This is not physics, this is simply programming.
This is how we have to write it in order to get the correct shape of the, of the of the input value.
So this is a well in detail why we put here from 0 to 1.
One thing to note.
Is there.
When we use this definition of boundary condition, we will need three needed information.
The first one is x.
The location in space.
The second one is time.
The boundary condition will end at a specific time, and the third one is the value at this boundary
condition.
So how did we well define them?
Well, this lambda function that we talked maybe a lot about how it is structured.
We can see the input array which is will give us two informations.
That is x and time the x.
We can see it here 0 or 1.
And then here 000111 at different time location that we will basically randomly generate it or going
to generate it.
So these are the two informations.
And they are going to input in this lambda function in this input array.
Now we have the two information.
How about the third information.
And the third information.
We get it by the double first column.
This basically the.
Function and how this information is, is going to be used.
Well we will this function will give us the third information, but well how we put the input array
in that function.
And we use only the space location.
As a to describe the output function which is.
In this function, the space multiplied by two will equal the boundary condition.
Let's see in the this graph.
That.
So our solution will be like this.
Basically, we will have a boundary condition and in the one direction is going to be, well zero and
the other direction which is here.
And at x equals one we will have the boundary condition of well two x basically.
And with time this initial condition will start to diffuse into its final solution.
So x and time.
Is related with X and time, we have a boundary condition value that is related with this equation.
Boundary condition value is two multiplied x.
If the x equals zero, the boundary information or the boundary condition will be zero.
Two multiplied zero is zero and at x equals one it will be two multiplied x, which is one and equals
to two.
This is why we can see this value is two.
So.
At the initial condition.
This one we will start this initial condition diffuse through this well basically the final solution
of the heat equation.
So the well just a minute here we can see that output is going to.
We will take x location.
This one only zero one which is only this value.
And we will multiply it by two which is two x will equal the boundary condition.
And we will add this as one one information which is we can see the values will be this way.
So these are the will the input array.
But if we multiply it by this is the input between 0 and 1.
But we set to multiply this input array which is.
This will be two.
This will be zero.
This will be two.
Because the location every location will be multiplied by two.
So this is the other input x and t output is an array that will take this input function and multiply
it by two.