Now what we are going to do is to start the the major well class in which we will define the.
Data even defined the loss, the loss method and even the function.
But for now, what we need to do is start this class that will have everything.
But what we will do is we will explain how to input the data.
So we need to input the data, which is basically our the data is our boundary condition and the initial
condition.
So here we, I will start with class.
And the class is called Net.
And we first define the initial condition and it will have a self, self, self.
And device.
Will be.
Torch dot device and the device will be cuda.
Of course, if cuda exist.
If torch.
Dot cuda.
Is.
Uh.
Valuable.
Else.
Well.
And torch device will be.
Sibiu.
This is very common, of course.
And with that.
And divisive.
Sorry.
Here we basically define the computational.
Network or the computational necessary Internet network, the device we need to calculate.
Either it's going to be the GPU or it's going to be the CPU.
In my case, I have a well GPU and I have everything is set up regarding cuda, so I will use GPU.
But of course you can use CPU as well.
And this model is not so big.
And of course, our model, our our also network is not so not the network is not big.
The actual problem, the points we need to calculate is also not so big.
So I would say it will be like no problem regarding the use of cuda so you can use it.
The cpu.
So here self.
We start by defining.
Well, basically the.
The computational domain.
So we have X, which is we will have starting with a wave.
So after this x, what we will do is we will.
Basically from X, we have to also define the temperature, the time, and based on that, we will calculate
the other like we will fill the U.
That will be calculated.
So first let's define something like mesh.
Although the neural networks, we don't really need mesh.
It's not the way the way we do this computation is not similar to our traditional way, so it's kind
of meshless.
But while computing using mesh is kind of easy to define, so we will do it.
So here self H equals 0.1, which is the step size of.
The step size of of x and K is the step size of t x equals.
Torch.
Dot range.
From -1 to 1 plus self dot.
And step size is self dot etch.
We do the same but this time with t so t.
And self-worth came.
And self taught care.
So how this things work?
Well, first, let's let's define also the whole grid and here self dot x.
Equals torch dot.
Stack.
Torch.
Dot.
Meshgrid.
X and T.
This has to be reshaped.
Reshape.
Two and minus one and it will be transposed.
What does it mean?
Let's let's let's start doing all of this.
So basically, what we will do is let's consider it first these two.
Or maybe this one is not needed.
So x underscore test and T underscore.
And test.
And we want to print.
And exist.
And, of course.
Print test.
And see how these things change.
So here we consider 0.1.
And then here.
0.1.
And one.
SIM goes here.
With actually we have the time cannot be negative, so it has to be zero.
This has to be.
So this is how it works.
You have the the torch that will is going to be from a -1 to 1 with a step size of 0.1.
So we start from minus one and then minus well, minus 0.9 and then minus 0.8 and minus 0.7 and so on
until it gets to a zero.
This one is actually zero.
And then it will be 10.1 until it will reach one.
The reason we did from minus one to add plus or 0.1, because in Python it will not consider the last
value.
So it's it's important to add just one, one one step to get this answer and here the we do the same
and but this time it's from zero to actually one.
Okay but we have still left this one and let's understand how it works.
So we this one is basically will give us a map of the whole domain.
So let's just remove self.
So torch stack.
Torch mesh grid mesh grid.
What x?
And T and then he would reshape it and transpose it.
So.
And let's print it.
Printex.
A mistake.
Oh, sorry.
This.
So here we can see that this will yield to minus one the time of zero, minus one, the time of one
minus one, the time of two.
So it will make us a whole domain that what we need to solve, which is we need to solve X and T for
U so the our mesh will look like this.
Of course, it doesn't have to be a uniform mesh or whatever.
Even we don't really need to use mesh.
But of course using now mesh is kind of.
Make our life simpler because it's easy to just uniformly compute the points.
But of course, it's a neural network and you don't have to.
We don't really care about real mesh.
So here what we have is a basically the transpose.
So let's, let's undo a little bit, understand how what's, what's happening.
So for example, if we remove the transpose, what we will get is this way.
So we will have this network which has to for like it will have two dimensions and this is not what
we want, so we have to transpose it.
So we will have here all of this X's and then again, the, the, the X's.
So this is not what what actually we want.
So what we will do or what we have to do is to transpose this from X and of course this is the first
one is going to be the, the, the X points from -1 to 1 and the other one is going to be from 0 to
1, which is the time.
But we don't want that.
So this is why when we put transpose, what will happen is we will actually have this like kind of,
I would say, beautiful list or tensor that has this component and every component will correspond to
X and T only.
A mesh grid.
Also, like if we want to see here, we can also see.
It's always nice to play with these things, to just understand how like these things work.
But let's just comment out this one.
Maybe.
Yeah.
Okay.
We can consider like this.
So we will have X, we will have also the time and here we will have, of course, the Xs and Xs, and
it will also give us a grid with the first one is the tensor of A of the X and the other one is tensor
with the time.
And then we have to reshape it or stack it.
Basically these two tensor become one tensor and then reshape it and then transpose it.
So this is what we did in all of this thing.
Of course there are many ways to do it, but what we at the end of at the end of the day, we need.
Something that looks like this.
We need X and T.
X for all the components of x and then T with all the time steps we have to take.
It's very important.
Okay, so now we did that.
What we have to do now.
We have to start with basically or like continue assigning the the data.
So all of this is basically, well, I will say computation domain.
Domain.
But but of course, we don't really need, um, really, you know, like actual mesh because we're dealing
with neural network and train data.
Well, we start defining them.
Basically.
This is the most important topic now.
They have two boundary condition.
One boundary is a.
That will the left part of the X and the other boundary is the right part of X.
And and of course, we have initial conditions.
The way it works in here, what will have this is not really okay.
So what what we will have is basically we will have X equals zero and then we will have a line and then
you will have X equals a no, sorry.
It's minus one.
I think -1 to 1.
Yeah.
And then you will have a start from zero and then you will have a function, a sine function.
So it will change from -1 to 1 and the sine function will have this u values.
And as the computation progress.
So this is the initial condition.
Maybe put it, let's use another color actually as the solution progress.
It will give us a shape that looks like this.
This is what this is the the general solution of this burgers.
Of course, here t t equals something like, I don't know, like we have from 0 to 1.
We said so let's say one.
So.
As a solution progress.
It will a little bit diffuse and it will have this wave well behavior.
So this is basic burgers equation and it's just very common.
And but we need to get it with neural network.
Of course, this solution.
So let's define this.
We have starting zero here and ending zero.
And in between we need to assign a sign.
We need to assign the initial condition to be sine like sine function.
So.
Boundary condition one is torch dot.
Well, basically we will do the same.
But anyway, let's write it.
Let's write it.
Dot stack, torch dot mesh grid.
And here is X.
Zero and T.
Why?
X0 because simply we we just interested in the the first component.
This is boundary condition.
So with time it has to remain the same boundary conditions.
Of course we can change it if we want, but in this case what we our aim is the boundary condition will
be zero all the way.
So dot reshape.
And this two minus one.
Same shape.
Same like.
Same.
Same way of assigning this way.
Boundary condition to.
Same thing.
But this one, what it has to be the last element.
So minus one means it's of course it doesn't mean the value.
It means the final element has to be this.
So this this way is we're creating the tensor of the boundary condition if we want to see it.
Just like quickly of like if we want, we can.
Like, Of course.
Here we already printed.
Maybe this one just to make sure.
Test.
Expressed.
And print this thing.
So we can see that the starting of this is minus one, minus one.
And of course, the time progress, if we want the last one, the last element, we will just put here
minus one and here minus one.
And then you will get the last element and the evolution over the time.
And that's it.
So we continue.
So now we have the boundary condition and.
We we need also the initial condition with it's going to be.
The same thing or the same concept, but we have to be careful about X.
Is its initial condition.
So all x but now the time will be only.
Not the first The zero, which is the first time.
And.
Oh, and for all the excess.
Okay, we're done.
Not yet.
Just a little bit more.
So self dot x x train not.
Yeah.
X underscore train.
Each train will equal torch dot concatenation for.
BC1.
BC2.
And see which is the initial condition.
This is what we defined.
Only the x.
These are the points that the boundary condition points and the initial condition points.
What we need to consider now is the values of the Y.
So y pc one equals sorry, not this way.
Y underscore pc one equals torch dot.
Zeros.
Length of.
A PC one.
So we assign all the values of this to be.
Basically zero or it is zero.
We do the same too.
We said it.
It gets to zero.
I'm not sure it's needed.
But anyway, like, this way, you can see that from here all the way to here, it's always has to be
zero.
This one all the way to here, it's always has to zero and initial condition only the first part.
So this is why we assign zero A, y, C equals.
Torch.
This one is a bit.
A little bit harder, but.
In this example, we need this start from positive.
So let's say minus torch.
Sign.
Math.
Dot pi.
Multiplied.
I see.
But.
I see.
Of what only.
Only the X elements.
So we take the X elements, we take it from here, and then we multiply it with a Pi.
This is actually useful because we said we took math though math pi dot c and we take the sine of the
whole thing and we need minus just to well, draw this kind of starting from a positive.
If you didn't put it it will maybe it will have a different um like picture rather than having the,
the starting of sine like this, it will start like this.
So anyway you have to put this, it's this y value.
To measure the value you need.
Okay, So after that, what we need to do, we need to self.
Solve dot.
Y train equals the same thing.
For.
Why boundary condition?
One Why Boundary condition two and why the initial condition?
And the only thing we need to do is I will explain it a little bit more elaborately.
It is basically is we need to have unsqueeze and.
Squeeze.
With one.
So unsqueezed.
What does it mean?
Why we need it?
We need it because you need all of this.
Let's remove these prints.
And let's.
Let's keep it like this, okay?
So let's let's do unsqueeze.
So let's consider why train this one?
And we call it test equals torch.
Just give it a tensor tensors and it will have.
A one, two, three, four.
So when let's print it.
Print this thing.
And then.
Do the Unsqueeze.
Which is this one.
And see how it will change the dimension.
So you will have what is printing.
There is a print statement.
I don't want it.
Yeah, this one.
Just some of it.
So here what we will have is we will have a tensor that has a list or a vector.
It will have one, two, three.
And this is not what we want.
We want the whole thing to have the dimension of everyone will be a list by itself because all these
work or let's say here we can see the X.
Will have this dimension.
So every, um, every row will represent one data point.
So we needed to match this one.
So this is why we did basically the Unsqueeze thing.
To be like this.
Anyway, this all code is going to be only for testing or just to show how things are defined.
And I will keep it, but I will just comment it out.
So.
By this time.
We already have the data.
We already have.
The X data which is made from the boundary condition and made from the initial condition.
And then we have the Y data, which is also defining the boundary condition and the initial condition.
This is Y, this is where we initialize it and this is in the in the in the when we're dealing with
neural network, it will consider the training data.
Next, we have to assign the other things like we'll talk about losses and optimizer and so on.