-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGateLayoutModel.m
274 lines (223 loc) · 11 KB
/
GateLayoutModel.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
classdef GateLayoutModel < comsolkit.LayeredModel
% GateLayoutModel Inherits from LayeredModel and adds gates.
properties(Dependent)
es % Handle to the electrostatic physics feature.
std % Handle to the stationary study feature for solving the model.
end
properties(Constant)
BASE_TAG_ES = 'es'; % Tag of the electrostatic physics feature.
BASE_TAG_STD = 'std'; % Tag of the study feature.
BASE_TAG_STAT = 'stat'; % Tag of the stationary feature.
DEFAULT_GATE_CLASS = @comsolkit.Gate; % Used for import functions.
DEFAULT_POT_VAR = 'mod1.V'; % Evaluate this for the potential.
end
methods
function obj = GateLayoutModel(varargin)
% GateLayoutModel Creates a gate layout model.
%
% GateLayoutModel(varargin)
%
% Parameters:
% FromFile: Load from a mph-file on the file system
% FromTag: Load from an existing model on the server by tag
% %%% parameters below only for new models %%%
% LengthUnit: Length of a unit in meters (default: 1e-9)
obj = [email protected](varargin{:});
% Create electrostatics physics, if it does not exist.
esIndex = obj.model.physics.index(obj.BASE_TAG_ES);
if esIndex < 0
obj.model.physics.create(obj.BASE_TAG_ES, ...
'Electrostatics', ...
obj.geom.tag());
end
% Create stationary study, if it does not exist.
stdIndex = obj.model.study.index(obj.BASE_TAG_STD);
if stdIndex < 0
study = obj.model.study.create(obj.BASE_TAG_STD);
study.feature.create(obj.BASE_TAG_STAT, 'Stationary');
else
study = obj.model.study(obj.BASE_TAG_STD);
statIndex = study.feature().index(obj.BASE_TAG_STAT);
assert(statIndex >= 0, ...
'No stationary study feature found.');
end
end
function es = get.es(obj)
import com.comsol.model.*;
esIndex = obj.model.physics.index(obj.BASE_TAG_ES);
assert(esIndex >= 0, 'Could not find electrostatics %s.', ...
obj.BASE_TAG_ES);
es = obj.model.physics(obj.BASE_TAG_ES);
end
function std = get.std(obj)
import com.comsol.model.*;
stdIndex = obj.model.study.index(obj.BASE_TAG_STD);
assert(stdIndex >= 0, 'Could not find study %s.', ...
obj.BASE_TAG_STD);
std = obj.model.study(obj.BASE_TAG_STD);
end
function savedObj = saveobj(obj)
% saveobj Saves the object including the comsol model.
%
% savedObj = saveobj(obj)
savedObj = [email protected](obj);
end
function potential = compute_interpolated_potential(obj, ...
coordinateArray, varargin)
% compute_interpolated_potential Solves model, returns pot.
%
% potential = compute_interpolated_potential(obj, ...
% coordinateArray)
% potential = compute_interpolated_potential(obj, ...
% coordinateArray, mRows, nColms)
%
% Parameters:
% coordinateArray: Solution is evaluated at this points, when
% they do not correspond to mesh vertices,
% values are interpolated (3 x n size).
% mRows, nColms: Reshape the data, when on a grid (optional).
%
% Example:
% precision = 10;
% l_domain = 3000;
% w_domain = 1200;
% x0 = 0:precision:l_domain;
% y0 = 0:precision:w_domain;
% z0 = [-100]; % Depth.
% [x,y,z] = meshgrid(x0,y0,z0);
% xyz = [x(:),y(:),z(:)]';
% pot = compute_interpolated_potential(xyz, ...
% length(x0), length(y0));
import com.comsol.model.*;
assert(nargin == 2 || nargin == 4, ...
'Wrong number of arguments');
assert(isnumeric(coordinateArray) && ...
size(coordinateArray, 1) == 3, ...
'Wrong coordinateArray format');
obj.std.run; % Solve the model.
% Retrieve interpolated potential values.
potential = evaluate_interpolated_expression(obj, ...
obj.DEFAULT_POT_VAR, coordinateArray, varargin{:});
end
function [coordinateCell, nameCell] = import_gds_file(obj, gdsFile)
% import_gds_file Import gds structures into layerArray.
%
% import_gds_file(obj, gds_file)
%
% The gds-file in gds_file should have
% either a single structure with n elements corresponding to
% n gates or a structure with n element referencing a
% structure with one element per structure.
% The later allows to have named gates.
%
% Schematic (option 1 with named gates):
% S (array of elements)
% |----|----|----|----|
% E E E E E (element references struct)
% | | | | |
% S S S S S (struct represents gate)
% | | | | |
% E E E E E (element contains XY coords)
%
% Schematic (option 2):
% S (array of elements)
% |----|----|----|----|
% E E E E E (element contains XY coords)
% TODO: Extend import to deal with multiple polygons per gate.
assert(exist(gdsFile, 'file') == 2, 'File does not exist.');
gdsLibrary = read_gds_library(gdsFile);
% Scale gds data according to ComsolModel.lengthUnit.
userUnit = get(gdsLibrary,'uunit');
ratio = userUnit / obj.lengthUnit;
% Find root structure element.
topName = topstruct(gdsLibrary);
assert(ischar(topName), 'Only one top structure allowed');
topStruct = obj.struct_by_name(gdsLibrary, topName);
% The case with one element per structure.
% If first element reference, assume all.
if is_ref(topStruct(1))
% Iterate over reference structures, get real structures.
nameCell = cell(1, numel(topStruct));
coordinateCell = cell(1, numel(topStruct));
for i = 1:numel(topStruct)
% Get referenced structure by name.
referenceStruct = obj.struct_by_name(gdsLibrary, ...
get(topStruct(i),'sname'));
% Include offset but no strans (rotation, scaling).
xyOffset = get(topStruct(i),'xy');
% This struct has allways one child.
xy = get(referenceStruct(1),'xy');
xy = xy{1};
xy = bsxfun(@plus, xy, xyOffset); % Add offset.
xy = xy .* ratio;
coordinateCell{i} = {xy};
nameCell{i} = referenceStruct.sname;
end
else
nameCell = {};
coordinateCell = cell(1, numel(topStruct));
for i = 1:numel(topStruct)
if is_ref(topStruct(i))
% This should not happen.
warning('Skipped reference element %d.', i);
continue;
end
xy = get(topStruct(i),'xy');
xy = xy{1};
xy = xy .* ratio;
coordinateCell{i} = {xy};
end
end
end
function choose_domain_region(obj)
% choose_domain_region Defines domain region on layerArray.
%
% choose_domain_region(obj)
%
% Usage:
% Draw a rectangle around the region to consider and
% double-click inside the rectangle.
f = figure;
set(f, 'Name', ['Select domain region and double-click ' ...
'inside to confirm.']);
obj.layerArray.plot();
h = imrect;
pos = wait(h);
fprintf('Dimensions:\n\torigin_x: %f\n\torigin_y: %f\n', ...
pos(1), pos(2));
fprintf('\tl_domain: %f\n\tw_domain: %f\n', pos(3), pos(4));
fprintf(['Set this parameters in models created from a ' ...
'template via set_param:\n']);
fprintf('\t%s.set_param(''origin_x'', %f);\n', ...
inputname(1), pos(1));
fprintf('\t%s.set_param(''origin_y'', %f);\n', ...
inputname(1), pos(2));
fprintf('\t%s.set_param(''l_domain'', %f);\n', ...
inputname(1), pos(3));
fprintf('\t%s.set_param(''w_domain'', %f);\n', ...
inputname(1), pos(4));
close(f);
end
end
methods(Access = private)
function s = struct_by_name(~, lib, str)
% struct_by_name Helper returns gds_structs by name.
%
% s = struct_by_name(~, lib, str)
nameCell = cellfun(@sname, lib(:), 'UniformOutput', false);
logicCell = cellfun(@(x) isequal(x,str), nameCell);
s = lib(logicCell);
if iscell(s) % FIX: some programs save gds differently
s = s{1};
end
end
end
methods(Static)
function loadedObj = loadobj(obj)
% loadobj Loads the object including the comsol model.
%
% loadedObj = loadobj(obj)
loadedObj = [email protected](obj);
end
end
end