You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
228 lines
9.4 KiB
228 lines
9.4 KiB
|
|
import torch
|
|
import numpy as np
|
|
import h5py
|
|
|
|
def torch_savemodel(model, epoch, optimizer, loss, path):
|
|
# torch.save(model, "{}/model_epoch{}.pth".format(model_dir , e))
|
|
torch.save({
|
|
'epoch': epoch,
|
|
'model_state_dict': model.state_dict(),
|
|
'optimizer_state_dict': optimizer.state_dict(),
|
|
'loss': loss,
|
|
}, path)
|
|
|
|
|
|
def process_rhodata(filename, data_shape, device):
|
|
x_3d = np.loadtxt(filename, delimiter=',', dtype=np.float32)
|
|
data_size = x_3d.shape[0]
|
|
x_3d = x_3d.reshape((data_size, 1, data_shape, data_shape, data_shape))
|
|
# input_data = torch.from_numpy(x_3d[1:-1]).to(device)
|
|
input_data = torch.from_numpy(x_3d).to(device)
|
|
return input_data
|
|
|
|
|
|
def process_refdata(filename, data_shape, device):
|
|
data = np.loadtxt(filename, delimiter=',', dtype=np.float32)
|
|
data_size = int(data.shape[0] / 6)
|
|
tmp = data.reshape(data_size, 6, -1)
|
|
output1 = torch.from_numpy(tmp).to(device).permute((0, 2, 1))
|
|
x_ = tmp[:, :, ::3]
|
|
y_ = tmp[:, :, 1::3]
|
|
z_ = tmp[:, :, 2::3]
|
|
img18 = np.concatenate([x_, y_, z_], 1)
|
|
img18_3d = img18.reshape(data_size, 18, data_shape, data_shape, data_shape)
|
|
|
|
# ref = torch.from_numpy(img18_3d[1:-1]).to(device)
|
|
ref = torch.from_numpy(img18_3d).to(device)
|
|
return ref
|
|
|
|
|
|
def compute_DH(input_data, ref):
|
|
# input_data torch cpu
|
|
size = input_data.shape[0]
|
|
ref18 = ref.contiguous().view(size, 18, -1)
|
|
map0 = ref18[:, 0::6].permute((0, 2, 1)).contiguous().view(size, -1, 1)
|
|
map1 = ref18[:, 1::6].permute((0, 2, 1)).contiguous().view(size, -1, 1)
|
|
map2 = ref18[:, 2::6].permute((0, 2, 1)).contiguous().view(size, -1, 1)
|
|
map3 = ref18[:, 3::6].permute((0, 2, 1)).contiguous().view(size, -1, 1)
|
|
map4 = ref18[:, 4::6].permute((0, 2, 1)).contiguous().view(size, -1, 1)
|
|
map5 = ref18[:, 5::6].permute((0, 2, 1)).contiguous().view(size, -1, 1)
|
|
ref_map = torch.cat([map0, map1, map2, map3, map4, map5], 2)
|
|
ref_U = ref_map[:, edofMat, :]
|
|
|
|
DHs_ref = []
|
|
for idx in range(size):
|
|
DH = np.zeros((6, 6))
|
|
x_vec = input_data[idx].view(-1).numpy()
|
|
for iele in range(nele):
|
|
uie = ref_U[idx, iele].detach().numpy()
|
|
De = D0 * x_vec[iele]
|
|
DH = DH + De @ (I * h ** 3 - intB @ uie)
|
|
DHs_ref.append(DH)
|
|
DHs_ref_np = np.array(DHs_ref, dtype=np.float32)
|
|
return DHs_ref_np
|
|
|
|
|
|
def load_dataset(dtype, U_flag, DH_flag, val_flag):
|
|
ddir = "alldata/"
|
|
dh_idx = [0, 1, 2, 7, 8, 14, 21, 28, 35]
|
|
|
|
if dtype == "frame128" or dtype == "tpms2" or dtype == "frame2_train" or dtype == "frame2_val" or dtype == "RSV" or dtype == "lhs_p" or dtype == "lhs_pp" or dtype == "lhs_small" or dtype == "pcg_lhs" or dtype == "rsv_small" or dtype == "new_frame" or dtype == "lhs" or dtype == "randgeo" or dtype == "randgrey":
|
|
load_data = np.load(ddir + dtype + ".npz")
|
|
rho = load_data['x0']
|
|
rho = torch.from_numpy(rho)
|
|
ret = [rho]
|
|
if U_flag:
|
|
displacement = load_data['y0']
|
|
displacement = torch.from_numpy(displacement)
|
|
ret.append(displacement)
|
|
if DH_flag:
|
|
load_data_DH = np.load(ddir + dtype + "_DH.npy")
|
|
if DH_flag == 2:
|
|
load_data_DH = np.array(load_data_DH, dtype=np.float32)
|
|
else:
|
|
load_data_DH = np.array(load_data_DH.reshape(-1, 36)[:, dh_idx], dtype=np.float32)
|
|
DH = torch.from_numpy(load_data_DH)
|
|
ret.append(DH)
|
|
return ret
|
|
|
|
if dtype == "tpms":
|
|
load_data = np.load(ddir + "TPMS.npz")
|
|
|
|
rho = np.concatenate((load_data['data_g'], load_data['data_p'], load_data['data_iwp']), 0)
|
|
rho = torch.from_numpy(rho)
|
|
|
|
if DH_flag:
|
|
load_data_DH = np.load(ddir + "TPMS_DH.npy")
|
|
|
|
load_data_DH = np.array(load_data_DH.reshape(-1, 36)[:, dh_idx], dtype=np.float32)
|
|
DH = torch.from_numpy(load_data_DH)
|
|
return rho, DH
|
|
if U_flag:
|
|
displacement = np.concatenate((load_data['label_g'], load_data['label_p'], load_data['label_iwp']), 0)
|
|
displacement = torch.from_numpy(displacement)
|
|
return rho, displacement
|
|
|
|
|
|
def mat2np(mat0, key):
|
|
num = mat0[key].shape[0]
|
|
data_list = []
|
|
for i in range(num):
|
|
data = np.array(mat0[mat0[key][i][0]], dtype=np.float32)
|
|
data_list.append(data)
|
|
array = np.stack(data_list,0)
|
|
return array
|
|
# print(data.shape)
|
|
|
|
|
|
data_shape = 40
|
|
|
|
I = np.eye(6)
|
|
D0 = np.array([[1.3462, 0.5769, 0.5769, 0, 0, 0],
|
|
[0.5769, 1.3462, 0.5769, 0, 0, 0],
|
|
[0.5769, 0.5769, 1.3462, 0, 0, 0],
|
|
[0, 0, 0, 0.3846, 0, 0],
|
|
[0, 0, 0, 0, 0.3846, 0],
|
|
[0, 0, 0, 0, 0, 0.3846]])
|
|
|
|
if data_shape == 20:
|
|
nele = 8000
|
|
intB = np.array([[-0.000625, 0, 0, 0.000625, 0, 0, -0.000625, 0, 0, 0.000625, 0, 0, -0.000625, 0, 0, 0.000625, 0, 0,
|
|
-0.000625, 0, 0, 0.000625, 0, 0],
|
|
[0, -0.000625, 0, 0, -0.000625, 0, 0, 0.000625, 0, 0, 0.000625, 0, 0, -0.000625, 0, 0, -0.000625,
|
|
0, 0, 0.000625, 0, 0, 0.000625, 0],
|
|
[0, 0, -0.000625, 0, 0, -0.000625, 0, 0, -0.000625, 0, 0, -0.000625, 0, 0, 0.000625, 0, 0,
|
|
0.000625, 0, 0, 0.000625, 0, 0, 0.000625],
|
|
[-0.000625, -0.000625, 0, -0.000625, 0.000625, 0, 0.000625, -0.000625, 0, 0.000625, 0.000625, 0,
|
|
-0.000625, -0.000625, 0, -0.000625, 0.000625, 0, 0.000625, -0.000625, 0, 0.000625, 0.000625, 0],
|
|
[0, -0.000625, -0.000625, 0, -0.000625, -0.000625, 0, -0.000625, 0.000625, 0, -0.000625, 0.000625,
|
|
0, 0.000625, -0.000625, 0, 0.000625, -0.000625, 0, 0.000625, 0.000625, 0, 0.000625, 0.000625],
|
|
[-0.000625, 0, -0.000625, -0.000625, 0, 0.000625, -0.000625, 0, -0.000625, -0.000625, 0, 0.000625,
|
|
0.000625, 0, -0.000625, 0.000625, 0, 0.000625, 0.000625, 0, -0.000625, 0.000625, 0, 0.000625]])
|
|
h = 0.05
|
|
else:
|
|
nele = 64000
|
|
intB = np.array([[-0.000156250, 0, 0, 0.000156250, 0, 0, -0.000156250, 0, 0, 0.000156250, 0, 0, -0.000156250, 0, 0,
|
|
0.000156250, 0, 0, -0.000156250, 0, 0, 0.000156250, 0, 0],
|
|
[0, -0.000156250, 0, 0, -0.000156250, 0, 0, 0.000156250, 0, 0, 0.000156250, 0, 0, -0.000156250, 0,
|
|
0, -0.000156250, 0, 0, 0.000156250, 0, 0, 0.000156250, 0],
|
|
[0, 0, -0.000156250, 0, 0, -0.000156250, 0, 0, -0.000156250, 0, 0, -0.000156250, 0, 0, 0.000156250,
|
|
0, 0, 0.000156250, 0, 0, 0.000156250, 0, 0, 0.000156250],
|
|
[-0.000156250, -0.000156250, 0, -0.000156250, 0.000156250, 0, 0.000156250, -0.000156250, 0,
|
|
0.000156250, 0.000156250, 0, -0.000156250, -0.000156250, 0, -0.000156250, 0.000156250, 0,
|
|
0.000156250, -0.000156250, 0, 0.000156250, 0.000156250, 0],
|
|
[0, -0.000156250, -0.000156250, 0, -0.000156250, -0.000156250, 0, -0.000156250, 0.000156250, 0,
|
|
-0.000156250, 0.000156250, 0, 0.000156250, -0.000156250, 0, 0.000156250, -0.000156250, 0,
|
|
0.000156250, 0.000156250, 0, 0.000156250, 0.000156250],
|
|
[-0.000156250, 0, -0.000156250, -0.000156250, 0, 0.000156250, -0.000156250, 0, -0.000156250,
|
|
-0.000156250, 0, 0.000156250, 0.000156250, 0, -0.000156250, 0.000156250, 0, 0.000156250,
|
|
0.000156250, 0, -0.000156250, 0.000156250, 0, 0.000156250]])
|
|
h = 0.025
|
|
|
|
|
|
|
|
|
|
def val_function(input_data_val, input_data_val_cpu, ref_val, flag):
|
|
output_val = model(input_data_val)
|
|
output_val = output_val.cpu()
|
|
|
|
err2_sum2 = torch.sum((output_val - ref_val) ** 2, [2, 3, 4])
|
|
ref_val_2 = (ref_val ** 2).sum([2, 3, 4])
|
|
relative_l2 = torch.sqrt(err2_sum2 / ref_val_2)
|
|
relative_u_ref = relative_l2.mean()
|
|
if flag:
|
|
logger['u_l2loss_ref'].append(relative_u_ref.item())
|
|
else:
|
|
logger['u_l2loss_randref'].append(relative_u_ref.item())
|
|
# val C
|
|
|
|
DHs_np = compute_DH(input_data_val_cpu, output_val)
|
|
|
|
return DHs_np, relative_u_ref
|
|
|
|
|
|
def disp2DH(input, output, D0, intB, h):
|
|
pp = input.contiguous().view(input.shape[0], -1, 1, 1)
|
|
DD = pp * D0 # [16, 64000, 6, 6]
|
|
tmp = torch.matmul(intB, output) # [16, 64000, 6, 6]
|
|
DDh = DD * (h ** 3)
|
|
tmp2 = DDh - torch.matmul(DD, tmp)
|
|
DHs = tmp2.sum(1)
|
|
return DHs
|
|
|
|
device = torch.device(f"cuda:0")
|
|
|
|
|
|
# 20
|
|
# Ke = np.loadtxt("3D/k.txt", delimiter=',', dtype=np.float32)
|
|
# Fe = np.loadtxt("3D/f.txt", delimiter=',', dtype=np.float32)
|
|
# 40
|
|
Ke = np.loadtxt("3D/k.txt", delimiter=',', dtype=np.float32) / 2
|
|
Fe = np.loadtxt("3D/f.txt", delimiter=',', dtype=np.float32) / 4
|
|
Ke = torch.from_numpy(Ke).to(device)
|
|
Fe = torch.from_numpy(Fe).to(device)
|
|
|
|
# mesh = np.loadtxt("3D/20mesh.txt", delimiter='\t', dtype=np.float32)
|
|
mesh = np.loadtxt("3D/40mesh.txt", delimiter='\t', dtype=np.float32)
|
|
tmp1 = 3 * mesh - 2
|
|
tmp2 = 3 * mesh - 1
|
|
tmp3 = 3 * mesh
|
|
edofMat = np.zeros([mesh.shape[0], 24], dtype=np.int)
|
|
edofMat[:, ::3] = tmp1
|
|
edofMat[:, 1::3] = tmp2
|
|
edofMat[:, 2::3] = tmp3
|
|
edofMat -= 1
|
|
|
|
Imask = torch.eye(6)
|
|
Imask[:3, :3] = 1
|
|
|
|
bound = 0.013462
|
|
dh_idx = [0, 1, 2, 7, 8, 14, 21, 28, 35]
|
|
|
|
D00 = torch.from_numpy(D0.astype(np.float32)).to(device)
|
|
intB2 = torch.from_numpy(intB.astype(np.float32)).to(device)
|
|
Ih = I.astype(np.float32) * h ** 3
|
|
|
|
dh_idx12 = [0, 1, 2, 6, 7, 8, 12, 13, 14, 21, 28, 35]
|
|
dh9_12 = [0, 1, 2, 1, 3, 4, 2, 4, 5, 6, 7, 8]
|