Comments (4)
Hi, I am glad you managed to have a working code! In the Parametric Poisson Tutorial you can see how to plot also the original solution. You have to define a static function, something like:
def my_solution(self, pts):
# pts are your times
return .....
truth_solution = my_solution
We do not have support for non-analytical solution plotting at the moment. But you can always achieve it using matplotlib
, a PINA dependency. Something like:
import matplotlib.pyplot as plt
# evaluation points
pts = ...
# true solution
nn_ = solver.neural_net(pts).detach()
true_ = compute_true(pts) # here you compute your true solution
# plot
plt.subplots(1, 3, 1) # true
plt.plot(pts, true_)
plt.subplots(1, 3, 2) # neural net
plt.plot(pts, nn_)
plt.subplots(1, 3, 3) # absolute difference
plt.plot(pts, (true_-nn_).abs())
plt.show()
Hope this is helpful!
from pina.
Closing this issue, as completed! @mariaade26 If you need further help, feel free to open new issuesπππ»
from pina.
Ciao @mariaade26 ππ»
It's great to see PINNs used for scientific applications, and I hope the tutorials helped you start with PINA!
Regarding your problem, the issue is not related to the definition of an init but to the fact that you have passed the self
argument into a function (temp_equation
) where the residual must be computed. Those functions in pina expect only input
, output
, and (optionally for inverse problem modelling) parameters params
.
You can solve your problem very easily by using static variables, which are variables shared among all instances of a class, rather than being unique to each class instance. You can define a new static variable by simply calling TempPred.disturbance=...
before or after initialization. To call a static variable you simply type TempPred.disturbance
. Here is an example following a snippet of your code.
class TempPred(TimeDependentProblem):
output_variables = ['T']
temporal_domain = CartesianDomain({'t': [0.0, 10.0]})
# # ADDING disturbance (INSIDE the class)
# TempPred.disturbance = disturbance
def temp_equation(input_, output_):
dTdt = grad(output_, input_, components = ['T'], d = ['t'])
R= 0.005
C = 27800.0
#Testerna = 10.0
print(TempPred.disturbance)
disturbance_interp = np.interp(input_.extract(['t']), tspan, TempPred.disturbance)
force_term = 1/(R*C) * (disturbance_interp - output_.extract(['T']))
return dTdt - force_term
def initial_condition(input_, output_):
T0 = 15.0
return output_.extract(['T'])- T0
conditions = {
't0': Condition(location=CartesianDomain({'t': 0.0}), equation=Equation(initial_condition)),
'D': Condition(location=CartesianDomain({'t': [0.0, 500.0]}), equation=Equation(temp_equation))
}
# OR ADDING disturbance (OUTSIDE the class)
TempPred.disturbance = disturbance
problem=TempPred()
# code ...
Notice that static methods can also be updated during training. For example, suppose you want to change the disturbance at the end of every epoch before the optimization step is called. This can be simply achieved by creating a Callback
:
# update
from pytorch_lightning.callbacks import Callback
class Update(Callback):
def on_train_epoch_end(self, trainer, __):
trainer.solver.problem.__class__.disturbance = ...
For more on callbacks and where to put them have a look at the lightning documentation.
Let me know how it goes and if you like the package leave us a star βοΈ which helps us grow the community!π
from pina.
Ciao!
I added the disturbance outside the class, as you suggested and ,after fixing some errors related to the Tensors definitions, the code runs.
In addition, I would like to ask you how can I plot, at the end of the training, both the true solution and the predicted one, because right now it only plots the predicted one (which is still really badπ
).
Here is the actual code!
import numpy as np
from pina.problem import TimeDependentProblem
from pina import Plotter, Trainer, Condition
from pina.geometry import CartesianDomain
from pina.solvers import PINN
from pina.model import FeedForward
from pina.equation.equation import Equation
from pina.operators import grad
import torch
from torch.nn import Softplus
from scipy.integrate import odeint
from pina.callbacks import MetricTracker
import matplotlib.pyplot as plt
########################
#EQUAZIONE ORIGINALE
tspan = np.linspace(0, 1000, 1000)
T0 = 15.0
Text = []
with open("Testerna.csv", 'r', newline = '') as file:
for line in file:
Text.append(float(line.strip()))
# print(line.strip())
T0 = 15
p= [0.005, 27800, 1.5]
#tspan = np.linspace(0,52560,52560)
disturbance = Text[0:len(tspan)]
def model(u,t,tspan,p,disturbance_fun):
R = p[0]
C = p[1]
a = p[2]
disturbance_interp = np.interp(t,tspan, disturbance_fun)
dudt = 1/(R*C) * (disturbance_interp - u)
return dudt
sol= odeint(model, T0, tspan, args = (tspan,p, disturbance))
plt.plot(tspan, sol, 'b', label = "Tinterna", linewidth = 1)
plt.grid()
plt.show()
############################## PINN
class TempPred(TimeDependentProblem):
output_variables = ['T']
temporal_domain = CartesianDomain({'t': [0.0, 1000.0]})
#bisogna aggiungere la disturbance nella classe o fuori
#TempPred.disturbance = disturbance
def temp_equation(input_, output_):
dTdt = grad(output_, input_, components = ['T'], d = ['t'])
R= 0.005
C = 27800.0
#print(TempPred.disturbance)
disturbance_interp = np.interp(input_.extract(['t']).detach().numpy(), tspan, TempPred.disturbance)
disturbance_interp = torch.Tensor(disturbance_interp)
force_term = 1/(R*C) * (disturbance_interp - output_.extract(['T']))
return dTdt - force_term
def initial_condition(input_, output_):
T0 = 15.0
return output_.extract(['T']) - T0
conditions = {
't0': Condition(location=CartesianDomain({'t': 0.0}), equation=Equation(initial_condition)),
'D': Condition(location=CartesianDomain({'t': [0.0, 1000.0]}), equation=Equation(temp_equation))
}
TempPred.disturbance = disturbance
problem=TempPred()
problem.discretise_domain(n=1000, mode = 'random', locations=['D', 't0'])
#creazione rete neurale
model = FeedForward(
layers = [10,10],
func = torch.nn.Sigmoid,
output_dimensions = len(problem.output_variables),
input_dimensions = len(problem.input_variables)
)
pinn = PINN(problem=problem, model=model, optimizer_kwargs={'lr': 0.006})
trainer = Trainer(solver = pinn,callbacks=[MetricTracker()] , accelerator='cpu',enable_model_summary=False, max_epochs=1000)
trainer.train()
pl = Plotter()
pl.plot(solver=pinn)
from pina.
Related Issues (20)
- Not able to solve simple PDE with flux condition HOT 4
- Tutorial for Equations Class HOT 2
- None
- Using custom arguments in boundary condition HOT 2
- Adaptive Activations Functions
- TimeDependentProblem, SpatialProblem, ParametricProblem HOT 2
- Tried using spatial, time and parametric domain HOT 3
- Solver Compilation HOT 1
- Tests and Monthly Automated Tag HOT 1
- Unkown parameters in InverseProblem can't go on device HOT 3
- Large-scale experiment HOT 2
- Benchmarks HOT 1
- KS mat generation HOT 3
- Residual-based attention PINN
- `add_points` assigns wrong labels to new data HOT 3
- Input points help needed HOT 2
- Problem in the example of parametric elliptic control problem HOT 1
- Cannot track mean_loss HOT 1
- .cpu() method for LabelTensor HOT 1
- Add tests for MetricTracker
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pina.