-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenEMS_init_mesh.m
185 lines (139 loc) · 5.06 KB
/
openEMS_init_mesh.m
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
clc
clear
close all
load('simParams.mat');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initialize openEMS environment %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Sim_CSX = 'openEMS_sim.xml';
load(fullfile(Sim_Path,'model_data.mat'));
load(fullfile(Sim_Path,'model_ports.mat'));
load(fullfile(Sim_Path,'openEMS_data.mat'));
pkg load geometry
pkg load miscellaneous
addpath(modelerPath);
addpath(miscPath);
addpath(openEMSpath);
physical_constants;
%%%%%%%%%%%
% Mesh!!! %
%%%%%%%%%%%
allMesh = [];
for metalIdx = 1:numel(conductorNames);
cMesh = DetectEdges(CSX, [], 'SetProperty',conductorNames{metalIdx});
if isempty(allMesh)
allMesh = cMesh;
else
allMesh.x = [allMesh.x cMesh.x];
allMesh.y = [allMesh.y cMesh.y];
allMesh.z = [allMesh.z cMesh.z];
end
end
for materialIdx = 1:numel(materials);
cMesh = DetectEdges(CSX, [], 'SetProperty',materials(materialIdx).name);
allMesh.x = [allMesh.x cMesh.x];
allMesh.y = [allMesh.y cMesh.y];
allMesh.z = [allMesh.z cMesh.z];
end
minMax = @(x) [min(x(:)) max(x(:))];
bBox = [minMax(allMesh.x) minMax(allMesh.y) minMax(allMesh.z)];
% Define original mesh
allMesh.x = unique([allMesh.x portPoints(:,1).']);
allMesh.y = unique([allMesh.y portPoints(:,2).']);
allMesh.z = unique([allMesh.z portPoints(:,3).']);
% These points are considered "immovable"
hardMesh = allMesh;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculate largest mesh step allowed %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fmax = f_center + f_cutoff;
maxDk = 0;
for mIdx = 1:numel(materials)
maxDk = (maxDk < materials(mIdx).epsR)*materials(mIdx).epsR + ...
(maxDk >= materials(mIdx).epsR)*maxDk;
end
step_max = (1/unit)*maxStep_wl*(c0/fmax)/sqrt(maxDk);
step_min = (1/unit)*minStep_wl*(c0/fmax)/sqrt(maxDk);
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Force sub minimal count %
%%%%%%%%%%%%%%%%%%%%%%%%%%%
axVect = 'xyz';
for axIdx = 1:3
cMesh = getfield(allMesh,axVect(axIdx));
cDiff = diff(cMesh);
binBadDiv = ((ceil(cDiff/step_max(axIdx)) - 1) < minEdge2EdgeCount_abs(axIdx));
badDivIdxs = find(binBadDiv);
startIdx = 1;
newMesh = [];
for dIdx = badDivIdxs
newDiv = linspace(cMesh(dIdx),cMesh(dIdx + 1),minEdge2EdgeCount_abs(axIdx) + 2);
newMesh = [newMesh cMesh(startIdx:dIdx)];
% Also, verify absolute minimum step restraint
% Bad bypass. Need to re-calculate the difference
% if (newDiv(2) - newDiv(1)) >= step_min(axIdx)
newMesh = [newMesh newDiv(2:(end-1))];
% end
startIdx = dIdx + 1;
end
newMesh = [newMesh cMesh(startIdx:end)];
% figure;
% plot(newMesh(:),ones(size(newMesh(:))),'.r','markersize',20);
% hold on;
% plot(cMesh(:),ones(size(cMesh(:))),'.b','markersize',10);
% hold off;
% Stock back after done
eval(['allMesh.' axVect(axIdx) ' = newMesh;']);
end
%%%%%%%%%%%%%%%%%%%%%%%
% Generate sub-meshes %
%%%%%%%%%%%%%%%%%%%%%%%
allMesh.x = AutoSmoothMeshLines(allMesh.x,step_max(1), gradRatio,'symmetric',0,'homogeneous',0,'algorithm',[ 3]);
allMesh.y = AutoSmoothMeshLines(allMesh.y,step_max(2), gradRatio,'symmetric',0,'homogeneous',0,'algorithm',[ 3]);
allMesh.z = AutoSmoothMeshLines(allMesh.z,step_max(3), gradRatio,'symmetric',0,'homogeneous',0,'algorithm',[ 3]);
%%%%%%%%%%%%%%%%%%%%%%%%%%
% Remove duplicate lines %
%%%%%%%%%%%%%%%%%%%%%%%%%%
% Here very close lines will be removed. This can happen for various reasons,
% but mostly slightly crooked lines. This is a common artifact from KiCAD
allMesh.x = removeSameMeshPoints(allMesh.x,samePointRes);
allMesh.y = removeSameMeshPoints(allMesh.y,samePointRes);
allMesh.z = removeSameMeshPoints(allMesh.z,samePointRes);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Smooth mesh in areas where step size is smaller than minimum %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Determine mesh limitations in material
minMeshCell = [min(diff(allMesh.x(:))) min(diff(allMesh.y(:))) min(diff(allMesh.z(:)))];
dt_smallest = minMeshCell/(c0/sqrt(maxDk));
% Stupid approximation
dt_min_approx = dt_smallest/2e4;
axNames = 'xyz';
for axIdx = 1:3
cMin = minCellSize_abs(axIdx);
if ~cMin
continue;
end
cAxVals = getfield(allMesh,axNames(axIdx));
cAxMesh = [];
cDiff = diff(cAxVals(:));
while max(cDiff < cMin)
% Find first small mesh cell
i1 = find(cDiff < cMin,1);
i2 = i1;
while cDiff(i2) < cMin
i2 = i2 + 1;
end
% Take one cell forward and one cell backwards
meshSnip = [cAxVals(i1-1) cAxVals(i2+1)];
minNparts = floor((meshSnip(2) - meshSnip(1))/cMin);
smoothSnip = linspace(meshSnip(1),meshSnip(2),minNparts + 1);
cAxVals = [cAxVals(1:(i1 - 2)) smoothSnip cAxVals((i2 + 2):end)];
cDiff = diff(cAxVals(:));
end
eval(['allMesh.' axNames(axIdx) ' = cAxVals;']);
end
save( fullfile(Sim_Path,'mesh_data.mat'),...
'allMesh',...
'hardMesh',...
'step_max',...
'maxDk',...
'fmax');