-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
66 lines (52 loc) · 2.26 KB
/
utils.py
File metadata and controls
66 lines (52 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import torch as th
from examples import Example_Longtime2012, create_ppd, example2ppd
from loss import vertify_lossfuncs
from network import CombinedNet
from PhysicalProcessDataClass import combine, read_DataDF
def plot_diff(
exam: Example_Longtime2012, trained_combinedNet: CombinedNet, savedir: "str"
):
stokes_ppd, darcy_ppd = example2ppd(exam)
pred_stokes_ppd, pred_darcy_ppd = create_ppd(
"pred_" + exam.__name__,
exam.NS_area,
exam.Darcy_area,
trained_combinedNet.NS_th,
trained_combinedNet.Darcy_th,
)
SD_ppd = combine(stokes_ppd, darcy_ppd)
SD_ppd.plot(save_dir=savedir, title="真实的数据")
pred_SD_ppd = combine(pred_stokes_ppd, pred_darcy_ppd)
pred_SD_ppd.plot(save_dir=savedir, title="预测的数据")
diff_DF = SD_ppd.DataDF.copy()
diff_DF[["U", "V", "P"]] = (SD_ppd.DataDF - pred_SD_ppd.DataDF)[["U", "V", "P"]]
diff_ppd = read_DataDF(diff_DF, name="真解与预测解之差")
diff_ppd.plot(save_dir=savedir)
def test_loss(exam: Example_Longtime2012, trained_combinedNet: CombinedNet):
stokes_UVP_func = trained_combinedNet.NS_th
darcy_UVP_func = trained_combinedNet.Darcy_th
eqn_dict = vertify_lossfuncs(exam, stokes_UVP_func, darcy_UVP_func)
eqnloss_dict = {k: v.abs().mean(dim=0).sum() for k, v in eqn_dict.items()}
stokes_ppd, darcy_ppd = example2ppd(exam)
pred_stokes_ppd, pred_darcy_ppd = create_ppd(
"pred_" + exam.__name__,
exam.NS_area,
exam.Darcy_area,
trained_combinedNet.NS_th,
trained_combinedNet.Darcy_th,
)
phyloss = dict()
for true_ppd, pred_ppd, name in zip(
[stokes_ppd, darcy_ppd], [pred_stokes_ppd, pred_darcy_ppd], ["stokes", "darcy"]
):
phy_names = ["U", "V", "P"]
diffUVP: th.Tensor = th.from_numpy(
pred_ppd.DataDF[phy_names].to_numpy()
) - th.from_numpy(true_ppd.DataDF[phy_names].to_numpy())
_loss = diffUVP.square().mean(dim=0)
for i, pn in enumerate(phy_names):
phyloss[f"{name}_{pn}_loss"] = _loss[i]
for name, loss in dict(**eqnloss_dict, **phyloss).items():
print(f"{name}: {loss:g}")
total_loss = sum(phyloss.values()) + sum(eqnloss_dict.values())
print(f"\nTotalLoss:{total_loss:g}")