%% Example of axial flux machine optimization script (see section 6.3 of User Manual - Part II)
% This script implements the automatic optimization of an axial flux motor minimizing two objectives 
% (motor weight and motor losses for the target operating point of 150 Nm and 1600 RPM) by varying several 
% geometry parameters of the motor. The EMRAX-228 motor is used as a reference design for this example 
% (see file SimFiles/EMRAX-228/EMRAX-228_forOptimization.mxa). The parallel processing is used to speed up 
% the optimization process allowing running FE simulations for several designs at the same time. 
% See also file \CustomScripts\evalDesigns_emrax228.m, which is used by the optimization algorithm 
% for processing generated designs and calculating the objective values. 
%% Step 1: Initialization
cd ../
initMXAscriptingAPI;
maxNumberOfProcesses = feature('numcores');                      % maximum number of processes working in parallel (equal to the number of physical cpu cores)
% maxNumberOfProcesses = 7;                                      % or can be defined manually
init_dataset_size = 50;                                           % number of initial designs generated using latin hypercube sampling
NoOfBachSequential = 14;                                          % number of designs per one iteration of optimization algorithm 
niter = 30;                                                      % number of optimization algorithm iterations
maxeval = init_dataset_size+niter*NoOfBachSequential;            % total number of designs generated
save2filename = 'Optimization_18s16p_training.mat';                     % save optimization algorithm results to this mat-file

%% Step 2: Specify initial design and design function
initdesignfile = 'C:\FEMmotor\MotorXP_AFM_v1.2.1_MATLAB(forWorkshop)\SimFiles\training\rsr_proto1.mxa';
designFunction = @evalDesigns_training;

%% Step 3: Specify optimization problem
no_outputs = 3;               % number of objectives
labelObjective = {'Weight, grams', 'Losses, watts', 'Magnet losses, watts'};     % objectives
% define bounds on design variables
bounds = [ % lower   upper
             200      250;                       % inner diameter bounds [mm]
             25       43;                        % slot width bounds [mm]
             20       42;                        % stator height bounds [mm]
             15       40;                        % magnet spacing bounds [mm]    
             5        15;                        % yoke height bounds [mm]   
             5        15;                        % magnet height bounds [mm]    
             1.5      2.5];                      % air gap [mm]
         
lb = bounds(:,1)';                     % lower bounds [lb1,lb2,...]
ub = bounds(:,2)';                     % upper bounds [ub1,ub2,...]

y_refdesign=[22948  2051.5];    % reference design weight (grams) and losses (watts) @(50 kW, 1600 RPM) - reference design red x on Pareto plot
runTSEMOptimizationafm(designFunction,initdesignfile,ub,lb,no_outputs,init_dataset_size,maxeval,niter,maxNumberOfProcesses,save2filename,y_refdesign,labelObjective);


