1
1
"""Formulation of the inverse Poisson problem in a square domain."""
2
2
3
+ import warnings
3
4
import requests
4
5
import torch
5
6
from io import BytesIO
7
+ from requests .exceptions import RequestException
6
8
from ... import Condition
7
9
from ... import LabelTensor
8
10
from ...operator import laplacian
9
11
from ...domain import CartesianDomain
10
12
from ...equation import Equation , FixedValue
11
13
from ...problem import SpatialProblem , InverseProblem
14
+ from ...utils import custom_warning_format
15
+
16
+ warnings .formatwarning = custom_warning_format
17
+ warnings .filterwarnings ("always" , category = ResourceWarning )
18
+
19
+
20
+ def _load_tensor_from_url (url , labels ):
21
+ """
22
+ Downloads a tensor file from a URL and wraps it in a LabelTensor.
23
+
24
+ This function fetches a `.pth` file containing tensor data, extracts it,
25
+ and returns it as a LabelTensor using the specified labels. If the file
26
+ cannot be retrieved (e.g., no internet connection), a warning is issued
27
+ and None is returned.
28
+
29
+ :param str url: URL to the remote `.pth` tensor file.
30
+ :param list[str] | tuple[str] labels: Labels for the resulting LabelTensor.
31
+ :return: A LabelTensor object if successful, otherwise None.
32
+ :rtype: LabelTensor | None
33
+ """
34
+ try :
35
+ response = requests .get (url )
36
+ response .raise_for_status ()
37
+ tensor = torch .load (
38
+ BytesIO (response .content ), weights_only = False
39
+ ).tensor .detach ()
40
+ return LabelTensor (tensor , labels )
41
+ except RequestException as e :
42
+ print (
43
+ "Could not download data for 'InversePoisson2DSquareProblem' "
44
+ f"from '{ url } '. "
45
+ f"Reason: { e } . Skipping data loading." ,
46
+ ResourceWarning ,
47
+ )
48
+ return None
12
49
13
50
14
51
def laplace_equation (input_ , output_ , params_ ):
@@ -29,35 +66,26 @@ def laplace_equation(input_, output_, params_):
29
66
return delta_u - force_term
30
67
31
68
32
- # URL of the file
33
- url = "https://github.com/mathLab/PINA/raw/refs/heads/master/tutorials/tutorial7/data/pts_0.5_0.5"
34
- # Download the file
35
- response = requests .get (url )
36
- response .raise_for_status ()
37
- file_like_object = BytesIO (response .content )
38
- # Set the data
39
- input_data = LabelTensor (
40
- torch .load (file_like_object , weights_only = False ).tensor .detach (),
41
- ["x" , "y" , "mu1" , "mu2" ],
69
+ # loading data
70
+ input_url = (
71
+ "https://github.com/mathLab/PINA/raw/refs/heads/master"
72
+ "/tutorials/tutorial7/data/pts_0.5_0.5"
42
73
)
43
-
44
- # URL of the file
45
- url = "https://github.com/mathLab/PINA/raw/refs/heads/master/tutorials/tutorial7/data/pinn_solution_0.5_0.5"
46
- # Download the file
47
- response = requests .get (url )
48
- response .raise_for_status ()
49
- file_like_object = BytesIO (response .content )
50
- # Set the data
51
- output_data = LabelTensor (
52
- torch .load (file_like_object , weights_only = False ).tensor .detach (), ["u" ]
74
+ output_url = (
75
+ "https://github.com/mathLab/PINA/raw/refs/heads/master"
76
+ "/tutorials/tutorial7/data/pinn_solution_0.5_0.5"
53
77
)
78
+ input_data = _load_tensor_from_url (input_url , ["x" , "y" , "mu1" , "mu2" ])
79
+ output_data = _load_tensor_from_url (output_url , ["u" ])
54
80
55
81
56
82
class InversePoisson2DSquareProblem (SpatialProblem , InverseProblem ):
57
83
r"""
58
84
Implementation of the inverse 2-dimensional Poisson problem in the square
59
85
domain :math:`[0, 1] \times [0, 1]`,
60
86
with unknown parameter domain :math:`[-1, 1] \times [-1, 1]`.
87
+ The `"data"` condition is added only if the required files are
88
+ downloaded successfully.
61
89
62
90
:Example:
63
91
>>> problem = InversePoisson2DSquareProblem()
@@ -83,5 +111,7 @@ class InversePoisson2DSquareProblem(SpatialProblem, InverseProblem):
83
111
"g3" : Condition (domain = "g3" , equation = FixedValue (0.0 )),
84
112
"g4" : Condition (domain = "g4" , equation = FixedValue (0.0 )),
85
113
"D" : Condition (domain = "D" , equation = Equation (laplace_equation )),
86
- "data" : Condition (input = input_data , target = output_data ),
87
114
}
115
+
116
+ if input_data is not None and input_data is not None :
117
+ conditions ["data" ] = Condition (input = input_data , target = output_data )
0 commit comments