Using EXTERNE mode =========================== Warning ------------- We draw attention to the fact that this mode of operation is to be reserved for advanced use. Most cases should be handled with the algorithms provided in command MACR_RECAL, and in particular the Levenberg-Marquardt algorithm, which is the most suitable for parameter readjustment problems. This mode of operation requires external software to perform the optimization (Python code, Matlab, Scilab, black box software, etc.). Additionally, it is necessary to have some Python skills. **Finally, the use of the EXTERNE mode is outside the scope of Aster QAster**, and should not be used for IPS studies. Methodology ------------ Principle ~~~~~~~ In this mode of use, *Code_Aster* is only used for functional evaluation for optimization software that is completely external to *Code_Aster*. .. image:: images/10000000000004350000010FAD02ED26C44BAC83.png :width: 6.3374in :height: 1.5945in .. _RefImage_10000000000004350000010FAD02ED26C44BAC83.png: **Figure** 7.2.1-a **:**: **MACR_RECAL** **"External algorithm"** Generally, optimization software requires the user to write a procedure for calculating the functional: f = F (param). If the software allows the reading/writing of files and the execution of external code ("system" command or others), then it is potentially usable with MACR_RECAL. We will encapsulate the call to the Python **recal.py** procedure as well as the writing of the parameters file and the rereading of the functional value file in the F calculation routine. .. image:: images/1000000000000469000001B4C28F438388EBCB2C.png :width: 6.3374in :height: 2.4472in .. _RefImage_1000000000000469000001B4C28F438388EBCB2C.png: **Figure** 7.2.1-b **:** MACR_RECAL **"Principle of EXTERNE mode", example** Matlab The optimization software launches*Code_Aster* each time the functional is evaluated. The functional evaluation routine uses text files to send to*Code_Aster* the parameters to be used and to retrieve the value of the functional. The Pythonrecal.py routine can therefore be called independently of Aster and acts as the link between the optimization software and *Code_Aster* (retrieving the parameters file, reformatting the slave file, launching Aster, recovering the functional). In practice, it is necessary to have a classic Astk profile from the slave study (and not the profile of the one using MACR_RECAL). This profile should be fully functional. In this mode EXTERNE, the operating mode is as follows: the recal.py routine is responsible for retrieving the parameters from the command line or from a input.txt file, will replace these parameters in the slave profile, then will launch the execution of the slave study for the new parameters, and finally will return the values of the functional to a text file output.txt. To date, this mode of operation has been tested with several optimization software (the Matlab optimization toolbox, the Tomlab toolbox for Matlab, and the Python-Scipy optimization module) but it is possible to use almost any software as long as this software allows the external execution of a script. We think in particular of Zopt (Zebulon), Sidolo. Using the external launch file recal.py ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The recal.py file can be run standalone with a few optional parameters on the command line: Usage: /aster/ NEW10 /bibpyt/Macro/recal.py export_file [options] Options: -h, --help show this help message and exit --input= INPUT Text string containing the parameters --input_step= INPUT_STEP Text string containing discretization steps Finite differences --input_file= INPUT_FILE File containing the settings --input_step_file= INPUT_STEP_FILE File containing the discretization steps of Finite differences --output= OUTPUT file containing the functional --output_grad= OUTPUT_GRAD file containing the gradient --aster_root= ASTER_ROOT Aster installation path --as_run=as_runPath to as_run --resudir= RESUDIR Default path for temporary Aster executions --noclean Erase temporary Code_Aster execution directory --info= INFO message level (0, 1, 2) --sources_root= SOURCES_ROOT Python default overload path --objective= OBJECTIVE Functional ([fcalc]/[error]) --objective_type= OBJECTIVE_TYPE functional type (float/ [vector]) --gradient_type= GRADIENT_TYPE gradient calculation by*Code_Aster* ([no] /normal/adim) --mr_parameters= MR_PARAMETERS MACR_RECAL settings file: settings, calculation, experience --study_parameters= STUDY_PARAMETERS Study parameter file: export --parameters= PARAMETERS Parameter file This procedure requires: 1. from the file .export of the Astk study defined earlier (the standard study of MACR_RECAL) 2. a text file containing the list of parameters From these two files, it starts a *Code_Aster* calculation for the specified parameters (or two *Code_Aster* calculations, see above) and generates a text file containing only the value of the functional (scalar or vector). This procedure can be started without arguments. She will then look for **in the current directory** for files by default: 1. if there is only one .export file in the current directory, this one will be used (otherwise the procedure stops in error) 2. the input file by default will be searched under the name "input.txt" 3. the output file will be generated with the name "output.txt" The arguments —i (--input) and -o (--output) are used to specify the files. The argument —g (--output_grad): allows you to specify the text file in which the gradient will be written, if this one is requested (argument -G). Other arguments are available: 1. --info: set the message level; 0=mute, 1 or 2; 2. --objective: allows you to specify whether the functional should be returned in vector or scalar form; 3. --gradient: allows you to specify if you do not want Aster to calculate and return the gradients (no), and if you want the gradients, to specify whether you want them undimensioned or not; By default, a*Code_Aster*.output file is created in the execution directory of the procedure, allowing access to the output of the last *Code_Aster* calculation. Finally, you must configure the environment variable ASTER_ROOT (the Aster base path) to be able to launch the recal.py file in Python or use the --aster_root argument. The correct functioning of the recal.py procedure can be checked manually, by generating an input file with parameter values and manually starting the procedure. Example on the *Code_Aster* machine: CD directory export ASTER_ROOT =/aster echo "10., 20., 30." >input.txt Python $ ASTER_ROOT/NEW10 /bibpyt/Macro/recal.py study.export —-info=2 Cat ouput.txt The *Code_Aster* calculation should start and finish properly. A output.txt file must be generated in the current directory. The zzzz159f test case illustrates the external use of MACR_RECAL: in this *Code_Aster* test, we define a function f and we call*Code_Aster* once to simulate a realignment iteration. Example: Matlab© optimization module ------------------------------------------ If we take the principle again and apply it to an optimization algorithm that would be written in Matlab, we must write two files go.m and function.m that perform the actions described below: .. image:: images/1000000000000473000001B239F3CF50B8EEDE7A.png :width: 6.3374in :height: 2.4146in .. _RefImage_1000000000000473000001B239F3CF50B8EEDE7A.png: **Figure** 7.3-a **:** MACR_RECAL **"EXTERNE mode principle" applied to** Matlab© The following example uses the Matlab optimization module and performs *Code_Aster* calculations on the local server. **go.m file** (initialization and launch of the optimization) .. code-block:: text clear all; long format; system ('rm -f fort.91'); options = optimset ('Display', 'iter', 'iter', 'LevenbergMarquardt', 'on', 'tolFun', 1e-8, 'maxFunevals', 1000, 'maxIter', 100); % Params: DSDE__, SIGY__, YOUN__ x0 = [:ref:` 1000., 30., 100000. < 1000., 30., 100000. >`]; lb = [:ref:` 500., 5., 50000. < 500., 5., 50000. >`]; ub = [:ref:` 10000., 500., 500000. < 10000., 500., 500000. >`]; % vector [x, resnorm, residual, exitflag, output, output, output, lambda, jacobian] = lsqnonlin (@fonction, x0, lb, ub, options) Scalar% % [x, fval, exitflag, output] = fmincon (@fonction, x0, [], [], [], [], [], lb, ub, ub, [], options) **Function.m file** (F (param) calculation)/Linux version .. code-block:: text function f = function (x) system ('rm -f input.txt'); system ('rm -f output.txt'); % Writing parameters dlmwrite ('input.txt', x, 'precision', '%.20f'); % Local iret = system ('export ASTER_ROOT =/opt/aster; Python $ ASTER_ROOT/NEW10 /bibpyt/Macro/recal.py **study.export** --objective_type=vector'); f = dlmread ('output.txt', ',', ', 0, 0);% reread a scalar or a vector In go.m, two examples of algorithms are given. The lsqnonlin algorithm minimizes a vector functional and it is therefore necessary to uncomment the first "iret=" line. This algorithm is based on Levenberg-Marquardt with active constraints, and is therefore similar to the one implemented in*Code_Aster*. **Note:** *It is important to note the presence of the command* *long format and the argument* *precisionfor the command* *dlmwrite. This makes it possible to work with a sufficient number of significant figures and not to stop if the tolerance on the function is no longer respected.* **Note 2:** *When the optimization software is not able to manage the return codes from the external execution, care must be taken to properly stop the procedure in the event of an abnormal stop of the Code_Aster calculation. That's why we delete the files*.input.txt and *output.txt* just after their use, so as not to fall into endless loops. *