File: optimizer_simfun.m

package info (click to toggle)
openems 0.0.35%2Bgit20190103.6a75e98%2Bdfsg.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,424 kB
  • sloc: cpp: 40,407; python: 2,028; yacc: 580; makefile: 458; lex: 350; sh: 176; ruby: 19
file content (134 lines) | stat: -rw-r--r-- 3,655 bytes parent folder | download | duplicates (3)
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
function result = optimizer_simfun( folder, params )
%
% simulation function
%
% the variable params contains the simulation parameters

disp( [mfilename ': SIMULATING...'] );

if nargin == 0
    % visualize the structure if called without parameters
    folder = 'tmp';
    params.turns = 10;
end

oldpwd = pwd;
[a,a,a] = mkdir( folder );
cd( folder );

% create the structure
f_max = 50e6;
structure( params, 'tmp.xml', f_max );

if nargin == 0
    % visualize the structure
    CSXGeomPlot('tmp.xml');
    return;
end

% start simulation
RunOpenEMS( '.', 'tmp.xml', '--engine=fastest' );

% postprocess the results
L = postproc( 'tmp.xml', f_max );
disp( ['DONE. L = ' num2str(L(1)/1e-6) ' uH'] );

% calculate result
goal   = 2e-6; % specify the goal: 2 uH
result = abs(goal - L(1)); % costs must not be negative

% restore curent folder
cd( oldpwd );




% -------------------------------------------------------------------------
% -------------------------------------------------------------------------

function CSX = structure( params, filename, f_max )
% CSX = structure( params, filename )

unit   = 1e-3; % specify length in mm
lambda = 3e8/f_max;
resolution = lambda/15/unit;
mesh_size  = 1.5;
radius = 10;
turns  = params.turns;
height = 4 * turns;
feed_length = 10;

CSX = InitCSX();

%% create coil
p1 = create_coil( radius, height, turns );
p  = p1(:,end) + [feed_length;0;0];
p1 = [p1 p];
p  = p1(:,1) + [feed_length;0;0];
p1 = [p p1];
CSX = AddMetal(CSX,'PEC1');
CSX = AddCurve(CSX, 'PEC1', 0, p1);

%% create mesh
extraspace = 5*radius;
mesh.x = linspace(-radius,radius,ceil(2*radius/mesh_size));
mesh.x = [mesh.x mesh.x(1)-extraspace mesh.x(end)+extraspace];
mesh.x = [mesh.x p1(1,1) p1(1,1)-mesh_size p1(1,1)+mesh_size];
mesh.x = SmoothMeshLines2( mesh.x, resolution );
mesh.y = linspace(-radius,radius,ceil(2*radius/mesh_size));
mesh.y = [mesh.y mesh.y(1)-extraspace mesh.y(end)+extraspace];
mesh.y = SmoothMeshLines2( mesh.y, resolution );
mesh.z = linspace(0,height,ceil(height/mesh_size));
mesh.z = [mesh.z mesh.z(1)-extraspace mesh.z(end)+extraspace];
% mesh.z = [mesh.z p1(3,1) p1(3,1)-mesh_size p1(3,1)+mesh_size];
mesh.z = SmoothMeshLines2( mesh.z, resolution );
CSX = DefineRectGrid(CSX, unit, mesh);


%% create port
[CSX,port] = AddCurvePort( CSX, 10, 1, 50, p1(:,1), p1(:,end), 'excite' );

if nargin > 1
    max_timesteps = 100000;
    min_decrement = 1e-5;
    FDTD = InitFDTD( max_timesteps, min_decrement );
    FDTD = SetGaussExcite( FDTD, 0, f_max );
    BC = {'PEC' 'PEC' 'PEC' 'PEC' 'PMC' 'PMC'};
    FDTD = SetBoundaryCond( FDTD, BC );

    WriteOpenEMS( filename, FDTD, CSX );
end


function p = create_coil(coil_rad,coil_length,coil_turns,coil_res,winding_direction,direction,offset,angle_offset)
if nargin < 8, angle_offset = 0; end
if nargin < 7, offset = [0; 0; 0]; end
if nargin < 6, direction = +1; end
if nargin < 5, winding_direction = +1; end
if nargin < 4, coil_res = 30; end
dt = 1/coil_res;
height = 0;

p = [];
while abs(height) < coil_length
    angle      = height / (coil_length/coil_turns) * 2*pi;
    p(1,end+1) = coil_rad * cos(angle*winding_direction+angle_offset);
    p(2,end)   = coil_rad * sin(angle*winding_direction+angle_offset);
    p(3,end)   = height * direction;
    p(:,end)   = p(:,end) + offset;
    height     = height + coil_length/coil_turns * dt;
end



function L = postproc( filename, f_max )
freq = linspace(0,f_max,201);
freq(1) = []; % delete DC component

folder = fileparts(filename);
U = ReadUI( 'port_ut1', folder, freq );
I = ReadUI( 'port_it1', folder, freq );
Z = U.FD{1}.val ./ I.FD{1}.val;

L = imag(Z) ./ (2*pi*freq);
L = reshape( L, 1, [] ); % row vector