diff --git a/src_py/magnethub/models/sydney/3C92.pt b/src_py/magnethub/models/sydney/3C92.pt index 5259b39..8dadbcd 100644 Binary files a/src_py/magnethub/models/sydney/3C92.pt and b/src_py/magnethub/models/sydney/3C92.pt differ diff --git a/src_py/magnethub/sydney.py b/src_py/magnethub/sydney.py index 39e3970..ad526d2 100644 --- a/src_py/magnethub/sydney.py +++ b/src_py/magnethub/sydney.py @@ -1,5 +1,5 @@ """ -File contains the model according to the Paderborn University approach for the magnet challenge. +File contains the model according to the Sydney University approach for the magnet challenge. Source: https://github.com/moetomg/magnet-engine """ @@ -24,83 +24,82 @@ "N87": "N87.pt", "ML95S": "ML95S.pt", } - # Material normalization data (1.B 2.H 3.F 4.T 5.dB/dt) normsDict ={"77": [[-2.63253458e-19, 7.47821754e-02], [-7.60950004e-18, 1.10664739e+01], - [5.24678898e+00, 2.89351404e-01], - [5.87473793e+01, 2.40667381e+01], - [6.16727829e+00, 3.83645439e+01]], - "78": [[5.67033925e-19, 7.22424510e-02], + [ 5.24678898e+00, 2.89351404e-01], + [ 5.87473793e+01, 2.40667381e+01], + [ 6.16727829e+00, 3.83645439e+01]], + "78": [[ 5.67033925e-19, 7.22424510e-02], [-1.54283684e-16, 1.15338828e+01], - [5.23810768e+00, 2.89979160e-01], - [5.87434082e+01, 2.40685291e+01], - [6.09561586e+00, 3.81356049e+01]], - "79": [[1.70344847e-13, 9.41321492e-02], + [ 5.23810768e+00, 2.89979160e-01], + [ 5.87434082e+01, 2.40685291e+01], + [ 6.09561586e+00, 3.81356049e+01]], + "79": [[ 1.70344847e-13, 9.41321492e-02], [-4.54025068e-02, 3.20463941e+01], - [5.21954346e+00, 2.66715437e-01], - [5.52068787e+01, 2.37196522e+01], - [6.77422905e+00, 3.90895233e+01]], - "N27": [[7.52738469e-19, 7.48951129e-02], + [ 5.21954346e+00, 2.66715437e-01], + [ 5.52068787e+01, 2.37196522e+01], + [ 6.77422905e+00, 3.90895233e+01]], + "N27": [[ 7.52738469e-19, 7.48951129e-02], [-8.97477366e-17, 1.47606605e+01], - [5.24649334e+00, 2.89964765e-01], - [5.87355194e+01, 2.40766029e+01], - [6.17841434e+00, 3.84738274e+01]], - "N30": [[1.43320465e-19, 6.56044649e-02], + [ 5.24649334e+00, 2.89964765e-01], + [ 5.87355194e+01, 2.40766029e+01], + [ 6.17841434e+00, 3.84738274e+01]], + "N30": [[ 1.43320465e-19, 6.56044649e-02], [-1.57874135e-16, 1.09083332e+01], - [5.31786680e+00, 2.78960317e-01], - [5.86466904e+01, 2.40616817e+01], - [7.01255989e+00, 4.09709969e+01]], + [ 5.31786680e+00, 2.78960317e-01], + [ 5.86466904e+01, 2.40616817e+01], + [ 7.01255989e+00, 4.09709969e+01]], "N49": [[-8.99073580e-19, 8.94479227e-02], - [4.15423721e-16, 3.70622618e+01], - [5.25545311e+00, 3.00384015e-01], - [5.94716339e+01, 2.44349327e+01], - [6.75209475e+00, 3.91901703e+01]], - "N87": [[1.72051200e-13, 6.26231476e-02], - [4.02299992e-02, 7.61060358e+00], - [5.26309967e+00, 2.87137657e-01], - [5.83059006e+01, 2.40639057e+01], - [6.53078842e+00, 3.93127785e+01]], - "3E6": [[1.01579639e-18, 7.04261607e-02], - [2.34374135e-16, 7.21573964e+00], - [5.34307003e+00, 2.66708523e-01], - [5.86578026e+01, 2.40552864e+01], - [7.23155785e+00, 4.15975838e+01]], + [ 4.15423721e-16, 3.70622618e+01], + [ 5.25545311e+00, 3.00384015e-01], + [ 5.94716339e+01, 2.44349327e+01], + [ 6.75209475e+00, 3.91901703e+01]], + "N87": [[ 1.72051200e-13, 6.26231476e-02], + [ 4.02299992e-02, 7.61060358e+00], + [ 5.26309967e+00, 2.87137657e-01], + [ 5.83059006e+01, 2.40639057e+01], + [ 6.53078842e+00, 3.93127785e+01]], + "3E6": [[ 1.01579639e-18, 7.04261607e-02], + [ 2.34374135e-16, 7.21573964e+00], + [ 5.34307003e+00, 2.66708523e-01], + [ 5.86578026e+01, 2.40552864e+01], + [ 7.23155785e+00, 4.15975838e+01]], "3F4": [[-1.75200068e-19, 5.98892952e-02], [-9.48865199e-18, 4.74414811e+01], - [5.14398336e+00, 3.04210454e-01], - [5.76523476e+01, 2.43824081e+01], - [6.23030663e+00, 3.64991379e+01]], - "T37": [[1.72051200e-13, 6.26231476e-02], - [4.02299992e-02, 7.61060358e+00], - [5.26309967e+00, 2.87137657e-01], - [5.83059006e+01, 2.40639057e+01], - [6.53078842e+00, 3.93127785e+01]], + [ 5.14398336e+00, 3.04210454e-01], + [ 5.76523476e+01, 2.43824081e+01], + [ 6.23030663e+00, 3.64991379e+01]], + "T37": [[ 1.72051200e-13, 6.26231476e-02], + [ 4.02299992e-02, 7.61060358e+00], + [ 5.26309967e+00, 2.87137657e-01], + [ 5.83059006e+01, 2.40639057e+01], + [ 6.53078842e+00, 3.93127785e+01]], "3C90":[[-3.27923689e-19, 6.56109348e-02], - [6.99196716e-17, 1.26583787e+01], - [5.19875193e+00, 2.68499136e-01], - [5.86049919e+01, 2.40574703e+01], - [6.29652929e+00, 3.84585190e+01]], + [ 6.99196716e-17, 1.26583787e+01], + [ 5.19875193e+00, 2.68499136e-01], + [ 5.86049919e+01, 2.40574703e+01], + [ 6.29652929e+00, 3.84585190e+01]], "3C92":[[-2.35520104e-13, 6.53518693e-02], - [1.18689366e-01, 1.23585692e+01], - [5.16579533e+00, 2.73998171e-01], - [5.84305267e+01, 2.40970516e+01], - [5.88209248e+00, 3.69935722e+01]], - "3C94":[[1.21232679e-19, 7.44383659e-02], + [ 1.18689366e-01, 1.23585692e+01], + [ 5.16579533e+00, 2.73998171e-01], + [ 5.84305267e+01, 2.40970516e+01], + [ 5.88209248e+00, 3.69935722e+01]], + "3C94":[[ 1.21232679e-19, 7.44383659e-02], [-2.19613879e-17, 1.18042579e+01], - [5.22766781e+00, 2.68348873e-01], - [5.87128143e+01, 2.40769634e+01], - [6.53718996e+00, 3.91955910e+01]], - "3C95":[[5.64116728e-14, 7.90115297e-02], - [1.11898437e-01, 1.29696641e+01], - [5.18842697e+00, 2.69014776e-01], - [5.86223640e+01, 2.40957470e+01], - [6.25767517e+00, 3.84026108e+01]], + [ 5.22766781e+00, 2.68348873e-01], + [ 5.87128143e+01, 2.40769634e+01], + [ 6.53718996e+00, 3.91955910e+01]], + "3C95":[[ 5.64116728e-14, 7.90115297e-02], + [ 1.11898437e-01, 1.29696641e+01], + [ 5.18842697e+00, 2.69014776e-01], + [ 5.86223640e+01, 2.40957470e+01], + [ 6.25767517e+00, 3.84026108e+01]], "ML95S":[[-1.53185180e-13, 1.15827541e-01], - [3.84426934e-01, 4.45061606e+01], - [5.21606445e+00, 2.65364528e-01], - [5.70770302e+01, 2.44398289e+01], - [7.30377579e+00, 4.04136391e+01]], + [ 3.84426934e-01, 4.45061606e+01], + [ 5.21606445e+00, 2.65364528e-01], + [ 5.70770302e+01, 2.44398289e+01], + [ 7.30377579e+00, 4.04136391e+01]], } # %% Initialize model @@ -122,9 +121,10 @@ def __init__(self, mdl_path, material): def __call__(self, data_B, data_F, data_T): """Call method.""" # ----------------------------------------------------------- batch execution - # if isinstance(data_F, np.ndarray): - # 1.Get dataloader + if data_B.ndim == 1: + data_B = np.array(data_B).reshape(1, -1) + loader = get_dataloader(data_B,data_F,data_T,self.mdl.norm) # 2.Validate the models @@ -138,12 +138,17 @@ def __call__(self, data_B, data_F, data_T): data_P = torch.cat((data_P,Pv.to(self.device)),dim=0) data_P, h_series = data_P.cpu().numpy(), h_series.cpu().numpy() + + # 3.Return results if data_P.size == 1: data_P = data_P.item() if h_series.ndim == 1: h_series = h_series.reshape(1, -1) + return data_P, h_series + + class MMINet(torch.nn.Module): """ Magnetization mechanism-determined neural network. @@ -235,7 +240,7 @@ def forward(self, x, var): H = torch.from_numpy(H).view(batch_size,-1,1) real_H = torch.cat((H[:, -self.n_init:, :],H[:, :-self.n_init, :]), dim=1) return torch.flatten(Pv).cpu(), real_H[:, :, 0].cpu() - + class StopOperatorCell(): """ MMINN Sub-layer: Static hysteresis prediction using stop operators. @@ -257,8 +262,7 @@ def __call__(self, dB, state): r = self.operator_thre.to(dB.device) output = self.sslu((dB + state)/r)*r return output.float() - - + class EddyCell(torch.nn.Module): """ MMINN subsubnetwork: Dynamic hysteresis prediction. @@ -312,12 +316,14 @@ def get_dataloader(data_B, data_F, data_T, norm, n_init=32): n_init : int Additional points for computing the history magnetization """ + # Data pre-process # 1. Down-sample to 128 points seq_length = 128 - cols = np.array(range(0,1023,8)) - data_B = data_B[:, cols] + cols = np.array(range(0,data_B.shape[1],round(data_B.shape[1]/seq_length))) + data_B = data_B[:, cols] + # 2. Add extra points for initial magnetization calculation data_length = seq_length + n_init data_B = np.hstack((data_B,data_B[:,1:1+n_init])) @@ -335,7 +341,7 @@ def get_dataloader(data_B, data_F, data_T, norm, n_init=32): in_B = (B-norm[0][0])/norm[0][1] in_F = (F-norm[2][0])/norm[2][1] in_T = (T-norm[3][0])/norm[3][1] - + # 5. Extra features in_dB = torch.diff(in_B,dim=1) # Flux density change in_dB = torch.cat((in_dB[:, 0:1, :], in_dB), dim=1) @@ -371,9 +377,9 @@ def get_operator_init(B0, dB, Bmax, Bmin, operator_size=30, max_out_H=1): Bmin: torch_like (batch) Min flux density of each cycle operator_size: int - TBD + The number of operators max_out_H: - TBD + The maximum output of field strength """ # 1. Parameter setting batch = dB.shape[0]