-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreferenceMotor.m
More file actions
98 lines (80 loc) · 3.17 KB
/
Copy pathreferenceMotor.m
File metadata and controls
98 lines (80 loc) · 3.17 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
% SPDX-License-Identifier: GPL-3.0
function [] = referenceMotor(simParams)
% motor1Az - Simulates and evaluates the magnetic potential of a parameterized
% permanent magnet synchronous motor (IPMSM) using isogeometric analysis.
%
% Syntax:
% x = referenceMotor(simParams)
%
% INPUT:
%
% simParams: a structure with data of the simulation run. It contains the fields:
% iter - Integer index for the simulation run (used in file naming).
% mh - Magnet height (geometry parameter).
% mw - Magnet width (geometry parameter).
% mag - Magnet distance to airgap (geometry parameter).
% rot - Rotor rotation angle in degrees.
% airgap - Boolean flag (true/false). If true, extract solution only in the airgap.
% predictionsDir - Directory of the prediction data.
% predictionsFile - Name of the prediction file.
% errorFile - Name where to write the errors in.
%
% Description:
% This function sets up the geometry, materials, boundary conditions, and excitations
% for a parameterized PMSM. It solves the magnetostatic problem using harmonic mortaring
% and isogeometric analysis (IGA). It computes torque and field predictions errors.
addpath(genpath("packages"))
[solShrink, K, Rotor, Stator, Motor, Coupling, solFull] = solveMotor(simParams);
% Extract the fields from the data structures into local variables
data_names = fieldnames (simParams);
for iopt = 1:numel (data_names)
eval ([data_names{iopt} '= simParams.(data_names{iopt});']);
end
if(airgap)
AirGapDofs = [];
AigGapPatches = 13:17;
for iPatch = 1:numel(AigGapPatches)
AirGapDofs = union(AirGapDofs, Rotor.Spaces.gnum{AigGapPatches(iPatch)});
end
x = Rotor.Solution.Values(AirGapDofs);
Torque = Coupling.calcTorqueBrBtRt();
% open prediction file
pred_sol = readmatrix(predictionsDir + "/" + predictionsFile);
K = load("MatrixK_gap.csv");
Rotor.MagneticPotential = zeros(size(Rotor.Solution.Values));
Rotor.MagneticPotential(AirGapDofs) = pred_sol;
Torque_pred = Coupling.calcTorqueBrBtRt();
else
Rind = Motor.getIndicesReduced(Rotor);
Sind = Motor.getIndicesReduced(Stator);
x = solShrink;
Torque = Coupling.Solution.Torque;
% open other prediction file
pred_sol = readmatrix(predictionsDir + "/" + predictionsFile);
newSolution = solFull(Motor.K);
newSolution(Rind) = pred_sol(1:size(Rind,1));
newSolution(Sind) = pred_sol(size(Rind,1)+1:end);
newSolution = Motor.reconstructSolution(0, newSolution);
Motor.postprocess(0, newSolution);
Torque_pred = Coupling.Solution.Torque;
end
% Compute error in Hcurl seminorm
diff = pred_sol - x;
rel_err_curl =sqrt((diff'* K * diff) /(x'* K * x));
% Compute relative error of torque
rel_err_torque = abs(Torque-Torque_pred)/ Torque;
err_data = [iter, rel_err_curl, rel_err_torque];
% Save to error file
if isfile(errorFile)
if iter==1
msg = 'This error file already exists. Are you sure you want to write into an existing file?';
error(msg)
end
fid = fopen(errorFile,'a');
else
fid = fopen(errorFile, 'w');
fprintf(fid, 'iter, curl_err, torque_err\n');
end
fprintf(fid, '%i,%f,%f\n', err_data);
fclose(fid);
end