From de30e87ee0a54fec31bacc45d952b83788e254fe Mon Sep 17 00:00:00 2001 From: Lynn Dang Date: Thu, 12 Oct 2023 18:06:05 +0800 Subject: [PATCH] Modify torchFunction.H and make examples/df0DFoam/zeroD_cubicReactor/H2/pytorchIntegrator invoke the new neural network --- .../H2/pytorchIntegrator/data_in_mean.npy | Bin 0 -> 164 bytes .../H2/pytorchIntegrator/data_in_std.npy | Bin 0 -> 164 bytes .../H2/pytorchIntegrator/data_target_mean.npy | Bin 0 -> 152 bytes .../H2/pytorchIntegrator/data_target_std.npy | Bin 0 -> 152 bytes .../H2/pytorchIntegrator/inference.py | 161 +++++++++--------- .../H2/pytorchIntegrator/mechanisms | 1 - src/dfChemistryModel/torchFunctions.H | 65 ++----- 7 files changed, 98 insertions(+), 129 deletions(-) create mode 100644 examples/df0DFoam/zeroD_cubicReactor/H2/pytorchIntegrator/data_in_mean.npy create mode 100644 examples/df0DFoam/zeroD_cubicReactor/H2/pytorchIntegrator/data_in_std.npy create mode 100644 examples/df0DFoam/zeroD_cubicReactor/H2/pytorchIntegrator/data_target_mean.npy create mode 100644 examples/df0DFoam/zeroD_cubicReactor/H2/pytorchIntegrator/data_target_std.npy delete mode 120000 examples/df0DFoam/zeroD_cubicReactor/H2/pytorchIntegrator/mechanisms diff --git a/examples/df0DFoam/zeroD_cubicReactor/H2/pytorchIntegrator/data_in_mean.npy b/examples/df0DFoam/zeroD_cubicReactor/H2/pytorchIntegrator/data_in_mean.npy new file mode 100644 index 0000000000000000000000000000000000000000..b4f3bdb81e40f158a8e492e2907bb2e906a3ab10 GIT binary patch literal 164 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZlV+l>qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= zXCxM+0{I%2I+{8PwF(pfu2*UWE?HYU-Cw8`8~|b083ADWp7n?QFVsHlui>4(4*-jq BFC72? literal 0 HcmV?d00001 diff --git a/examples/df0DFoam/zeroD_cubicReactor/H2/pytorchIntegrator/data_in_std.npy b/examples/df0DFoam/zeroD_cubicReactor/H2/pytorchIntegrator/data_in_std.npy new file mode 100644 index 0000000000000000000000000000000000000000..735bd95c427b159c981a08ff5a9d060febaa3960 GIT binary patch literal 164 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZlV+l>qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= zXCxM+0{I%2I+{8PwF(pfF5MYZT@EeW=>Fr7yu(U=35P8?>JGjJ5)OO*f3rVOz~xYA HKGzNaTqoAIaUsO_*m=~X4l#&V(cT3DEP6dh= vXCxM+0{I$dI+{8PwF(pfF0pTW?6OX5w)0r8Xur{Zx80)$QF{$|*4P37cXuYl literal 0 HcmV?d00001 diff --git a/examples/df0DFoam/zeroD_cubicReactor/H2/pytorchIntegrator/data_target_std.npy b/examples/df0DFoam/zeroD_cubicReactor/H2/pytorchIntegrator/data_target_std.npy new file mode 100644 index 0000000000000000000000000000000000000000..21b8c7ba424d0e6ec2a0a6ac9c6a0496dcff261c GIT binary patch literal 152 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZlV+l>qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= uXCxM+0{I$dI+{8PwF(pft{r{F_AAR**uR(AWd9^%i9Mf;i+zascRK)e4<+;f literal 0 HcmV?d00001 diff --git a/examples/df0DFoam/zeroD_cubicReactor/H2/pytorchIntegrator/inference.py b/examples/df0DFoam/zeroD_cubicReactor/H2/pytorchIntegrator/inference.py index 0aec93c8a..d6946bce1 100644 --- a/examples/df0DFoam/zeroD_cubicReactor/H2/pytorchIntegrator/inference.py +++ b/examples/df0DFoam/zeroD_cubicReactor/H2/pytorchIntegrator/inference.py @@ -1,4 +1,3 @@ -print ("Entering the first line") from builtins import Exception, print from calendar import prcal import torch @@ -11,7 +10,7 @@ import torch.profiler import os -print("Entering Inference") + torch.set_printoptions(precision=10) @@ -31,23 +30,21 @@ def json2Parser(json_path): args = json.load(f) return edict(args) - -class Net(torch.nn.Module): - def __init__(self): - super(Net, self).__init__() - neurons = layers - self.depth = len(neurons) - 1 - self.actfun = MyGELU() - self.layers = [] - for i in range(self.depth - 1): - self.layers.append(torch.nn.Linear(neurons[i], neurons[i + 1])) - self.layers.append(self.actfun) - self.layers.append(torch.nn.Linear(neurons[-2], neurons[-1])) # last layer - self.fc = torch.nn.Sequential(*self.layers) +class NN_MLP(torch.nn.Module): + def __init__(self, layer_info): + super(NN_MLP, self).__init__() + self.net = torch.nn.Sequential() + n = len(layer_info) - 1 + for i in range(n - 1): + self.net.add_module('linear_layer_%d' %(i), torch.nn.Linear(layer_info[i], layer_info[i + 1])) + self.net.add_module('gelu_layer_%d' %(i), torch.nn.GELU()) + if i <= 2: + self.net.add_module('batch_norm_%d' %(i), torch.nn.BatchNorm1d(layer_info[i + 1])) + self.net.add_module('linear_layer_%d' %(n - 1), torch.nn.Linear(layer_info[n - 1], layer_info[n])) def forward(self, x): - x = self.fc(x) - return x + return self.net(x) + try: #load variables from constant/CanteraTorchProperties path_r = r"./constant/CanteraTorchProperties" @@ -88,56 +85,57 @@ def forward(self, x): #glbal variable will only init once when called interperter #load parameters from json - norm0 = json2Parser(str(model1Path+"norm.json")) - norm1 = json2Parser(str(model2Path+"norm.json")) - norm2 = json2Parser(str(model3Path+"norm.json")) - setting0 = json2Parser(str(model1Path+"settings.json")) - lamda = setting0.power_transform - delta_t = setting0.delta_t - dim = setting0.dim - layers = setting0.layers + lamda = 0.1 + delta_t = 1e-06 + dim = 9 + #layers = setting0.layers + + Xmu0 = np.load('data_in_mean.npy') + Xstd0 = np.load('data_in_std.npy') + Ymu0 = np.load('data_target_mean.npy') + Ystd0 = np.load('data_target_std.npy') + + Xmu0 = torch.tensor(Xmu0).unsqueeze(0).to(device=device) + Xstd0 = torch.tensor(Xstd0).unsqueeze(0).to(device=device) + Ymu0 = torch.tensor(Ymu0).unsqueeze(0).to(device=device) + Ystd0 = torch.tensor(Ystd0).unsqueeze(0).to(device=device) + + Xmu1 = Xmu0 + Xstd1 = Xstd0 + Ymu1 = Ymu0 + Ystd1 = Ystd0 + + Xmu2 = Xmu0 + Xstd2 = Xstd0 + Ymu2 = Ymu0 + Ystd2 = Ystd0 - Xmu0 = torch.tensor(norm0.input_mean).unsqueeze(0).to(device=device) - Xstd0 = torch.tensor(norm0.input_std).unsqueeze(0).to(device=device) - Ymu0 = torch.tensor(norm0.label_mean).unsqueeze(0).to(device=device) - Ystd0 = torch.tensor(norm0.label_std).unsqueeze(0).to(device=device) + #load model + layers = [9, 6400, 3200, 1600, 800, 400, 6] - Xmu1 = torch.tensor(norm1.input_mean).unsqueeze(0).to(device=device) - Xstd1 = torch.tensor(norm1.input_std).unsqueeze(0).to(device=device) - Ymu1 = torch.tensor(norm1.label_mean).unsqueeze(0).to(device=device) - Ystd1 = torch.tensor(norm1.label_std).unsqueeze(0).to(device=device) - Xmu2 = torch.tensor(norm2.input_mean).unsqueeze(0).to(device=device) - Xstd2 = torch.tensor(norm2.input_std).unsqueeze(0).to(device=device) - Ymu2 = torch.tensor(norm2.label_mean).unsqueeze(0).to(device=device) - Ystd2 = torch.tensor(norm2.label_std).unsqueeze(0).to(device=device) + model0= NN_MLP(layers) + model1= NN_MLP(layers) + model2= NN_MLP(layers) - #load model - model0 = Net() - model1 = Net() - model2 = Net() - - path_list=os.listdir(model1Path) - for filename in path_list: - if os.path.splitext(filename)[1] == '.pt': - modelname = filename - - - if torch.cuda.is_available()==False: - check_point0 = torch.load(str(model1Path+modelname), map_location='cpu') - check_point1 = torch.load(str(model2Path+modelname), map_location='cpu') - check_point2 = torch.load(str(model3Path+modelname), map_location='cpu') - else: - check_point0 = torch.load(str(model1Path+modelname)) - check_point1 = torch.load(str(model2Path+modelname)) - check_point2 = torch.load(str(model3Path+modelname)) - - model0.load_state_dict(check_point0) - model1.load_state_dict(check_point1) - model2.load_state_dict(check_point2) + state_dict = (torch.load('Temporary_Chemical.pt'))['state_dict'] + + new_state_dict = {} + for k, v in state_dict.items(): + name = k[7:] + new_state_dict[name] = v + model0.load_state_dict(new_state_dict) + #model0.load_state_dict(state_dict) + + model1 = model0 + model2 = model0 + + model0.eval() model0.to(device=device) + model1.eval() model1.to(device=device) + model2.eval() model2.to(device=device) if len(device_ids) > 1: @@ -152,13 +150,12 @@ def inference(vec0, vec1, vec2): ''' use model to inference ''' - #args = np.reshape(args, (-1, 9)) #reshape to formed size - #vec0 = np.reshape(vec0, (-1, 24)) - #vec1 = np.reshape(vec1, (-1, 24)) - #vec2 = np.reshape(vec2, (-1, 24)) - vec0 = np.reshape(vec0, (-1, 10)) + vec0 = np.reshape(vec0, (-1, 10)) # T, P, Yi(7), Rho vec1 = np.reshape(vec1, (-1, 10)) vec2 = np.reshape(vec2, (-1, 10)) + vec0[:,1] *= 101325 + vec1[:,1] *= 101325 + vec2[:,1] *= 101325 try: with torch.no_grad(): @@ -166,21 +163,21 @@ def inference(vec0, vec1, vec2): input1_ = torch.from_numpy(vec1).double().to(device=device) #cast ndarray to torch tensor input2_ = torch.from_numpy(vec2).double().to(device=device) #cast ndarray to torch tensor + # pre_processing rho0 = input0_[:, -1].unsqueeze(1) input0_Y = input0_[:, 2:-1].clone() input0_bct = input0_[:, 0:-1] input0_bct[:, 2:] = (input0_bct[:, 2:]**(lamda) - 1) / lamda #BCT - input0_normalized = (input0_bct - Xmu0) / Xstd0 - # input0_normalized[:, -1] = 0 #set Y_AR to 0 + input0_normalized = (input0_bct - Xmu0) / Xstd0 #DimXmu0 = 9, DimXstd0 = 9, input0_bct = input0_normalized = input0_normalized.float() + rho1 = input1_[:, -1].unsqueeze(1) input1_Y = input1_[:, 2:-1].clone() input1_bct = input1_[:, 0:-1] input1_bct[:, 2:] = (input1_bct[:, 2:]**(lamda) - 1) / lamda #BCT input1_normalized = (input1_bct - Xmu1) / Xstd1 - # input1_normalized[:, -1] = 0 #set Y_AR to 0 input1_normalized = input1_normalized.float() rho2 = input2_[:, -1].unsqueeze(1) @@ -188,31 +185,39 @@ def inference(vec0, vec1, vec2): input2_bct = input2_[:, 0:-1] input2_bct[:, 2:] = (input2_bct[:, 2:]**(lamda) - 1) / lamda #BCT input2_normalized = (input2_bct - Xmu2) / Xstd2 - # input2_normalized[:, -1] = 0 #set Y_AR to 0 input2_normalized = input2_normalized.float() + + #inference + output0_normalized = model0(input0_normalized) output1_normalized = model1(input1_normalized) output2_normalized = model2(input2_normalized) - + + # post_processing - output0_bct = (output0_normalized * Ystd0 + Ymu0) * delta_t + input0_bct - output0_Y = (lamda * output0_bct[:, 2:] + 1)**(1 / lamda) + #output0_bct = (output0_normalized * Ystd0 + Ymu0) * delta_t + input0_bct + output0_bct = output0_normalized * Ystd0 + Ymu0 + input0_bct[:, 2:-1] + output0_Y = input0_Y.clone() + output0_Y[:, :-1] = (lamda * output0_bct + 1)**(1 / lamda) output0_Y = output0_Y / torch.sum(input=output0_Y, dim=1, keepdim=True) output0 = (output0_Y - input0_Y) * rho0 / delta_t output0 = output0.cpu().numpy() - output1_bct = (output1_normalized * Ystd1 + Ymu1) * delta_t + input1_bct - output1_Y = (lamda * output1_bct[:, 2:] + 1)**(1 / lamda) + + output1_bct = output1_normalized * Ystd1 + Ymu1 + input1_bct[:, 2:-1] + output1_Y = input1_Y.clone() + output1_Y[:, :-1] = (lamda * output1_bct + 1)**(1 / lamda) output1_Y = output1_Y / torch.sum(input=output1_Y, dim=1, keepdim=True) - output1 = (output1_Y - input1_Y) * rho1 / delta_t + output1 = (output1_Y - input1_Y) * rho1 / delta_t output1 = output1.cpu().numpy() - output2_bct = (output2_normalized * Ystd2 + Ymu2) * delta_t + input2_bct - output2_Y = (lamda * output2_bct[:, 2:] + 1)**(1 / lamda) + output2_bct = output2_normalized * Ystd2 + Ymu2 + input2_bct[:, 2:-1] + output2_Y = input2_Y.clone() + output2_Y[:, :-1] = (lamda * output2_bct + 1)**(1 / lamda) output2_Y = output2_Y / torch.sum(input=output2_Y, dim=1, keepdim=True) - output2 = (output2_Y - input2_Y) * rho2 / delta_t + output2 = output2_Y - output2_Y output2 = output2.cpu().numpy() result = np.append(output0, output1, axis=0) diff --git a/examples/df0DFoam/zeroD_cubicReactor/H2/pytorchIntegrator/mechanisms b/examples/df0DFoam/zeroD_cubicReactor/H2/pytorchIntegrator/mechanisms deleted file mode 120000 index b7afede27..000000000 --- a/examples/df0DFoam/zeroD_cubicReactor/H2/pytorchIntegrator/mechanisms +++ /dev/null @@ -1 +0,0 @@ -../../../../../mechanisms \ No newline at end of file diff --git a/src/dfChemistryModel/torchFunctions.H b/src/dfChemistryModel/torchFunctions.H index bf130c60a..1639d2870 100644 --- a/src/dfChemistryModel/torchFunctions.H +++ b/src/dfChemistryModel/torchFunctions.H @@ -1,5 +1,6 @@ template template + void Foam::dfChemistryModel::getGPUProblems ( const DeltaTType &deltaT, @@ -18,15 +19,15 @@ void Foam::dfChemistryModel::getGPUProblems scalar rhoi = rho_[cellI]; // if T < 700, set RR=0 - // if (T_[cellI] < 700) - // { - // Qdot_[cellI] = 0; - // for (int i = 0; i < mixture_.nSpecies(); i++) - // { - // RR_[i][cellI] = 0.0; - // } - // continue; - // } + if (T_[cellI] < 700) + { + Qdot_[cellI] = 0; + for (int i = 0; i < mixture_.nSpecies(); i++) + { + RR_[i][cellI] = 0.0; + } + continue; + } // set problems GpuProblem problem(mixture_.nSpecies()); @@ -40,46 +41,10 @@ void Foam::dfChemistryModel::getGPUProblems } problem.rhoi = rhoi; - // choose DNN module - if (((Qdot_[cellI] < 3e7) && (T_[cellI] < 2000) && ( T_[cellI] >= 700)) || (T_[cellI] < 700))//choose1 - { - // if use CVODE - // ode_problem.Y = problem.Y; - // ode_problem.Ti = Ti; - // ode_problem.pi = pi; - // ode_problem.rhoi = rhoi; - // ode_problem.deltaT = deltaT[cellI]; - // ode_problem.cpuTime = cpuTimes_[cellI]; - // ode_problem.cellid = cellI; - // if (!(Pstream::myProcNo() % cores_)) // submaster - // { - // ode_problem.local = false; - // } - // CPUproblemList.append(ode_problem); - - // selectDNN_[cellI]=0; - // continue; - - // if use DNN - problem.DNNid = 0; - GPUproblemList.append(problem); - continue; - } - if(((Qdot_[cellI] >= 3e7) && (T_[cellI] < 2000)&&(T_[cellI] >= 700))||((Qdot_[cellI] > 7e8) && T_[cellI] > 2000)) //choose2 - { - problem.DNNid = 1; - GPUproblemList.append(problem); - - selectDNN_[cellI]=1; - continue; - } - if ((Qdot_[cellI] < 7e8) && (T_[cellI] >= 2000) && (Qdot_[cellI]!=0)) //choose3 - { - problem.DNNid = 2; - GPUproblemList.append(problem); - selectDNN_[cellI]=2; - continue; - } + problem.DNNid = 0; + GPUproblemList.append(problem); + selectDNN_[cellI]=0; + continue; } @@ -244,4 +209,4 @@ void Foam::dfChemistryModel::updateSolutionBuffer solutionList.clear(); } return; -} \ No newline at end of file +}