summaryrefslogtreecommitdiff
path: root/openEMS/matlab/optimizer_asco_sim.m
blob: 6f62ebfadf643539844e03e7f338305ff90ecfd7 (plain)
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
function optimizer_asco_sim( optimdir, inputfile, outputfile, simfun )
%optimizer_asco_sim( optimdir, inputfile, outputfile, simfun )
%
% This function is called from general.sh. Do not call it yourself.
%
% tasks:
%  - set correct matlab path
%  - evaluate inputfile
%  - start simulation or get result from cache
%  - postprocess simulation results
%  - create output file (important: needs single \n at the first line and double \n at the last line!)

error( nargchk(4,4,nargin) );

% add CSXCAD and openEMS to the matlab path
folder = fileparts( mfilename('fullpath') );
addpath( folder );
addpath( [folder '/../../CSXCAD/matlab'] );

% change to optimdir
olddir = pwd;
cd( optimdir );

% read parameters set by asco
if ~isempty( strfind(inputfile,'-') )
    % matlab cannot execute a file with dashes...
    inputfile2 = strrep( inputfile,'-','_' );
    movefile( [inputfile '.m'], [inputfile2 '.m'] );
    run( inputfile2 );
    movefile( [inputfile2 '.m'], [inputfile '.m'] );
end
% now a structure named 'params' is available

% check cache
folder = create_folder_name( params );
if exist( ['./' folder], 'dir' ) && exist( ['./' folder '/result.mat'], 'file' )
    % read cache
    disp( 'CACHE HIT' );
    result = load( [folder '/result.mat'], 'result' );
    result = result.result;
else
    % start simulation in folder <folder>
    disp( ['starting simulation function ' simfun] );
    disp( ['         simulation folder   ' folder] );
    [simfun_folder,simfun] = fileparts(simfun);
    oldpath = path;
    addpath( simfun_folder );
    fhandle = str2func(simfun); % does not work for octave-3.2.4!
    path( oldpath );
    mkdir( folder );
    result = fhandle(folder,params);
    save( [folder '/result.mat'], 'result', '-mat' );
end

% write results for asco
fid = fopen( outputfile, 'wt' );
fprintf( fid, '\nvalue= %e\n\n', result );
fclose( fid );

% update best result
best = [];
best.result = result;
best.params = params;
if exist( [pwd '/best_result.mat'], 'file' )
    old = load( 'best_result.mat', 'best' );
    if old.best.result > best.result
        save( 'best_result.mat', 'best', '-mat' );
    end
else
    save( 'best_result.mat', 'best', '-mat' );
end

% restore old folder
cd( olddir );







function folder = create_folder_name( params )
params = orderfields( params );
folder = 'opt';
fnames = fieldnames(params);
for n=1:numel(fnames)
    folder = [folder '_' fnames{n} '=' num2str(params.(fnames{n}))];
end