1\chapter{Advanced Simulation Code Interfaces}\label{advint}
2
3This chapter extends the interface discussion in
4Chapter~\ref{interfaces} and its discussion of generic black-box
5interfaces to simulations (Section~\ref{interfaces:building}).  It
6describes specialized, tightly integrated, and advanced interfaces
7through which Dakota can perform function evaluation mappings.  It
8describes AMPL-based algebraic mappings
9(Section~\ref{advint:algebraic}), tight integration of a simulation
10code into Dakota (Section~\ref{advint:direct}), and specialized
11interfaces to Matlab, Python, and Scilab
12(Sections~\ref{advint:existingdirect} and \ref{advint:scilab}).
13
14\section{Algebraic Mappings}\label{advint:algebraic}
15
16If desired, one can define algebraic input-output mappings using the
17AMPL code~\cite{Fou03} and save these mappings in 3 files:
18\path{stub.nl}, \path{stub.col}, and \path{stub.row}, where
19\texttt{stub} is a particular root name describing a particular
20problem.  These files names can be communicated to Dakota using the
21\texttt{algebraic\_mappings} input.
22
23Dakota will use \path{stub.col} and \path{stub.row} to obtain
24input and output identifier strings, respectively, and will use the
25AMPL solver library~\cite{Gay97} to evaluate expressions conveyed
26in \path{stub.nl}, and, if needed, their first and second
27derivatives.
28
29As a simple example (from \path{dakota/share/dakota/test/dakota_ampl*}), consider
30algebraic mappings based on Newton's law $F = m a$.  The
31following is an AMPL input file of variable and expression
32declarations and output commands:
33\begin{center}
34\begin{bigbox}
35\begin{small}
36\verbatimtabinput[8]{dakota_ampl_fma.mod}
37\end{small}
38\end{bigbox}
39\end{center}
40
41When processed by an AMPL processor, three files are created (as
42requested by the ``option auxfiles" command).  The first is the \path{dakota_ampl_fma.nl}
43file containing problem statistics, expression graphs, bounds, etc.:
44\begin{center}
45\begin{bigbox}
46\begin{small}
47\verbatimtabinput[8]{dakota_ampl_fma.nl}
48\end{small}
49\end{bigbox}
50\end{center}
51
52Next, the \path{dakota_ampl_fma.col} file contains the set of variable
53descriptor strings:
54\begin{center}
55\begin{bigbox}
56\begin{small}
57\verbatimtabinput[8]{dakota_ampl_fma.col}
58\end{small}
59\end{bigbox}
60\end{center}
61
62and the \path{dakota_ampl_fma.row} file contains the set of response descriptor
63strings:
64\begin{center}
65\begin{bigbox}
66\begin{small}
67\verbatimtabinput[8]{dakota_ampl_fma.row}
68\end{small}
69\end{bigbox}
70\end{center}
71
72The variable and objective function names declared within AMPL should
73be a subset of the variable descriptors and response descriptors used
74by Dakota (see the Dakota Reference Manual~\cite{RefMan} for information
75on Dakota variable and response descriptors).  Ordering of the inputs
76and outputs within the AMPL declaration is not important, as Dakota
77will reorder data as needed.  The following listing shows an excerpt
78from \path{dakota/share/dakota/test/dakota_ampl_fma.in}, which demonstrates a
79combined algebraic/simulation-based mapping in which algebraic
80mappings from the \texttt{fma} definition are overlaid with
81simulation-based mappings from \texttt{text\_book}:
82\begin{center}
83\begin{bigbox}
84\begin{small}
85\begin{verbatim}
86variables,
87        continuous_design = 5
88          descriptor    'x1' 'mass' 'a' 'x4' 'v'
89          initial_point  0.0  2.0  1.0  0.0  3.0
90          lower_bounds  -3.0  0.0 -5.0 -3.0 -5.0
91          upper_bounds   3.0 10.0  5.0  3.0  5.0
92
93interface,
94        algebraic_mappings = 'dakota_ampl_fma.nl'
95        system
96          analysis_driver = 'text_book'
97          parameters_file = 'tb.in'
98          results_file    = 'tb.out'
99          file_tag
100
101responses,
102        response_descriptors = 'force' 'ineq1' 'energy'
103        num_objective_functions = 1
104        num_nonlinear_inequality_constraints = 1
105        num_nonlinear_equality_constraints = 1
106        nonlinear_equality_targets = 20.0
107        analytic_gradients
108        no_hessians
109\end{verbatim}
110\end{small}
111\end{bigbox}
112\end{center}
113Note that the algebraic inputs and outputs are a subset of the total
114inputs and outputs and that Dakota will track the algebraic
115contributions to the total response set using the order of the
116descriptor strings.  In the case where both the algebraic and
117simulation-based components contribute to the same function, they are
118added together.
119
120To solve \texttt{text\_book} algebraically (refer to
121Section~\ref{additional:textbook} for definition), the
122following AMPL model file could be used
123\begin{center}
124\begin{bigbox}
125\begin{small}
126\verbatimtabinput[8]{dakota_ampl_tb.mod}
127\end{small}
128\end{bigbox}
129\end{center}
130Note that the nonlinear constraints should not currently be declared
131as constraints within AMPL.  Since the Dakota variable bounds and
132constraint bounds/targets currently take precedence over any AMPL
133specification, the current approach is to declare all AMPL outputs as
134objective functions and then map them into the appropriate response
135function type (objectives, least squares terms, nonlinear
136inequality/equality constraints, or generic response functions) within
137the Dakota input specification.
138
139
140\section{Developing a Direct Simulation Interface}\label{advint:direct}
141
142If a more efficient interface to a simulation is desired (e.g., to
143eliminate process creation and file I/O overhead) or if a targeted
144computer architecture cannot accommodate separate optimization and
145simulation processes (e.g., due to lightweight operating systems on
146compute nodes of large parallel computers), then linking a simulation
147code directly with Dakota may be desirable. This is an advanced
148capability of Dakota, and it requires a user to have access to (and
149knowledge of) the Dakota source code, as well as the source code of
150the simulation code.
151
152Three approaches are outlined below for developing direct linking
153between Dakota and a simulation: extension, derivation, and
154sandwich. For additional information, refer to ``Interfacing with
155Dakota as a Library'' in the Dakota Developers Manual~\cite{DevMan}.
156
157Once performed, Dakota can bind with the new direct simulation
158interface using the \texttt{direct} interface specification in
159combination with an \texttt{analysis\_driver}, \texttt{input\_filter}
160or \texttt{output\_filter} specification that corresponds to the name
161of the new subroutine.
162
163\subsection{Extension}\label{advint:direct:extension}
164
165The first approach to using the direct function capability with a new
166simulation (or new internal test function) involves \emph{extension}
167of the existing \textbf{DirectFnApplicInterface} class to include new
168simulation member functions. In this case, the following steps are
169performed:
170\begin{enumerate}
171\item The functions to be invoked (analysis programs, input and
172  output filters, internal testers) must have their main programs
173  changed into callable functions/subroutines.
174
175\item The resulting callable function can then be added directly
176  to the private member functions in \textbf{DirectFnApplicInterface}
177  if this function will directly access the Dakota data structures
178  (variables, active set, and response attributes of the class). It is
179  more common to add a wrapper function to
180  \textbf{DirectFnApplicInterface} which manages the Dakota data
181  structures, but allows the simulator subroutine to retain a level of
182  independence from Dakota (see Salinas, ModelCenter, and Matlab
183  wrappers as examples).
184
185\item The if-else blocks in the \textbf{derived\_map\_if()},
186  \textbf{derived\_map\_ac()}, and \textbf{derived\_map\_of()} member
187  functions of the \textbf{DirectFnApplicInterface} class must be
188  extended to include the new function names as options. If the new
189  functions are class member functions, then Dakota data access may be
190  performed through the existing class member attributes and data
191  objects do not need to be passed through the function parameter
192  list. In this case, the following function prototype is appropriate:
193\begin{small}
194\begin{verbatim}
195    int function_name();
196\end{verbatim}
197\end{small}
198  If, however, the new function names are not members of the
199  \textbf{DirectFnApplicInterface} class, then an \texttt{extern}
200  declaration may additionally be needed and the function prototype
201  should include passing of the Variables, ActiveSet, and Response
202  data members:
203\begin{small}
204\begin{verbatim}
205    int function_name(const Dakota::Variables& vars,
206                      const Dakota::ActiveSet& set, Dakota::Response& response);
207\end{verbatim}
208\end{small}
209
210\item The Dakota system must be recompiled and linked with the new
211  function object files or libraries.
212\end{enumerate}
213
214Various header files may have to be included, particularly within the
215\textbf{DirectFnApplicInterface} class, in order to recognize new
216external functions and compile successfully. Refer to the Dakota
217Developers Manual~\cite{DevMan} for additional information on the
218\textbf{DirectFnApplicInterface} class and the Dakota data types.
219
220\subsection{Derivation}\label{advint:direct:derivation}
221
222As described in ``Interfacing with Dakota as a Library'' in the Dakota
223Developers Manual~\cite{DevMan}, a derivation approach can be employed
224to further increase the level of independence between Dakota and the
225host application. In this case, rather than \emph{adding} a new
226function to the existing \textbf{DirectFnApplicInterface} class, a new
227interface class is derived from \textbf{DirectFnApplicInterface} which
228\emph{redefines} the \textbf{derived\_map\_if()},
229\textbf{derived\_map\_ac()}, and \textbf{derived\_map\_of()} virtual
230functions.
231
232% Note: this approach has benefits primarily in library mode
233In the approach of Section~\ref{advint:direct:sandwich} below, the
234class derivation approach avoids the need to recompile the Dakota
235library when the simulation or its direct interface class is modified.
236
237\subsection{Sandwich}\label{advint:direct:sandwich}
238
239In a ``sandwich'' implementation, a simulator provides both the
240``front end'' and ``back end'' with Dakota sandwiched in the middle.
241To accomplish this approach, the simulation code is responsible for
242interacting with the user (the front end), links Dakota in as a
243library (refer to ``Interfacing with Dakota as a Library'' in the
244Dakota Developers Manual~\cite{DevMan}), and plugs in a derived direct
245interface class to provide a closely-coupled mechanism for performing
246function evaluations (the back end). This approach makes Dakota
247services available to other codes and frameworks and is currently used
248by Sandia codes such as Xyce (electrical simulation), Sage (CFD), and
249SIERRA (multiphysics).
250
251
252\section{Existing Direct Interfaces to External Simulators}\label{advint:existingdirect}
253
254In addition to built-in polynomial test functions described in
255Section~\ref{interfaces:direct}, Dakota includes direct interfaces to
256Sandia's Salinas code for structural dynamics, Phoenix Integration's
257ModelCenter framework, The Mathworks' Matlab scientific computing
258environment, Scilab (as described in Section~\ref{advint:scilab}), and
259Python. While these can be interfaced to with a script-based
260approach, some usability and efficiency gains may be realized by
261re-compiling Dakota with these direct interfaces enabled. Some
262details on Matlab and Python interfaces are provided here. Note that
263these capabilities permit using Matlab or Python to evaluate a
264parameter to response mapping; they do not make Dakota algorithms
265available as a service, i.e., as a Matlab toolbox or Python module.
266
267\subsection{Matlab}\label{advint:existingdirect:matlab}
268
269Dakota's direct function interface includes the capability to invoke
270Matlab for function evaluations, using the Matlab engine API. When
271using this close-coupling, the Matlab engine is started once when
272Dakota initializes, and then during analysis function evaluations are
273performed exchanging parameters and results through the Matlab C API.
274This eliminates the need to use the file system and the expense of
275initializing the Matlab engine for each function evaluation.
276
277The Dakota/Matlab interface has been built and tested on 32-bit Linux
278with Matlab 7.0 (R14) and on 64-bit Linux with Matlab 7.1 (R14SP3).
279Configuration support for other platforms is included, but is
280untested. Builds on other platforms or with other versions of Matlab
281may require modifications to Dakota including its build system
282
283To use the Dakota/Matlab interface, Dakota must be configured and
284compiled with the Matlab feature enabled. The Mathworks only provides
285shared object libraries for its engine API, so Dakota must be
286dynamically linked to at least the Matlab libraries. To compile
287Dakota with the Matlab interface enabled, set the CMake variable {\tt
288  DAKOTA\_MATLAB:BOOL=ON}, possibly with {\tt
289  MATLAB\_DIR:FILEPATH=/path/to/matlab}, where \\ {\tt MATLAB\_DIR} is the
290root of your Matlab installation (it should be a directory containing
291directories bin/YOURPLATFORM and extern/include).
292
293Since the Matlab libraries are linked dynamically, they must be
294accessible at compile time and at run time. Make sure the path to the
295appropriate Matlab shared object libraries is on your
296{\tt LD\_LIBRARY\_PATH}. For example to accomplish this in BASH on
29732-bit Linux, one might type
298\begin{verbatim}
299export LD_LIBRARY_PATH=/usr/local/matlab/bin/glnx86:$LD_LIBRARY_PATH
300\end{verbatim}
301or add such a command to the .bashrc file. Then proceed with
302compiling as usual.
303
304Example files corresponding to the following tutorial are available in
305\path{dakota/share/dakota/examples/linked_interfaces/Matlab}.
306
307\subsubsection{Dakota/Matlab input file specification}
308
309The Matlab direct interface is specified with {\tt direct, matlab}
310keywords in an interface specification. The Matlab m-file which
311performs the analysis is specified through the {\tt analysis\_drivers}
312keyword. Here is a sample Dakota {\tt interface} specification:
313\begin{small}
314\begin{verbatim}
315  interface,
316    matlab
317      analysis_drivers = 'myanalysis.m'
318\end{verbatim}
319\end{small}
320
321Multiple Matlab analysis drivers are supported. Multiple analysis
322components are supported as for other interfaces as described in
323Section~\ref{interfaces:components}. The {\tt .m} extension in the
324{\tt analysis\_drivers} specification is optional and will be stripped
325by the interface before invoking the function. So {\tt myanalysis}
326and {\tt myanalysis.m} will both cause the interface to attempt to
327execute a Matlab function {\tt myanalysis} for the evaluation.
328
329\subsubsection{Matlab .m file specification}
330
331The Matlab analysis file {\tt myanalysis.m} must define a Matlab
332function that accepts a Matlab structure as its sole argument and
333returns the same structure in a variable called {\tt Dakota}. A
334manual execution of the call to the analysis in Matlab should
335therefore look like:
336\begin{small}
337\begin{verbatim}
338  >> Dakota = myanalysis(Dakota)
339\end{verbatim}
340\end{small}
341Note that the structure named Dakota will be pushed into the Matlab
342workspace before the analysis function is called. The structure
343passed from Dakota to the analysis m-function contains essentially the
344same information that would be passed to a Dakota direct function
345included in {\tt DirectApplicInterface.C}, with fields shown in
346Figure~\ref{advint:figure:matlabparams}.
347
348\begin{figure}
349\centering
350\begin{bigbox}
351\begin{small}
352\begin{verbatim}
353Dakota.
354  numFns              number of functions (responses, constraints)
355  numVars             total number of variables
356  numACV              number active continuous variables
357  numADIV             number active discrete integer variables
358  numADRV             number active discrete real variables
359  numDerivVars        number of derivative variables specified in directFnDVV
360  xC                  continuous variable values ([1 x numACV])
361  xDI                 discrete integer variable values ([1 x numADIV])
362  xDR                 discrete real variable values ([1 x numADRV])
363  xCLabels            continuous var labels (cell array of numACV strings)
364  xDILabels           discrete integer var labels (cell array of numADIV strings)
365  xDRLabels           discrete real var labels (cell array of numADIV strings)
366  directFnASV         active set vector ([1 x numFns])
367  directFnDVV         derivative variables vector ([1 x numDerivVars])
368  fnFlag              nonzero if function values requested
369  gradFlag            nonzero if gradients requested
370  hessFlag            nonzero if hessians requested
371  currEvalId          current evaluation ID
372\end{verbatim}
373\end{small}
374\end{bigbox}
375\caption{Dakota/Matlab parameter data
376structure.\label{advint:figure:matlabparams}}
377\end{figure}
378
379The structure {\tt Dakota} returned from the analysis must contain a
380subset of the fields shown in
381Figure~\ref{advint:figure:matlabresponse}. It may contain additional
382fields and in fact is permitted to be the structure passed in,
383augmented with any required outputs.
384\begin{figure} \centering
385\begin{bigbox}
386\begin{small}
387\begin{verbatim}
388Dakota.
389  fnVals      ([1 x numFns], required if function values requested)
390  fnGrads     ([numFns x numDerivVars], required if gradients  requested)
391  fnHessians  ([numFns x numDerivVars x numDerivVars],
392               required if hessians requested)
393  fnLabels    (cell array of numFns strings, optional)
394  failure     (optional: zero indicates success, nonzero failure
395\end{verbatim}
396\end{small}
397\end{bigbox}
398\caption{Dakota/Matlab response data
399structure.\label{advint:figure:matlabresponse}}
400\end{figure}
401
402An example Matlab analysis driver {\tt rosenbrock.m} for the
403Rosenbrock function is shown in Figure
404~\ref{advint:figure:matlabrosen}.
405\begin{figure} \centering
406\begin{bigbox}
407\begin{tiny}
408\begin{verbatim}
409function Dakota = rosenbrock(Dakota)
410
411  Dakota.failure = 0;
412
413  if ( Dakota.numVars ~= 2 | Dakota.numADV | ...
414      ( ~isempty( find(Dakota.directFnASM(2,:)) | ...
415      find(Dakota.directFnASM(3,:)) ) & Dakota.numDerivVars ~= 2 ) )
416
417    sprintf('Error: Bad number of variables in rosenbrock.m fn.\n');
418    Dakota.failure = 1;
419
420  elseif (Dakota.numFns > 2)
421
422    % 1 fn -> opt, 2 fns -> least sq
423    sprintf('Error: Bad number of functions in rosenbrock.m fn.\n');
424    Dakota.failure = 1;
425
426  else
427
428    if Dakota.numFns > 1
429      least_sq_flag = true;
430    else
431      least_sq_flag = false;
432    end
433
434    f0 = Dakota.xC(2)-Dakota.xC(1)*Dakota.xC(1);
435    f1 = 1.-Dakota.xC(1);
436
437    % **** f:
438    if (least_sq_flag)
439      if Dakota.directFnASM(1,1)
440        Dakota.fnVals(1) = 10*f0;
441      end
442      if Dakota.directFnASM(1,2)
443        Dakota.fnVals(2) = f1;
444      end
445    else
446      if Dakota.directFnASM(1,1)
447        Dakota.fnVals(1) = 100.*f0*f0+f1*f1;
448      end
449    end
450
451    % **** df/dx:
452    if (least_sq_flag)
453      if Dakota.directFnASM(2,1)
454        Dakota.fnGrads(1,1) = -20.*Dakota.xC(1);
455        Dakota.fnGrads(1,2) =  10.;
456      end
457      if Dakota.directFnASM(2,2)
458        Dakota.fnGrads(2,1) = -1.;
459        Dakota.fnGrads(2,2) =  0.;
460      end
461
462    else
463
464      if Dakota.directFnASM(2,1)
465        Dakota.fnGrads(1,1) = -400.*f0*Dakota.xC(1) - 2.*f1;
466        Dakota.fnGrads(1,2) =  200.*f0;
467      end
468
469    end
470
471    % **** d^2f/dx^2:
472    if (least_sq_flag)
473
474      if Dakota.directFnASM(3,1)
475        Dakota.fnHessians(1,1,1) = -20.;
476        Dakota.fnHessians(1,1,2) = 0.;
477        Dakota.fnHessians(1,2,1) = 0.;
478        Dakota.fnHessians(1,2,2) = 0.;
479      end
480      if Dakota.directFnASM(3,2)
481        Dakota.fnHessians(2,1:2,1:2) = 0.;
482      end
483
484    else
485
486      if Dakota.directFnASM(3,1)
487        fx = Dakota.xC(2) - 3.*Dakota.xC(1)*Dakota.xC(1);
488        Dakota.fnHessians(1,1,1) = -400.*fx + 2.0;
489        Dakota.fnHessians(1,1,2) = -400.*Dakota.xC(1);
490        Dakota.fnHessians(1,2,1) = -400.*Dakota.xC(1);
491        Dakota.fnHessians(1,2,2) =  200.;
492      end
493
494    end
495
496    Dakota.fnLabels = {'f1'};
497
498  end
499\end{verbatim}
500\end{tiny}
501\end{bigbox}
502\caption{Sample Matlab implementation of the Rosenbrock test function
503for the Dakota/Matlab interface.\label{advint:figure:matlabrosen}}
504\end{figure}
505
506\subsection{Python}\label{advint:existingdirect:python}
507
508Dakota's Python direct interface has been tested on Linux with Python
5092.x. When enabled, it allows Dakota to make function evaluation calls
510directly to an analysis function in a user-provided Python module.
511Data may flow between Dakota and Python either in multiply-subscripted
512lists or NumPy arrays.
513
514The Python direct interface must be enabled when compiling Dakota.
515Set the CMake variable \\ {\tt DAKOTA\_PYTHON:BOOL=ON}, and optionally
516{\tt DAKOTA\_PYTHON\_NUMPY:BOOL=ON} (default is ON) to use Dakota's
517NumPy array interface (requires NumPy installation providing
518arrayobject.h). If NumPy is not enabled, Dakota will use
519multiply-subscripted lists for data flow.
520
521An example of using the Python direct interface with both lists and
522arrays is included in
523\path{dakota/share/dakota/examples/linked_interfaces/Python}. The Python direct driver is
524selected with, for example,
525\begin{verbatim}
526  interface,
527    python
528      # numpy
529      analysis_drivers = 'python_module:analysis_function'
530\end{verbatim}
531where {\tt python\_module} denotes the module (file
532\path{python_module.py}) Dakota will attempt to import into the Python
533environment and {\tt analysis\_function} denotes the function to call
534when evaluating a parameter set. If the Python module is not in the
535directory from which Dakota is started, setting the {\tt PYTHONPATH}
536environment variable to include its location can help the Python
537engine find it.  The optional {\tt numpy} keyword indicates Dakota
538will communicate with the Python analysis function using numarray data
539structures instead of the default lists.
540
541Whether using the list or array interface, data from Dakota is passed
542(via kwargs) into the user function in a dictionary containing the
543entries shown in Table~\ref{advint:table:pythonparams}. The {\tt
544analysis\_function} must return a dictionary containing the data
545specified by the active set vector with fields ``fns'', ``fnGrads'',
546and ``fnHessians'', corresponding to function values, gradients, and
547Hessians, respectively. The function may optionally include a failure
548code in ``failure'' (zero indicates success, nonzero failure) and
549function labels in ``fnLabels''. See the linked interfaces example
550referenced above for more details.
551
552\begin{table}
553\centering
554\caption{Data dictionary passed to Python direct interface.}
555\label{advint:table:pythonparams}\vspace{2mm}
556\begin{tabular}{|l|l|}
557\hline
558\textbf{Entry Name} & \textbf{Description}  \\
559\hline
560functions  & number of functions (responses, constraints) \\
561variables  & total number of variables \\
562cv         & list/array of continuous variable values \\
563div        & list/array of discrete integer variable values \\
564drv        & list/array of discrete real variable values \\
565av         & single list/array of all variable values \\
566cv\_labels  & continuous variable labels \\
567div\_labels & discrete integer variable labels \\
568drv\_labels & discrete real variable labels \\
569av\_labels  & all variable labels \\
570asv        & active set vector \\
571dvv        & derivative variables vector \\
572analysis\_components & list of analysis components as strings \\
573currEvalId & current evaluation ID number \\
574\hline
575\end{tabular}
576\end{table}
577
578\section{Scilab Script and Direct Interfaces}\label{advint:scilab}
579
580Scilab is open source computation software which can be used to
581perform function evaluations during Dakota studies, for example to
582calculate the objective function in optimization. Dakota includes
583three Scilab interface variants: scripted, linked, and compiled. In
584each mode, Dakota calls Scilab to perform a function evaluation and
585then retrieves the Scilab results. Dakota's Scilab interface was
586contributed in 2011 by Yann Collette and Yann Chapalain. The
587Dakota/Scilab interface variants are described next.
588
589\subsection{Scilab Script Interface}
590
591Dakota distributions include a directory
592\path{dakota/share/dakota/examples/script_interfaces/Scilab} which demonstrates
593script-based interfacing to Scilab. The {\tt Rosenbrock} subdirectory
594contains four notable files:
595\begin{itemize}
596  \item \path{dakota_scilab_rosenbrock.in} (the Dakota input file),
597  \item \path{rosenbrock.sci} (the Scilab computation code),
598  \item \path{scilab_rosen_bb_simulator.sh} (the analysis driver), and
599  \item \path{scilab_rosen_wrapper.sci} (Scilab script).
600\end{itemize}
601
602The \path{dakota_scilab_rosenbrock.in} file specifies the Dakota
603study to perform. The interface type is external ({\tt fork}) and the
604shell script \path{scilab_rosen_bb_simulator.sh} is the analysis
605driver used to perform function evaluations.
606
607The Scilab file \path{rosenbrock.sci} accepts variable values and
608computes the objective, gradient, and Hessian values of the Rosenbrock
609function as requested by Dakota.
610
611The \path{scilab_rosen_bb_simulator.sh} is a short shell driver
612script, like that described in Section~\ref{interfaces:building}, that
613Dakota executes to perform each function evaluation. Dakota passes
614the names of the parameters and results files to this script as
615\texttt{\$argv[1]} and \texttt{\$argv[2]}, respectively. The
616\path{scilab_rosen_bb_simulator.sh} is divided into three parts:
617pre-processing, analysis, and post-processing.
618
619In the analysis portion, the \path{scilab_rosen_bb_simulator.sh}
620uses \path{scilab_rosen_wrapper.sci} to extract the current
621variable values from the input parameters file (\texttt{\$argv[1]})
622and communicate them to the computation code in
623\path{rosenbrock.sci}. The resulting objective function is
624transmitted to Dakota via the output result file (\texttt{\$argv[1]}),
625and the driver script cleans up any temporary files.
626
627The directory also includes PID and FemTRUSS examples, which are run
628in a similar way.
629
630\subsection{Scilab Linked Interface}
631
632The Dakota/Scilab linked interface allows Dakota to communicate
633directly with Scilab through in-memory data structures, typically
634resulting in faster communication, as it does not rely on files or
635pipes. In this mode, Dakota publishes a data structure into the
636Scilab workspace, and then invokes the specified Scilab
637analysis\_driver directly. In Scilab, this structure is an mlist
638(\url{http://help.scilab.org/docs/5.3.2/en\_US/mlist.html}), with the same
639fields as in the Matlab interface~\ref{advint:figure:matlabparams},
640with the addition of a field {\tt dakota\_type}, which is used to validate
641the names of fields in the data structure.
642
643The linked interface is implemented in source files {\tt
644  src/ScilabInterface.[CH]} directory, and must be enabled at compile
645time when building Dakota from source by setting {\tt
646  DAKOTA\_SCILAB:BOOL=ON}, and setting appropriate environment
647variables at compile and run time as described in \path{README.Scilab}
648in \path{dakota/share/dakota/examples/linked_interfaces/Scilab/}. This directory also
649contains examples for the Rosenbrock and PID problems.
650
651These examples are similar to those in \path{dakota/share/dakota/examples/script_interfaces}, with
652a few notable exceptions:
653\begin{enumerate}
654\item There is no shell driver script
655\item The Dakota input file specifies the interface as 'scilab',
656  indicating a direct, internal interface to Scilab using the Dakota
657  data structure described above:
658\begin{small}
659\begin{verbatim}
660interface,
661  scilab
662    analysis_driver = 'rosenbrock.sci'
663\end{verbatim}
664\end{small}
665\end{enumerate}
666
667
668\subsection{Scilab Compiled Interface}
669
670In ``compiled interface'' mode, the Dakota analysis driver is a
671lightweight shim, which communicates with the running application code
672such as Scilab via named pipes. It is similar to that for Matlab in
673\path{dakota/share/dakota/examples/compiled_interfaces/Matlab}, whose README is likely
674instructive. An example of a Scilab compiled interface is included in \\
675\path{dakota/share/dakota/examples/compiled_interfaces/Scilab/Rosenbrock}.
676
677As with the other Scilab examples, there are computation code and
678Dakota input files. Note the difference in the Dakota input file
679\path{rosenbrock.in}, where the analysis driver starts the dakscilab
680shim program and always evaluates functions, gradients, and Hessians.
681
682\begin{small}
683\begin{verbatim}
684interface,
685  fork
686    analysis_driver = '../dakscilab -d -fp "exec fp.sci" -fpp "exec fpp.sci"'
687    parameters_file = 'r.in'
688    results_file = 'r.out'
689    deactivate active_set_vector
690\end{verbatim}
691\end{small}
692
693The dakscilab executable results from compiling \path{dakscilab.c}
694and has the following behavior and options. The driver dakscilab
695launches a server. This server then facilitates communication between
696Dakota and Scilab via named pipes communication. The user can also use
697the first named pipe (\texttt{\$\{DAKSCILAB\_PIPE\}1}) to communicate
698with the server:
699\begin{small}
700\begin{verbatim}
701    echo dbg scilab_script.sce > ${DAKSCILAB_PIPE}1
702    echo quit > ${DAKSCILAB_PIPE}1
703\end{verbatim}
704\end{small}
705The first command, with the keyword 'dbg', launches the script
706\path{scilab_script.sci} for evaluation in Scilab. It permits to give
707instructions to Scilab. The second command 'quit' stops the server.
708
709The dakscilab shim supports the following options for the driver call:
710\begin{enumerate}
711  \item -s  to start the server
712  \item -si to run an init script
713  \item -sf to run a final script
714  \item -f -fp -fpp to specify names of objective function, gradient
715    and hessian, then load them.
716\end{enumerate}
717
718For the included PID example, the driver call is
719\begin{small}
720\begin{verbatim}
721    analysis_driver = '../dakscilab -d -si "exec init_test_automatic.sce;"
722                     -sf "exec visualize_solution.sce;" -f "exec f_pid.sci"'
723\end{verbatim}
724\end{small}
725
726Here there is an initialization script
727(\path{init_test_automatic.sce;}) which is launched before the
728main computation. It initializes a specific Scilab module called
729xcos. A finalization script to visualize the xcos solution is also
730specified (\path{visualize_solution.sce}). Finally, the objective
731function is given with the computation code called
732\path{f_pid.sci}.
733
734
735% LocalWords:  Scilab Yann Collette Chapalain Rosenbrock subdirectory dakota bb
736% LocalWords:  scilab rosenbrock rosen argv pre PID FemTRUSS workspace mlist 1i
737% LocalWords:  Matlab src ScilabInterface BOOL README dakscilab Hessians dbg si
738% LocalWords:  init fp fpp sce xcos pid AMPL nl ampl fma auxfiles tb extern CFD
739
740% LocalWords:  DirectFnApplicInterface ModelCenter ActiveSet Sandia Xyce API LD
741% LocalWords:  multiphysics Mathworks R14 R14SP3 CMake FILEPATH YOURPLATFORM py
742% LocalWords:  bashrc matlab myanalysis DirectApplicInterface subscripted NumPy
743% LocalWords:  arrayobject PYTHONPATH numpy numarray kwargs fns fnGrads cv drv
744% LocalWords:  fnHessians fnLabels asv dvv currEvalId
745