source: CPL/oasis3-mct_5.0/doc/UG2_Model_interfacing.tex @ 6328

Last change on this file since 6328 was 6328, checked in by aclsce, 17 months ago

First import of oasis3-mct_5.0 (from oasis git server, branch OASIS3-MCT_5.0)

  • Property svn:executable set to *
File size: 105.4 KB
Line 
1\newpage
2\chapter{Interfacing a component code with OASIS3-MCT}
3\label{sec_modelinterfacing}
4
5At run-time, OASIS3-MCT performs parallel exchange of coupling data between parallel
6components and sub-components and allows regridding (also called remapping or interpolation), time integration or accumulation and other transformations of these coupling fields.
7
8This chapter describes how to adapt the component codes to couple them through OASIS3-MCT.
9
10OASIS3-MCT supports coupling exchanges between parallel
11components and sub-components deployed in diverse
12configurations; the different possibilities and how to use
13the OASIS3-MCT library accordingly are described in section
14\ref{sec_deploy}.
15
16The OASIS3-MCT Application Programming Interface (API) includes different classes of modules or routines that are described in detail in sections \ref{API}, \ref{APIC} and \ref{APIpython} for Fortran, C and python codes respectively.
17In section \ref{sec_notes}, the reader will also find an overview of the MCT library (see \ref{subsec_MCT}) and additional notes on how to exchange scalars (see \ref{subsec_scalar}), how to to reproduce different coupling
18algorithms with OASIS3-MCT using the {\tt LAG} index (see \ref{subsec_lag}), and on the {\tt
19  SEQ} index (see \ref{subsec_sec}).
20
21\section{Configurations of components supported}
22\label{sec_deploy}
23
24Since OASIS3-MCT\_3.0 release, coupling exchanges between components and
25sub-components deployed in a much larger diversity of configurations
26than before
27are supported. This is illustrated on figure
28\ref{Fig_coupling_layouts_a} and how to use the OASIS3-MCT library
29accordingly is detailed on figure \ref{Fig_coupling_layouts_b}. All
30OASIS3-MCT API routines are also described in details in sections
31\ref{API}, \ref{APIC} and \ref{APIpython}.
32
33We call here an ``executable'' a compiled code
34  forming a part of or the whole coupled system. A ``component'' is the ensemble of
35  processes, or tasks, within the coupled system calling {\tt oasis\_init\_comp}
36  with the same {\tt comp\_name} argument (see section
37  \ref{init_comp}). A ``sub-component'' is the subset of tasks within a
38  component sending or receiving coupling fields on a specific grid;
39  of course, a component may have only one sub-component
40  that gathers all its tasks.
41
42In {\tt examples/tutoria\_communication} and {\tt
43  examples/regrid\_environment} one finds practical examples of how to use the OASIS3-MCT library
44(see sections \ref{subsec_tutorial} and \ref{subsec_regrid}).
45
46With OASIS3-MCT, it is currently possible to (the text between [ and ] refers to figure \ref{Fig_coupling_layouts_a}) :
47
48\begin{figure}
49  \includegraphics[scale=.6]{figures/coupling_layouts_a}
50  \caption{The different configuration of components supported by
51    OASIS3-MCT. Two executables {\tt exe1} and {\tt exe2} are running
52    concurrently on separate sets of MPI tasks (0-5 for {\tt exe1} and
53    6-37 for {\tt exe2}). Executable {\tt exe1} includes only one
54    component {\tt comp1} that has coupling fields defined on only one
55    grid {\tt grid1} (decomposed on all of its 6 tasks). Executable
56    {\tt exe2} includes 3 components, {\tt comp2}, {\tt comp3}, and
57    {\tt comp4} running concurrently respectively on tasks 6-11, 12-33
58    and 34-37. Component {\tt comp2} participates in the coupling with
59    fields defined on only one coupling grid {\tt grid2} (decomposed
60    on all of its 5 tasks) while {\tt comp4} does not participate at all in the coupling.
61Component {\tt comp3} has 3 sub-components, respectively exchanging
62coupling fields defined on {\tt grid3} (tasks 12-21), {\tt grid4}
63(tasks 22-30) and {\tt grid5} (tasks 12-26, therefore overlaping with both {\tt
64  grid3} and {\tt grid4}); finally, {\tt comp3} has 3 tasks (31-33)
65not involved in the coupling. Sub-components{\tt  exe2-comp3-grid3} and
66{\tt exe2-comp3-grid5}, or sub-components {\tt exe2-comp3-grid4} and
67{\tt exe2-comp3-grid5} are examples of coupling between sub-components running sequentially on overlapping sets of tasks.}
68  \label{Fig_coupling_layouts_a}
69\end{figure}
70
71\begin{figure}
72  \includegraphics[scale=.6]{figures/coupling_layouts_b}
73  \caption{The sequence of OASIS3-MCT calls that have to be implemented in the codes so to allow the configuration of components described on figure \ref{Fig_coupling_layouts_a}.
74Each MPI tasks has to call {\tt oasis\_init\_comp} once with the name
75of its component as $2^{nd}$ argument. As none of {\tt comp4} tasks is
76participating to the coupling, {\tt comp4} tasks calls {\tt
77  oasis\_init\_comp} with {\tt coupled=.false.”} as $4^{th}$ argument
78and does not call any other OASIS3-MCT routine. As some of {\tt comp3}
79tasks are participating in the coupling, all {\tt comp3} tasks have to
80call {\tt oasis\_init\_comp}, {\tt oasis\_get\_localcomm}, {\tt
81  oasis\_create\_couplcomm}, {\tt oasis\_enddef} and {\tt
82  oasis\_terminate} (these are the only routine to be called by {\tt
83  comp3} tasks 31-33 not participating to the coupling). To initialise
84the coupling exchanges, the tasks of a sub-component holding a field
85decomposed on a specific grid have to call the {\tt
86  oasis\_def\_partition} to express the decomposition of the grid,
87{\tt oasis\_def\_var} to declare the coupling field and {\tt
88  oasis\_enddef}. Finally, the tasks of a sub-component exchanging
89coupling fields have to call {\tt oasis\_put} and {\tt oasis\_get} accordingly.
90}
91  \label{Fig_coupling_layouts_b}
92\end{figure}
93
94\begin{itemize}
95
96\item to implement coupling exchanges between two components or
97  sub-components running concurrently on separate sets of tasks within two different executables [A, D, E, J];
98\item to implement coupling exchanges between two components or
99  sub-components running concurrently on separate sets of tasks within one same executable [B, F, I];
100\item to implement coupling exchanges within one executable between
101  two concurrent sub-components [C]
102\item to implement coupling exchanges within one executable between
103  two sub-components running sequentially on overlapping sets of tasks (i.e. a task can be coupled to itself calling both the ``put” and the ``get” of the exchange) [G, H]
104\item to have some tasks of a component not participating to the coupling exchanges [tasks 31-33 of {\tt exe2-comp3}]
105\item to have all processes of a component not participating to the coupling exchanges [{\tt exe2-comp4}, tasks 34-37]
106
107\end{itemize}
108
109The sequence of OASS3-MCT API routines that have to be called in the different cases is detailed on figure  \ref{Fig_coupling_layouts_b}.  These routines are also described in detail in the next section.
110
111\section{OASIS3-MCT Fortran API}
112\label{API}
113
114To interact with the rest of the coupled system, few calls of the
115OASIS3-MCT library routines, which sources can be found in {\tt
116  oasis3-mct/lib/psmile} directory, have to be implemented in
117component Fortran codes. They belong to the following classes:
118\begin{enumerate}
119\item Module to use (section \ref{subsubsec_Use})
120\item Initialisation (section \ref{subsubsec_Initialisation})
121\item Partition definition (section \ref{subsubsec_Partition})
122\item Grid data file definition (section \ref{subsubsec_griddef})
123\item Coupling-I/O field declaration (section
124  \ref{subsubsec_Declaration})
125\item End of definition phase (section
126  \ref{subsubsec_Endofdefinition})
127\item Coupling-I/O field sending and receiving (section
128  \ref{subsubsec_sendingreceiving})
129\item Termination (section \ref{subsubsec_Termination})
130\item Optional auxiliary routines (section
131  \ref{subsubsec_auxroutines})
132\end{enumerate}
133
134\subsection{Module to use in the code}
135\label{subsubsec_Use}
136% {Use}
137
138To use OASIS3-MCT library, a user needs to add in his code:
139
140\begin{itemize}
141
142\item {\tt USE mod\_oasis}
143
144  or
145
146\item {\tt USE mod\_prism}
147 
148\end{itemize}
149
150Both use statements are valid but only one needs to be used in a particular component. This single use statement provides all methods required. The methods, datatypes, and capabilities
151are identical for both the {\tt mod\_prism} or {\tt mod\_oasis}
152interfaces, the only difference being the name of the interface. The
153interface in module {\tt mod\_prism} is provided for backwards
154compatability with prior versions of OASIS3. Use of module {\tt
155  mod\_oasis} is recommended and provides access to a set of updated
156routine names that will continue to evolve in the future, always
157ensuring backward compatibility.  In the following sections, both the
158{\tt mod\_prism} and {\tt mod\_oasis} interface names are defined and
159a single description of the interface arguments is provided.
160
161\subsection{Initialisation}
162\label{subsubsec_Initialisation}
163% {Initialisation}
164
165\subsubsection{Coupling initialisation}
166\label{init_comp}
167
168\begin{itemize}
169
170\item {\tt CALL oasis\_init\_comp (compid, comp\_name, kinfo, coupled, commworld)}
171\item {\tt CALL prism\_init\_comp\_proto (compid, comp\_name, kinfo, coupled, commworld)}
172
173  \begin{itemize}
174  \item {\tt compid [INTEGER; OUT]}: returned component ID
175  \item {\tt comp\_name [CHARACTER; IN]}: component name; maximum length of 80 characters
176  \item {\tt kinfo [INTEGER; OUT]}: returned error code
177  \item {\tt coupled [LOGICAL, OPTIONAL; IN]}: flag to specify if the calling task is participating or not to the coupling ({\tt .true.} by default).
178  \item {\tt commworld [INTEGER, OPTIONAL; IN]} : optional argument to specify the global communicator gathering the components of the coupled model.
179  If not specified, {\tt MPI\_COMM\_WORLD} will be used as the default communicator to startup. All components of the coupled model must
180  specify the same {\tt commworld} argument.
181
182  \end{itemize}
183 
184  This routine must called by all tasks of all components whether or not they are involved in the coupling \footnote{The component may also call MPI\_Init explicitly, but if so, has to call it before calling {\tt oasis\_init\_comp}; in this case, the component also has to call MPI\_Finalize explicitly, but only after calling {\tt oasis\_terminate}.}.
185
186A component is defined as the ensemble of tasks  calling {\tt
187  oasis\_init\_comp} with the same {\tt comp} {\tt \_name}
188argument. If and only if all tasks of a component are excluded from
189the coupling, the logical {\tt coupled} can be set to {\tt .false.}
190for this component tasks; in this case, {\tt oasis\_init\_comp} is the
191only API routine that needs to be called by the component tasks. If at
192least one tasks of a component is participating to the coupling, all
193component tasks have to call {\tt oasis\_init\_comp} with {\tt
194  coupled=.true.} (which is the default); in this case, the component tasks not
195participating to the coupling will also have to call {\tt oasis\_get\_localcomm}, {\tt oasis\_create\_couplcomm}, {\tt
196  oasis\_enddef} and {\tt oasis\_terminate}.
197\end{itemize}
198
199\subsubsection{Communicator for internal parallelisation}
200\label{subsec_MPI1}
201
202\begin{itemize}
203\item {\tt CALL oasis\_get\_localcomm (local\_comm, kinfo )}
204\item {\tt CALL prism\_get\_localcomm\_proto (local\_comm, kinfo )}
205
206  \begin{itemize}
207  \item {\tt local\_comm [INTEGER; OUT]}: value of local communicator
208  \item {\tt kinfo [INTEGER; OUT]}: returned error code.
209  \end{itemize}
210
211  This routine returns the value of a local communicator gathering
212  only the tasks of the component (i.e. the tasks that called {\tt
213  oasis\_init\_comp} with the same {\tt comp\_name}
214argument).
215
216  This may be needed as all executables of the coupled system are started in a pseudo-MPMD
217  mode with MPI1 and therefore share automatically the same MPI\_COMM\_WORLD
218  communicator.  Another communicator has to be used for the internal
219  parallelisation of each component. OASIS3-MCT creates this local
220  communicator {\tt local\_comm} based on the value of the {\tt
221    comp\_name} argument in the {\tt oasis\_init\_comp} call.
222
223  Retrieving a local communicator {\tt local\_comm} is also needed if
224  {\tt oasis\_create\_couplcomm} is called, as {\tt local\_comm} is an argument of this routine (see below).
225
226  % With CLIM-MPI2, OASIS3 executable spawns the component
227  % executables at the beginning of the run; the components keep their
228  % internal parallelisation context unchanged with respect to their
229  % standalone mode. In this case, calling the
230  % prism\_get\_localcomm\_proto routine is useless but if called, the
231  % communicator MPI\_COMM\_WORLD will be returned as local
232  % communicator.
233\end{itemize}
234
235\begin{itemize}
236\item {\tt CALL oasis\_create\_couplcomm(icpl, local\_comm,
237    coupl\_comm, kinfo)}
238\item {\tt CALL prism\_create\_couplcomm(icpl, local\_comm,
239    coupl\_comm, kinfo)}
240  \begin{itemize}
241  \item {\tt icpl [INTEGER; IN]}: coupling process flag
242  \item {\tt local\_comm [INTEGER; IN]}: MPI communicator with all
243    processes of the component
244  \item {\tt coupl\_comm [INTEGER; OUT]}: returned MPI communicator
245    gathering only component processes participating in the coupling
246  \item {\tt kinfo [INTEGER; OUT; OPTIONAL]}: returned error code
247  \end{itemize}
248
249  This routine creates a coupling communicator for a subset of
250  processes. It is mandatory to call this routine if only a subset of
251  the component processes participate in the coupling (e.g. {\tt comp3} in figure
252    \ref{Fig_coupling_layouts_b}); in that case, the processes
253    involved in the coupling have to call it with icpl=1 while the
254    other have to call it with icpl = {\tt MPI\_UNDEFINED}. Argument
255   {\tt  local\_comm} is the MPI communicator associated with all processes
256    of the component returned by {\tt oasis\_get\_localcomm}. The new coupling
257    communicator is returned in {\tt coupl\_comm}.
258
259\end{itemize}
260If this communicator already exists in the code, the component should
261simply provide it to OASIS3-MCT with:
262
263\begin{itemize}
264\item {\tt CALL oasis\_set\_couplcomm(coupl\_comm, kinfo)}
265\item {\tt CALL prism\_set\_couplcomm(coupl\_comm, kinfo)}
266  \begin{itemize}
267  \item {\tt coupl\_comm [INTEGER; IN]}: MPI communicator gathering
268    only component processes participating in the coupling
269  \item {\tt kinfo [INTEGER; OUT; OPTIONAL]}: returned error code
270  \end{itemize}
271
272  This routine allows users to provide a local coupling communicator to
273  OASIS3-MCT, given that it already exists in the code. The value of
274  {\tt coupl\_comm} must be the value of this local coupling
275  communicator for the processes participating to the coupling and it
276  must be {\tt MPI\_COMM\_NULL} for processes not involved in the
277  coupling.
278\end{itemize}
279
280\subsection{Partition definition}
281\label{subsubsec_Partition}
282
283The coupling fields sent or received by a component are usually
284scattered among the different component processes. With OASIS3-MCT,
285all processes exchanging coupling data have to describe, in a global index space, the local
286partitioning of the different grids onto which the data is expressed (see \ref{subsubsec_griddef} for the grid definition).
287The processes not implied in the
288coupling do not have to call this routine (for backward compatibility with OASIS3-MCT\_2.0,
289they may still call it describing a null partition, i.e. with {\tt ig\_paral(:)=0}).
290
291\begin{itemize}
292
293  \vspace{0.2cm}
294\item {\tt CALL oasis\_def\_partition (il\_part\_id, ig\_paral,
295    kinfo, ig\_size, name)} or
296\item {\tt CALL prism\_def\_partition\_proto (il\_part\_id, ig\_paral,
297    kinfo, ig\_size, name)}
298
299  \begin{itemize}
300  \item {\tt il\_part\_id [INTEGER; OUT]}: partition ID
301  \item {\tt ig\_paral [INTEGER, DIMENSION(:), IN]}: vector of
302    integers describing the local grid partition in the global index space;
303    has a different expression depending on the type of the partition;
304    in OASIS3-MCT, 5 types of partition are supported: Serial (no
305    partition), Apple, Box, Orange, and Points (see below).
306  \item {\tt kinfo [INTEGER; OUT]}: returned error code.
307  \item {\tt ig\_size [INTEGER, OPTIONAL, IN]}: Optional argument, mandatory
308    if the coupling data is exchanged for only a subdomain of the
309    global grid; in this case, {\tt ig\_size} must give the total number of grid points. 
310  \item {\tt name [CHARACTER, OPTIONAL, IN]}: Optional argument associating a name to the partition,
311    mandatory if {\tt oasis\_def\_partition} is called either for a grid
312    decomposed not across all the processes of a component or if the related
313    oasis\_def\_partition are not
314    called in the same order on the different component processes;
315    this argument is new since OASIS3-MCT\_3.0 release and is linked to the
316    greater flexibility in the configuration of components supported
317    (see \ref{sec_deploy}); it has a maximum length of 120 characters.
318  \end{itemize}
319\end{itemize}
320
321\subsubsection{Serial (no partition)}
322
323This is the choice for a grid entirely supported by only one process . In this case, we have {\tt
324  ig\_paral(1:3)}:
325\begin{itemize}
326\item {\tt ig\_paral(1)} = 0 (indicates a Serial ``partition'')
327\item {\tt ig\_paral(2)} = 0
328\item {\tt ig\_paral(3)} = the total grid size.
329\end{itemize}
330
331\subsubsection{Apple partition}
332
333Each partition is a segment of the global domain, described by its
334global offset and its local size. In this case, we have {\tt
335  ig\_paral(1:3)}:
336\begin{itemize}
337\item {\tt ig\_paral(1)} = 1 (indicates an Apple partition)
338\item {\tt ig\_paral(2)} = the segment global offset
339\item {\tt ig\_paral(3)} = the segment local size
340\end{itemize}
341
342Figure \ref{apple_partition} illustrates an Apple partition over 3
343processes.
344\begin{figure}
345  \includegraphics[scale=.6]{figures/apple_new}
346  \caption{Apple partition. It is assumed here that the global index starts at
347    0 in the upper left corner.}
348  \label{apple_partition}
349\end{figure}
350
351
352\subsubsection{Box partition}
353
354Each partition is a rectangular region of the global domain, described
355by the global offset of its upper left corner, and its local extents
356in the X and Y dimensions. The global extent in the X dimension must
357also be given. In this case, we have {\tt ig\_paral(1:5)}:
358\begin{itemize}
359\item {\tt ig\_paral(1)} = 2 (indicates a Box partition)
360\item {\tt ig\_paral(2)} = the upper left corner global offset
361\item {\tt ig\_paral(3)} = the local extent in x
362\item {\tt ig\_paral(4)} = the local extent in y
363\item {\tt ig\_paral(5)} = the global extent in x.
364\end{itemize}
365
366Figure \ref{box_partition} illustrates a Box partition over 3
367processes.
368 
369\begin{figure}
370  \includegraphics[scale=.6]{figures/box_new}
371  \caption{Box partition. It is assumed here that the global index starts at 0
372    in the upper left corner.}
373  \label{box_partition}
374\end{figure}
375 
376\subsubsection{Orange partition}
377
378Each partition is an ensemble of segments in the global domain. Each
379segment is described by its global offset and its local extent.  In
380this case, we have {\tt ig\_paral(1:N)} where {\tt N = 2 + 2*number of
381  segments}
382% \footnote{As for the Box partition, the maximum number of segments
383%   is presently 338; it can be increased by modifying the value of
384%   {\tt Clim\_MaxSegments}}.
385
386\begin{itemize}
387\item {\tt ig\_paral(1)} = 3 (indicates a Orange partition)
388\item {\tt ig\_paral(2)} = the total number of segments for the
389  partition (limited to 200 presently, see note for ig\_paral(4) for
390  Box partition above)
391\item {\tt ig\_paral(3)} = the first segment global offset
392\item {\tt ig\_paral(4)} = the first segment local extent
393\item {\tt ig\_paral(5)} = the second segment global offset
394\item {\tt ig\_paral(6)} = the second segment local extent
395\item ...
396\item {\tt ig\_paral(N-1)} = the last segment global offset
397\item {\tt ig\_paral(N)} = the last segment local extent
398\end{itemize}
399
400Figure \ref{orange_partition} illustrates an Orange partition with 3
401segments for one process. The other process partitions are not
402illustrated.
403
404\begin{figure}
405  \includegraphics[scale=.6]{figures/orange_new}
406  \caption{Orange partition for one process. It is assumed here that
407    the global index starts at 0 in the upper left corner.}
408  \label{orange_partition}
409\end{figure}
410
411\subsubsection{Points partition}
412\label{subsubsec_pointspart}
413
414This partition is a list of global indices associated with each
415process.  The index naming
416is arbitrary but must be consistent between all processes involved
417in the partition description.  In
418this case, we have {\tt ig\_paral(1:N)} where {\tt N = 2 + number of
419  points}
420% \footnote{As for the Box partition, the maximum number of segments
421%   is presently 338; it can be increased by modifying the value of
422%   {\tt Clim\_MaxSegments}}.
423
424\begin{itemize}
425\item {\tt ig\_paral(1)} = 4 (indicates a Points partition)
426\item {\tt ig\_paral(2)} = number of points in the partition
427\item {\tt ig\_paral(3)} = the first global index
428\item {\tt ig\_paral(4)} = the second global index
429\item ...
430\item {\tt ig\_paral(N)} = the last global index
431\end{itemize}
432
433% {Partition definition}
434\subsection{Grid data file definition}
435\label{subsubsec_griddef}
436
437Grid data files are required by OASIS3-MCT for specific
438operations, see sections \ref{sec_transformations} and \ref{subsec_griddata}.
439These grid data files can be created by the
440user before the run or can be written directly at run time by the components with the following routines.
441If a grid data files does not exist, the corresponding routine will create it; if the grid data file exists, the routine can be used to {\bf add} grid definition fields but it will not {\bf overwrite} grid definition fields already existing in the file with the same grid name.
442
443These routines can be called only by one component process to write the whole grid or by each process holding a part of a grid. In the former case, optional argument {\tt il\_part\_id} is not needed and the arrays handling the longitudes of the grid points or corners ({\tt lon}, {\tt clon}), the latitudes of the grid points or corners ({\tt lat}, {\tt clat}), the masks ({\tt mask}), fracs ({\tt frac}), and areas ({\tt area}) of the grid cells need to cover the whole grid; in the later case, the {\tt il\_part\_id} returned by {\tt oasis\_def\_partition} needs to be provided as input argument and the arrays need to cover only the local partition of the grid.
444
445The field names in the {\em grids.nc}, {\em masks.nc}, and {\em
446  areas.nc} follow a well-defined convention.  The fields are normally
447two-dimensional, and each field name consists of a grid acronym followed by a string that identifies the field.  For instance, the center latitudes for the grid torc will be called torc.lat and the center longitudes will be called torc.lon in the netcdf file.  The {\em grids.nc} file contains the center latitudes (.lat) and longitudes (.lat) as well as the corner latitudes (.cla) and corner longitudes (.clo).  The corner fields have a third dimension associated with the number of corners per gridcell.  The {\em area.nc} file constains the area field (.srf).  The {\em masks.nc} file contains the mask (.msk) and frac (.frc) fields.
448
449\begin{itemize}
450
451\item {\tt CALL oasis\_start\_grids\_writing (flag)} or
452\item {\tt CALL prism\_start\_grids\_writing (flag)}
453  \begin{itemize}
454  \item {\tt flag [INTEGER; OUT]}: always 1
455  \end{itemize} 
456Must be called to start the grid writing process.
457 
458  \vspace{0.2cm}
459\item {\tt CALL oasis\_write\_grid (cgrid, nx\_global, ny\_global, lon, lat, il\_part\_id)}
460\item {\tt CALL prism\_write\_grid (cgrid, nx\_global, ny\_global, lon, lat, il\_part\_id)}
461       
462  \begin{itemize}
463  \item {\tt cgrid [CHARACTER; IN]}: grid name prefix (see
464    \ref{subsec_namcouplesecond} and \ref{subsec_griddata}); maximum length of 64 characters (4 are usually used for historical reasons)
465  \item {\tt nx\_global [INTEGER; IN]} : first dimension of the global grid
466  \item {\tt ny\_global [INTEGER; IN]} : second dimension of the global grid ({\tt =1} if the grid is expressed as a 1D vector)
467  \item {\tt lon [REAL, DIMENSION(nx,ny); IN]} : single or double real array of longitudes covering the whole grid ({\tt nx=nx\_global}, {\tt ny=ny\_global}) or only the local partition (degrees East).
468  \item {\tt lat [REAL, DIMENSION(nx,ny); IN]} : single or double real array of latitudes covering the whole grid ({\tt nx=nx\_global}, {\tt ny=ny\_global}) or only the local partition (degrees North)
469\item  {\tt il\_part\_id [INTEGER, OPTIONAL; IN]}: partition ID returned by {\tt oasis\_def\_partition}, see \ref{subsubsec_Partition}; needed if each component task holding a part of a decomposed grid writes its own part of the grid.
470  \end{itemize}
471
472  Writes the component grid longitudes and latitudes. Longitudes must be
473  given in degrees East in the interval -360.0 to 720.0. Latitudes
474  must be given in degrees North in the interval -90.0 to 90.0. Note
475  that if some grid points overlap, it is recommended to define those
476  points with the same number (e.g. 90.0 for both, not 450.0 for one
477  and 90.0 for the other) to ensure automatic detection of overlap by
478  OASIS3-MCT (which is essential to have a correct conservative
479  remapping \texttt{SCRIPR/CONSERV}, see section \ref{subsec_interp}).
480
481  \vspace{0.2cm}
482\item {\tt CALL oasis\_write\_corner (cgrid, nx\_global, ny\_global, nc, clon, clat, il\_part\_id))}
483\item {\tt CALL prism\_write\_corner (cgrid, nx\_global, ny\_global, nc, clon, clat, il\_part\_id))}
484
485  \begin{itemize}
486  \item {\tt cgrid }, {\tt nx\_global }, {\tt ny\_global }, {\tt il\_part\_id }: as for {\tt oasis\_write\_grid}
487
488  \item {\tt nc [INTEGER; IN]} : number of corners per grid cell (can be any number)
489  \item {\tt clon [REAL, DIMENSION (nx,ny,nc);IN]} : single or double real array of corner
490    longitudes covering the whole grid ({\tt nx=nx\_global}, {\tt ny=ny\_global}) or only the local partition (in degrees\_East)
491  \item {\tt clat [REAL, DIMENSION (nx,ny,nc);IN]} : single or double real array of corner
492    latitudes covering the whole grid ({\tt nx=nx\_global}, {\tt ny=ny\_global}) or only the local partition (in degrees\_North)
493  \end{itemize}
494
495  Writes the grid cell corner longitudes and latitudes
496  (counterclockwise sense). Longitudes must be given in degrees East
497  in the interval -360.0 to 720.0. Latitudes must be given in degrees
498  North in the interval -90.0 to 90.0. Note also that cells larger
499  than 180.0 degrees in longitude are not supported. Writing of
500  corners is optional as corner information is needed only for {\tt
501    SCRIPR/CONSERV} (see section \ref{subsec_interp}). If called,
502  needs to be called after {\tt oasis/prism\_write\_grid}.
503
504  \vspace{0.2cm}
505\item {\tt CALL oasis\_write\_mask (cgrid, nx\_global, ny\_global, mask, il\_part\_id, companion)}
506\item {\tt CALL prism\_write\_mask (cgrid, nx\_global, ny\_global, mask, il\_part\_id, companion)}
507
508  \begin{itemize}
509  \item {\tt cgrid }, {\tt nx\_global }, {\tt ny\_global }, {\tt il\_part\_id }: as for {\tt oasis\_write\_grid}
510  \item {\tt mask [INTEGER, DIMENSION(nx,ny) ;IN]} : mask array
511    covering the whole grid ({\tt nx=nx\_global}, {\tt ny=ny\_global})
512    or only the local partition. Be careful about OASIS3-MCT
513    historical convention (!): 0 = not masked (i.e. active), 1 =
514    masked (i.e. not active).
515  \item {\tt companion [CHARACTER ;IN; OPTIONAL]} : the character
516    string value associated with the mask field attribute {\tt coherent\_with\_grid}'.  This will be written to the {\em masks.nc} netcdf file with the mask field.  It is purely informational and used in cases where the mask field is derived from or consistent with another grid.
517  \end{itemize}
518  Writes the component grid mask.  The mask field should be consistent
519  with the frac field (see below) and will define the 0/1 mask of the
520  grid cell.  The mask field is used by both the {\tt SCRIPR} map
521  generation function and in the global {\tt CONSERV} operations if defined.
522  The mask field is written to the {\em masks.nc} file.
523 
524  \vspace{0.2cm}
525\item {\tt CALL oasis\_write\_frac (cgrid, nx\_global, ny\_global, frac, il\_part\_id, companion)}
526\item {\tt CALL prism\_write\_frac (cgrid, nx\_global, ny\_global, frac, il\_part\_id, companion)}
527
528  \begin{itemize}
529  \item {\tt cgrid }, {\tt nx\_global }, {\tt ny\_global }, {\tt il\_part\_id }: as for {\tt oasis\_write\_grid}
530  \item {\tt frac [REAL, DIMENSION(nx,ny) ;IN]} : single or double real frac array covering the whole grid ({\tt nx=nx\_global}, {\tt ny=ny\_global}) or only the local partition. 
531  \item {\tt companion [CHARACTER ;IN; OPTIONAL]} : the character
532    string value associated with the frac field attribute
533    ``coherent\_with\_grid''.  It should refer to the acronym of
534    the grid which mask was used to define the fraction of the current
535    grid (see section \ref{subsec_cooking}). This will be written to the {\em
536      masks.nc} NetCDF file with the fraction field.  It is purely
537    informational and used in cases where the frac field is derived
538    from or consistent with another grid mask.
539   
540  \end{itemize}
541  Writes the component grid cell fractions.  This should be consistent
542  with the mask field and defines the fraction of the grid cell that
543  is active (i.e. not masked).  The fraction field is only used in the
544  global {\tt CONSERV} operations. Either the mask or fractions must be defined for that operation.  If both are defined, they must be consistent; OASIS3-MCT will abort if they are not coherent or if both are missing.  Note that by OASIS3-MCT conventions for the mask, a gridcell with mask=0 (active) should have a fractions greater than 0 and a gridcell with mask=1 (inactive) should have a fractions equal to 0.  The fraction field is written to the {\em masks.nc} file.
545
546  \vspace{0.2cm}
547\item {\tt CALL oasis\_write\_area (cgrid, nx\_global, ny\_global, area, il\_part\_id)}
548\item {\tt CALL prism\_write\_area (cgrid, nx\_global, ny\_global, area, il\_part\_id)}
549
550  \begin{itemize}
551  \item {\tt cgrid }, {\tt nx\_global }, {\tt ny\_global }, {\tt il\_part\_id }: as for {\tt oasis\_write\_grid}
552  \item {\tt area [REAL, DIMENSION(nx,ny); IN]} : single or double real array of grid cell
553    areas covering the whole grid ({\tt nx=nx\_global}, {\tt ny=ny\_global}) or only the local partition
554  \end{itemize}
555  Writes of the component grid cell areas. Needed for some {\tt SCRIPR} options and for the {\tt CONSERV}
556  operation (see section \ref{subsec_cooking}).    The area field is written to the {\em areas.nc} file.
557  The surfaces may be given in any units but they must be the same on
558  the source and target sides. Furthermore they must be in square radians if the True Area (TR) correction is activated, see section 4.3.
559
560  \vspace{0.2cm}
561\item {\tt CALL oasis\_write\_angle (cgrid, nx\_global, ny\_global, angle, il\_part\_id)}
562\item {\tt CALL prism\_write\_angle (cgrid, nx\_global, ny\_global, angle, il\_part\_id)}
563
564  \begin{itemize}
565  \item {\tt cgrid }, {\tt nx\_global }, {\tt ny\_global }, {\tt il\_part\_id }: as for {\tt oasis\_write\_grid}
566  \item {\tt angle [REAL, DIMENSION(nx,ny); IN]} : single or double real array of grid cell
567    angles covering the whole grid ({\tt nx=nx\_global}, {\tt ny=ny\_global}) or only the local partition
568  \end{itemize}
569  Writes of the component grid cell angles.  The angle field is written to the {\em grids.nc} file.  This field does not play a role in OASIS3-MCT implementation and is never needed.
570
571
572  \vspace{0.2cm}
573\item {\tt CALL prism\_terminate\_grids\_writing()} or
574\item {\tt CALL oasis\_terminate\_grids\_writing()}
575
576\end{itemize}
577
578The creation of the different grid data files is completed in the routine
579{\tt oasis\_enddef}.
580
581\subsection{Coupling field declaration}
582\label{subsubsec_Declaration}
583
584All processes of a component that send or receive a coupling field, or a part of it, needs to declare the coupling field.
585 
586Processes not implied in the
587coupling do not have to call this routine at all (for backward
588compatibility with OASIS3-MCT\_2.0, they may still call it with any {\tt name} and {\tt il\_part\_id}).
589
590\begin{itemize}
591
592\item {\tt CALL oasis\_def\_var (var\_id, name, il\_part\_id,
593    var\_nodims, kinout, \newline var\_type,
594    kinfo)} or
595   
596\item {\tt CALL oasis\_def\_var (var\_id, name, il\_part\_id,
597    var\_nodims, kinout, \newline var\_actual\_shape, var\_type,
598    kinfo)} or
599
600\item {\tt CALL prism\_def\_var\_proto(var\_id, name, il\_part\_id,
601    var\_nodims, kinout, var\_type, kinfo)} or
602
603\item {\tt CALL prism\_def\_var\_proto(var\_id, name, il\_part\_id,
604    var\_nodims, kinout, var\_actual\_shape, var\_type, kinfo)}
605
606  \begin{itemize}
607  \item {\tt var\_id [INTEGER; OUT]}: coupling field ID.  Note that all
608      coupling fields appearing in the {\it namcouple} must be defined with
609      a call to {\tt oasis\_def\_var}; not doing so would lead to an abort. 
610      But all fields defined with a call to {\tt oasis\_def\_var} must not necessarily
611      appear in the {\it namcouple}. If a field does not appear in the {\it namcouple},
612      the {\tt var\_id} returned by the {\tt oasis\_def\_var} will be equal to -1;
613      the value of the {\tt var\_id} should be tested and the corresponding
614      {\tt oasis\_put} and {\tt oasis\_get} should not be called if {\tt var\_id} 
615      equals -1. These constraints are imposed to avoid that a typo in the {\it namcouple}
616       would lead to coupling exchanges not corresponding to what the user intends to activate.
617  \item {\tt name [CHARACTER; IN]}: field symbolic name (as in the
618    {\it namcouple}); maximum length of 80 characters
619  \item {\tt il\_part\_id [INTEGER; IN]}: partition ID returned from {\tt oasis\_def\_partition} (see section
620    \ref{subsubsec_Partition})
621  \item {\tt var\_nodims [INTEGER, DIMENSION(2); IN]}: is an integer array of size two. 
622    The first element, var\_nodims(1), is not used anymore in OASIS3-MCT, so
623    its value can be anything; The second element, var\_nodims(2),
624    is the number of fields in a bundle (this will be 1 for unbundle
625    fields or greater than 1 for fields that are bundled; note that
626    if var\_nodims(2)=0, it will be automatically reset to 1 in the
627    routine, to ensure backward compatibility).
628  \item {\tt kinout [INTEGER; IN]}: {\tt OASIS\_In} or {\tt PRISM\_In}
629    (i.e. = 21) for fields received by the component; {\tt OASIS\_Out},
630    {\tt PRISM\_Out} (i.e. = 20) for fields sent by the component
631    \footnote{Parameters OASIS\_In, PRISM\_In, OASIS\_Out, PRISM\_Out
632      are defined in
633      oasis3-mct/lib/psmile/src/mod\_oasis\_parameters.F90}.
634  \item {\tt var\_actual\_shape [INTEGER, DIMENSION(2*id\_var\_nodims(1)), IN]}:
635    is not used anymore.  The interface has recently
636    been overloaded, and this argument is no longer required.  But
637    for backwards compatibility, it can still be passed; if so, it has to be a vector of integers of any length (for simplicity we advise to pass a vector of length 1).
638  \item {\tt var\_type [INTEGER; IN]}: type of coupling field array;
639    put {\tt OASIS\_Real} or {\tt PRISM\_Real} (i.e. = 4) for single
640    or double precision real arrays.  All coupling data is treated as
641    double precision in the coupling layer, but conversion to or from
642    single precision data is supported in the interface.
643  \item {\tt kinfo [INTEGER; OUT]}: returned error code.
644  \end{itemize}
645\end{itemize}
646% {coupling field declaration}
647
648\subsection{End of definition phase}
649\label{subsubsec_Endofdefinition}
650All processes of components at least partly involved in the coupling (e.g. {\tt comp3} in figure
651    \ref{Fig_coupling_layouts_b}) have to close the definition phase. Different configurations of components and corresponding use of {\tt oasis\_enddef} are described in section \ref{sec_deploy} and on figures \ref{Fig_coupling_layouts_a} and \ref{Fig_coupling_layouts_b}.
652\begin{itemize}
653\item {\tt CALL oasis\_enddef (kinfo)}
654\item {\tt CALL prism\_enddef\_proto(kinfo)}
655  \begin{itemize}
656  \item kinfo [INTEGER; OUT]: returned error code.
657  \end{itemize}
658\end{itemize}
659
660% {End of definition phase}
661
662\subsection{Sending ``put'' and receiving ``get'' actions}
663\label{subsubsec_sendingreceiving}
664
665This section describes how to send (put) and receive (get) fields through
666OASIS-MCT API.  This coupling interface supports several ranks and
667types of coupling fields.  First, the fields passed to the interface can be
6684 byte or 8 byte reals. The field decomposition must be
669consistent with the decomposition defined by the grid partition (see
670\ref{subsubsec_Partition})\footnote{But the decomposition of a
671  field does not necesseraly have to match the rank of the grid
672  partition description (i.e. it can be expressed in either 1D or 2D).} and the fields
673can be bundled (i.e. have an extra non-spatial dimension for something
674like different ice categories).  The bundle dimension is always the last dimension in the
675field passed to the get and put routines.  And the size of the bundle dimension
676must match the value defined for the variable in var\_nodims(2) in the
677{\tt oasis\_def\_var} interface (see section \ref{subsubsec_Declaration}).
678
679So in general, the fields passed into the get and put interface can have rank
6801, 2, or 3 and include the following possible options where fld can be a 4
681byte or 8 byte real array.
682
683\begin{itemize}
684\item 1D, fld(:) = a single, unbundle field of decomposition rank 1.
685\item 2D, fld(:,:) = a single, unbundle field of decomposition rank 2.
686\item 1D bundle, fld(:,:) = a bundle set of fields of decomposition rank 1.
687  The size of the second dimension must equal the number of fields in the
688  bundle, defined by var\_nodims(2) in the {\tt oasis\_def\_var} interface.
689\item 2D bundle, fld(:,:,:) = a bundle set of fields of decomposition rank 2.
690  The size of the third dimension must equal the number of fields in the
691  bundle, defined by var\_nodims(2) in the {\tt oasis\_def\_var} interface.
692\end{itemize}
693
694%With bundle fields, it is important that the number of fields in the bundle match
695%in the two models being coupled.  This requires that the var\_nodims(2) values
696%in the send and receive model match the bundle dimension of that bundle coupling field.
697Different bundle fields
698can have different numbers of fields, but for a given bundle field, the
699number of fields must match on the send and receive side.  This is explicitly
700checked within the coupling layer and will lead to an abort if not done correctly.
701It is possible to define a 1D bundle or 2D bundle field  with a bundle dimension of 1, for a bundle that contains only one single field.
702
703Finally, the bundle field option can be used to
704bundle together multi-level variables, multiple related fields, and other types
705of fields.  The fields must share a common partition and common namcouple settings (e.g. interpolation)
706to be bundle.  While this is a useful feature for multi-level fields, {\bf this does not mean
707that 3D interpolation is supported}.
708Each field in the bundle is
709treated internally as a separate field in the coupling layer without
710any information about the relationship between the fields in the bundle.  In fact,
711the bundle field variables are internally renamed and a field number is appended
712to the variable name to keep track of the distinct fields in the bundle.  That
713updated variable name will appear in restart and output files.
714
715
716\subsubsection{Sending a coupling (or I/O) field or writing a coupling restart file}
717\label{prismput}
718
719In the component time step loop, each process sends its part of the
720coupling (or I/O) field.
721
722\begin{itemize}
723\item {\tt CALL oasis\_put (var\_id,date,fld1,info,fld2,fld3,fld4, fld5,write\_restart)}
724\item {\tt CALL prism\_put\_proto(var\_id, date, fld1, info, fld2,
725    fld3, fld4, fld5, write\_restart)}
726  \begin{itemize}
727  \item {\tt var\_id [INTEGER; IN]}: field ID (returned from corresponding {\tt
728      oasis\_def\_var}, see section \ref{subsubsec_Declaration})
729  \item {\tt date [INTEGER; IN]}: number of seconds (or any other time
730    units as long as the same are used in all components and in the {\it
731      namcouple}) at the time of the call (by convention at the
732    beginning of the timestep)
733  \item {\tt fld1 [REAL, IN]}: coupling (or I/O) field array; can be
734    1D, 2D, bundle 1D, or bundle 2D, see above.
735  \item {\tt info [INTEGER; OUT]}: returned info code:
736    \begin{itemize}
737    \item {\tt OASIS\_Sent} (=4) if the field was sent to another component
738    \item {\tt OASIS\_LocTrans} (=5) if the field was only used in a time
739      transformation (not sent, not output)
740    \item {\tt OASIS\_ToRest} (=6) if the field was written to a restart
741      file only
742    \item {\tt OASIS\_Output} (=7) if the field was written to an output
743      file only
744    \item {\tt OASIS\_SentOut} (=8) if the field was both written to an
745      output file and sent to another component
746    \item {\tt OASIS\_ToRestOut} (=9) if the field was written both to a
747      restart file and to an output file.
748    \item {\tt OASIS\_WaitGroup} (=14) if the field was not sent because it is part of a group.
749    It will be sent only when the {\tt oasis\_put} of the last field in the group will be called; however, the field is buffered and therefore the field array can be modified in the component code when returning from the oasis\_put call.
750    \item {\tt OASIS\_Ok} (=0) otherwise and no error occurred.
751    \end{itemize}
752  \item {\tt fld2 [REAL, IN, OPTIONAL]}: optional $2^{nd}$ coupling
753    field array; can be 1D, 2D, bundle 1D, or bundle 2D.  Rank and size
754    must match {\tt fld1}.
755  \item {\tt fld3 [REAL, IN, OPTIONAL]}: optional $3^{rd}$ coupling
756    field array; can be 1D, 2D, bundle 1D, or bundle 2D.  Rank and size
757    must match {\tt fld1}.
758  \item {\tt fld4 [REAL, IN, OPTIONAL]}: optional $4^{th}$ coupling
759    field array; can be 1D, 2D, bundle 1D, or bundle 2D.  Rank and size
760    must match {\tt fld1}.
761  \item {\tt fld5 [REAL, IN, OPTIONAL]}: optional $5^{th}$ coupling
762    field array; can be 1D, 2D, bundle 1D, or bundle 2D.  Rank and size
763    must match {\tt fld1}.
764  \item {\tt write\_restart [LOGICAL, IN, OPTIONAL]}: optional argument to write an
765    intermediate restart file associated with the variable {\tt
766      var\_id} at the current timestep (see below).
767  \end{itemize}
768\end{itemize}
769
770To ensure a proper use of the {\tt oasis\_put}, one has to take care
771of the following aspects:
772
773\begin{itemize}
774
775\item A $2^{nd}$, $3^{rd}$, $4^{th}$ and $5^{th}$ source field can be
776  passed as optional arguments. If so, the $2^{nd}$, $3^{rd}$,
777  $4^{th}$ and $5^{th}$ set of weights present in the remapping file
778  will be applied, respectively (see section \ref{subsec_mapdata} for
779  the remapping file format). This will be used for example for the {\tt
780    SCRIPR/BICUBIC} remapping for which a $1^{st}$, $2^{nd}$,
781  $3^{rd}$, $4^{th}$ set of weights should be respectively applied to
782  the field value, its gradient in the first dimension, its gradient
783  in the second dimension, and its cross-gradient in that order. For  {\tt
784    SCRIPR/BICUBIC}{\tt fld2}, {\tt fld3} and {\tt fld4} are
785  therefore mandatory.
786
787  This will be used also for the {\tt CONSERV/SECOND} for which a 1st, 2nd, 3rd
788  set of weights should be respectively applied to the field value, its
789  gradient with respect to the latitude ($\theta$) $\frac{\delta f}{\delta \theta}$
790  and its gradient with respect to the longitude ($\phi$) $\frac{1}{cos \theta}\frac{\delta f}{\delta \phi}$ 
791  in that order. For  {\tt CONSERV/SECOND}{\tt fld2} and {\tt fld3} are
792  therefore mandatory.
793
794  Bicubic and higher order
795  remapping are therefore supported given that the higher order fields
796  are provided at each time step as {\tt oasis\_put} arguments. Note
797  that if {\tt fld3}, or {\tt fld4}, or {\tt fld5} are passed, {\tt fld2}, or {\tt fld3} and
798  {\tt fld2}, or {\tt fld4} and {\tt fld3} and {\tt fld2} must also be passed respectively.
799 
800\item Note that from OASIS3-MCT\_4.0 onwards,
801the number of weights in the remapping file and the number of fields in the coupling restart file
802(when such a file is needed) must strictly match the number of source fields passed to the
803oasis\_put .
804
805%\item Trying to send with {\tt oasis\_put} a field declared with a
806%  {\tt oasis\_def\_var} but not defined in the configuration file {\it
807%    namcouple} will lead to an abort. In this case, the field ID
808%  returned by the {\tt oasis\_def\_var} is equal to -1 and the
809%  corresponding {\tt oasis\_put} should not be called.
810
811\item This routine may be called by the component at each timestep. The
812  convention for the {\tt date} argument is to indicate the time at
813  the beginning of the timestep. The sending is actually performed
814  if the time obtained by adding the field lag ({\tt LAG} in the
815  {\em namcouple}, if any, with {\tt LAG=0} by default) to the {\tt date} corresponds to a time at which it
816  should be activated, given the coupling or I/O period indicated by
817  the user in the {\it namcouple} (see section \ref{sec_namcouple}).
818
819\item By convention, the first coupling
820  of a run occurs at {\tt date=0}.
821
822\item For a coupling
823  field with a positive lag, the coupling restart file (see section
824  \ref{subsec_restartdata}) is automatically overwritten by the {\tt
825    oasis\_put} when the {\tt date+LAG=runtime}.
826
827  % \item A field will not be sent at all if its coupling (or I/O)
828  %   period indicated in the {\it namcouple} is greater than the
829  %   total run time.
830
831\item The total run time should match an integer number of coupling
832  periods.
833
834\item If a local time transformation is indicated for the field by the
835  user in the {\it namcouple} (INSTANT, AVERAGE, ACCUMUL, T\_MIN or
836  T\_MAX, see section \ref{sec_transformations}), it is automatically
837  performed and the resulting field is finally sent at the coupling or
838  I/O frequency.  For non-instantaneous transformations, partially
839  transformed fields will be written to the restart file at the end of
840  the run for use on the next component startup, when needed.
841
842\item A coupling field sent by a source component can be
843  associated with more than one target field and component, with different entries in the {\it namcouple} configuration
844  file. In that case, the source component needs to send the field only
845  once and the corresponding data will arrive at multiple targets as
846  specified in the {\it namcouple}. Different coupling frequencies and
847  transformations are allowed for different coupling exchanges of the
848  same field. If coupling restart files are required (either if a {\tt
849    LAG} or if a {\tt LOCTRANS} transformation is specified), it is
850  mandatory to specify different files for the different fields.
851
852\item Trying to send with {\tt oasis\_put} a field declared with a {\tt
853  oasis\_def\_var} but not defined in the configuration file {\it
854  namcouple} will lead to an abort. When a field is not defined in the {\it namcouple}, the field ID
855returned by the {\tt oasis\_def\_var} is equal to -1 and the
856corresponding {\tt oasis\_put} should not be called.
857
858\item Coupling multiple fields via a single communication is supported through colon delimited field lists in the namcouple (see \ref{subsubsec_secondEXPORTED}).
859All fields will use the namcouple settings for that entry. In the component
860model codes, these fields are still sent ({\bf â€œput”}) one at a time. Inside
861OASIS3-MCT, the fields are stored and a single mapping and send instruction is executed
862for all fields. This is useful to reduce communication costs by aggregating multiple fields into a single communication when multiple fields have the same coupling transformations.
863
864This option does not put any constraint
865on the order of the related {\tt oasis\_put} and {\tt oasis\_get} in the codes.
866
867As they appear in one single entry line, these fields must share the same coupling restart file
868but this restart file may contain other fields.
869
870\item The optional argument, {\tt write\_restart}, in the {\tt oasis\_put} routine is {\bf false} by default.
871If a user sets that argument to true, an ``intermediate'' restart is written for that field {\bf only for that timestep}. The {\tt write\_restart} 
872saves the data that exists at the time of the call, taking into account LOCTRANS operations. In
873cases where multiple fields are coupled as a single operation in the model (indicated via a list of colon delimited
874fields in the {\it namcouple}, see \ref{subsubsec_secondEXPORTED}), users are encouraged to specify
875the {\tt write\_restart} flag on ALL {\tt oasis\_put} calls at a given
876time for this set of fields.
877Restarts are created with a 9 digit timestamp in
878their filename, corresponding to the time in seconds of the oasis\_put
879call, e.g. TA000003600\_rst4.nc or TC000014400\_rst4.nc.
880A restart file that starts with TA is a restart file associated
881with LOCTRANS operations; a restart
882file that starts with TC is a restart file associated with
883coupling operations. The coupling restart filename
884defined in the namcouple (e.g. rst4.nc) is used to generate the
885filename of these intermediate restart files.
886\end{itemize}
887
888\subsubsection{Receiving a coupling (or I/O) field}
889
890In the component time stepping loop, each process receives its part of the
891coupling field.
892
893\begin{itemize}
894 
895\item {\tt CALL oasis\_get (var\_id, date, fld, info)}
896\item {\tt CALL prism\_get\_proto(var\_id, date, fld, info)}
897  \begin{itemize}
898  \item {\tt var\_id [INTEGER; IN]}: field ID (returned by the corresponding
899    {\tt oasis\_def\_var})
900  \item {\tt date [INTEGER; IN]}: number of seconds (or any other time
901    units as long as the same are used in all components and in the {\it
902      namcouple}) at the time of the call (by convention at the
903    beginning of the timestep)
904  \item {\tt fld [REAL, OUT]}: I/O or coupling field array; 
905    can be 1D, 2D, bundle 1D, or bundle 2D.
906  \item {\tt info [INTEGER; OUT]}: returned info code:
907    \begin{itemize}
908    \item OASIS\_Recvd(=3) if the field was received from another
909      component
910    \item OASIS\_FromRest (=10) if the field was read from a restart
911      file only
912    \item OASIS\_Input (=11) if the field was read from an input file
913      only
914    \item OASIS\_RecvOut (=12) if the field was both received from
915      another component and written to an output file
916    \item OASIS\_FromRestOut (=13) if the field was both read from a
917      restart file and written to an output file
918    \item OASIS\_Ok (=0) otherwise and no error occurred.
919    \end{itemize}
920  \end{itemize}
921\end{itemize}
922
923To ensure a proper use of the {\tt oasis\_get}, one has to take care of the following aspects:
924
925\begin{itemize}
926
927\item This routine may be called by the component at each timestep. The {\tt
928  date} argument is automatically analysed and the receiving action is
929actually performed only if {\tt date} corresponds to a time for which
930it should be activated, given the period indicated by the user in the
931{\it namcouple}. An exchange at the beginning of the run at time=0 is
932expected.
933
934\item Trying to receive with {\tt oasis\_get} a field declared with a {\tt
935  oasis\_def\_var} but not defined in the configuration file {\it
936  namcouple} will lead to an abort. In this case, the field ID
937returned by the {\tt oasis\_def\_var} is equal to -1 and the
938corresponding {\tt oasis\_get} should not be called.
939
940\item If a coupling field has a positive lag, the coupling field that
941  matches the {\tt oasis\_get} at time=0 is automatically read in from a coupling
942  restart file and sent to match that  {\tt oasis\_get} under the {\tt
943    oasis\_enddef} of the source model (see section \ref{subsec_lag}).
944
945\item Coupling multiple fields via a single communication is supported
946  through colon delimited field lists in the namcouple (see
947  \ref{subsubsec_secondEXPORTED}).
948All fields will use the namcouple settings for that entry. In the component
949model codes, these fields are still received (via an {\tt oasis\_get}) one at a time. Inside
950OASIS3-MCT, the fields are stored and a single mapping and receive instruction is executed
951for all fields. This is useful in cases where multiple fields have the same coupling transformations
952and to reduce communication costs by aggregating multiple fields into a single communication. If a
953LOCTRANS transformation is needed for these multiple fields, it is necessary to define a restart file
954for these multiple fields.
955\end{itemize}
956
957\subsection{Termination}
958\label{subsubsec_Termination}
959
960\begin{itemize}
961
962\item {\tt CALL oasis\_terminate (kinfo)}
963\item {\tt CALL prism\_terminate\_proto(kinfo)}
964  \begin{itemize}
965  \item {\tt kinfo [INTEGER; OUT]}: returned error code.
966  \end{itemize}
967All processes of components at least partly involved in the coupling (e.g. {\tt comp3} in figure
968    \ref{Fig_coupling_layouts_b}) have to terminate the coupling by
969  calling this routine\footnote{If the process called {\tt MPI\_Init}
970    (before calling {\tt oasis\_init\_comp}), it must also call {\tt
971      MPI\_Finalize} explicitly, but only after calling {\tt
972      oasis\_terminate\_proto}.}(normal termination). Different configurations of components and corresponding use of {\tt oasis\_terminate} are described in section \ref{sec_deploy} and on figures \ref{Fig_coupling_layouts_a} and \ref{Fig_coupling_layouts_b}.
973
974 
975
976\end{itemize}
977
978% {Termination}
979
980\subsection{Auxiliary routines}
981\label{subsubsec_auxroutines}
982
983The following auxiliary routines are currently available.
984
985\begin{itemize}
986\item {\tt CALL oasis\_abort (compid, routine\_name, abort\_message, file, line, rcode)}
987\item {\tt CALL prism\_abort\_proto(compid, routine\_name,
988    abort\_message, file, line, rcode)}
989  \begin{itemize}
990  \item {\tt compid [INTEGER; IN]}: component ID (from {\tt
991      oasis\_init\_comp})
992  \item {\tt routine\_name [CHARACTER*; IN]}: name of calling routine
993  \item {\tt abort\_message [CHARACTER*; IN]}: message to be written out
994  \item {\tt file [CHARACTER*; OPTIONAL; IN]}: file from which oasis\_abort is called from
995  \item {\tt line [INTEGER, OPTIONAL; IN]}: line in file from which oasis\_abort is called from
996  \item {\tt rcode [INTEGER, OPTIONAL; IN]}: Optional argument. When OASIS3-MCT
997    aborts, it returns rcode if it is present, else it returns 1
998  \end{itemize}
999
1000  If a process needs to abort voluntarily, it should do so by calling
1001  {\tt oasis\_abort}. This will ensure a proper termination of all
1002  processes in the coupled model communicator. This routine writes the
1003  name of the calling component, the name of the calling routine, and the
1004  message to the process debug file (see {\tt \$NLOGPRT} in section
1005  \ref{subsec_namcouplefirst}).  This routine cannot be called before
1006  {\tt oasis\_init\_comp}.
1007
1008  \vspace{0.2cm}
1009\item {\tt CALL oasis\_get\_debug(debug, kinfo)}
1010\item {\tt CALL prism\_get\_debug(debug, kinfo)}
1011  \begin{itemize}
1012  \item {\tt debug [INTEGER; OUT]}: output debug value
1013  \item {\tt kinfo [INTEGER; OUT; OPTIONAL]}: returned error code
1014  \end{itemize}
1015
1016  This routine may be called at any time to retrieve the current
1017  OASIS3-MCT internal debug level (see {\tt \$NLOGPRT} in section
1018  \ref{subsec_namcouplefirst}).  This is useful if the user wants to
1019  return the original debug value after changing it.
1020
1021  \vspace{0.2cm}
1022\item {\tt CALL oasis\_set\_debug(debug, kinfo)}
1023\item {\tt CALL prism\_set\_debug(debug, kinfo)}
1024  \begin{itemize}
1025  \item {\tt debug [INTEGER; IN]}: input debug value
1026  \item {\tt kinfo [INTEGER; OUT; OPTIONAL]}: returned error code
1027  \end{itemize}
1028
1029  This routine may be called at any time to change the debug level in
1030  OASIS3-MCT.  This method allows users to vary the debug level at
1031  different points in the component integration.
1032
1033  \vspace{0.2cm}
1034\item {\tt CALL oasis\_get\_intercomm(new\_comm, cdnam, kinfo)}
1035\item {\tt CALL prism\_get\_intercomm(new\_comm, cdnam, kinfo)}
1036  \begin{itemize}
1037  \item {\tt new\_comm [INTEGER; OUT]}: MPI inter-communicator
1038  \item {\tt cdnam [CHARACTER*; IN]}: other component name (i.e. 2nd argument of  the call to {\tt oasis\_init\_comp} in that component)
1039  \item {\tt kinfo [INTEGER; OUT; OPTIONAL]}: returned error code
1040  \end{itemize}
1041
1042  This routine sets up an MPI inter-communicator between two components,
1043  the local component and the component
1044  associated with {\tt cdnam}.  This call is collective across the
1045  tasks of the two components only.
1046  An MPI inter-communicator preserves the rank of the original communicators
1047  and does not allow collective communication within the communicator.  It
1048  provides point to point communication between two non-overlapping MPI groups.
1049  This method must be called synchronously across all components involved to
1050  minimize the chance of a deadlock, and it should be called only after
1051  {\tt oasis\_enddef} is called.
1052  See {\tt oasis\_get\_intracomm} below to create an intra-communicator.
1053
1054  \vspace{0.2cm}
1055\item {\tt CALL oasis\_get\_intracomm(new\_comm, cdnam, kinfo)}
1056\item {\tt CALL prism\_get\_intracomm(new\_comm, cdnam, kinfo)}
1057  \begin{itemize}
1058  \item {\tt new\_comm [INTEGER; OUT]}: MPI intra-communicator
1059  \item {\tt cdnam [CHARACTER*; IN]}: other component name (i.e. 2nd argument of the call to {\tt oasis\_init\_comp} in that component).  This argument is a single string.
1060  \item {\tt kinfo [INTEGER; OUT; OPTIONAL]}: returned error code
1061  \end{itemize}
1062
1063  This routine sets up an MPI intra-communicator between two
1064  components, the local component and the component defined by
1065  the string {\tt cdnam}.  This call is collective
1066  across the tasks of the two components creating the intra-communicator only,
1067  and it must be called synchronously across all tasks of the two components
1068  to minimize the chance of a deadlock.  It should be called only after
1069  {\tt oasis\_enddef} is called.
1070  This method creates a new communicator consisting of a new collective group
1071  of tasks with new ranks.
1072  This communicator supports collective communications and is
1073  more typically used in MPI applications than inter-communicators
1074  (see {\tt oasis\_get\_intercomm} above).
1075  See also {\tt oasis\_get\_multi\_intracomm} for another method that
1076  supports creating an MPI intra-communicator between two or more components.
1077
1078  \vspace{0.2cm}
1079\item {\tt CALL oasis\_get\_multi\_intracomm(new\_comm, cdnam, root\_ranks, kinfo)}
1080\item {\tt CALL prism\_get\_multi\_intracomm(new\_comm, cdnam, root\_ranks, kinfo)}
1081  \begin{itemize}
1082  \item {\tt new\_comm [INTEGER; OUT]}: MPI intra-communicator
1083  \item {\tt cdnam [CHARACTER*; IN]}: array of component names (i.e. 2nd argument of the call to {\tt oasis\_init\_comp} in that component).  This argument is a 1d array of character strings (i.e. cdnam(:)).
1084  \item {\tt root\_ranks [INTEGER; OUT]}: array of root ranks.  This argument is a 1d integer array (i.e. root\_ranks(:)) of the same size as {\tt cdnam}.
1085  \item {\tt kinfo [INTEGER; OUT]}: returned error code
1086  \end{itemize}
1087
1088  This routine sets up an MPI intra-communicator between two or more
1089  components defined by the component names passed in the {\tt cdnam} array
1090  argument.  The local model name MUST BE one of the
1091  models defined in the {\tt cdnam} array.  The component names must
1092  be valid names, but empty strings are allowed and ignored.
1093  This call is collective
1094  across all the tasks of the components defined in {\tt cdnam},
1095  and it must be called synchronously and consistently across all tasks of
1096  those components to minimize the chance of a deadlock.  It should be called only after
1097  {\tt oasis\_enddef} is called.
1098  This method creates a new communicator consisting of a new collective group
1099  of tasks with new ranks.  The root ranks of the individual components relative
1100  to the new communicator is output in the {\tt root\_ranks} argument.
1101  The size of {\tt cdnam} and {\tt root\_ranks} should be identical,
1102  and the values of {\tt root\_ranks} are consistent with the order of {\tt cdnam}..
1103  This communicator supports collective communications and is
1104  more typically used in MPI applications than inter-communicators
1105  (see {\tt oasis\_get\_intercomm} above). 
1106  See also {\tt oasis\_get\_intracomm} for another method that
1107  supports creating an MPI intra-communicator between two components.
1108
1109\item {\tt CALL oasis\_put\_inquire(var\_id, date, kinfo)}
1110\item {\tt CALL prism\_put\_inquire\_proto(var\_id, date, kinfo)}
1111  \begin{itemize}
1112  \item {\tt var\_id [INTEGER; IN]}: field ID (from
1113  corresponding oasis\_def\_var)
1114  \item {\tt date [INTEGER; IN]}: as in {\tt oasis\_put}, number of seconds (or any other time
1115    units as long as the same are used in all components and in the {\it
1116      namcouple}) in the run at the time of the call
1117  \item {\tt kinfo [INTEGER; OUT]}: returned info code
1118    \begin{itemize}
1119    \item OASIS\_Sent(=4) if the field would be sent to another component
1120    \item OASIS\_LocTrans (=5) if the field would be only used in a time
1121      transformation (not sent, not output)
1122    \item OASIS\_ToRest (=6) if the field would be written to a restart
1123      file only
1124    \item OASIS\_Output (=7) if the field would be written to an output
1125      file only
1126    \item OASIS\_SentOut (=8) if the field would be both written to an
1127      output file and sent to another component (directly or via OASIS3
1128      main process)
1129    \item OASIS\_ToRestOut (=9) if the field would be written both to a
1130      restart file and to an output file.
1131    \item OASIS\_Ok (=0) otherwise and no error occurred.
1132    \end{itemize}
1133  \end{itemize}
1134
1135This routine may be called at any time to
1136inquire what would happen to the field corresponding to that
1137{\tt var\_id} if it was sent with an {\tt oasis\_put} at that same {\tt date}). This maybe useful if, for example, the calculation of
1138a coupling field is costly and if one wants to compute it only when it is
1139really sent out.
1140
1141\item {\tt CALL oasis\_get\_ncpl(var\_id, ncpl, kinfo)}
1142\item {\tt CALL prism\_get\_ncpl\_proto(var\_id, ncpl, kinfo)}
1143  \begin{itemize}
1144  \item {\tt var\_id [INTEGER; IN]}: field ID (from
1145  corresponding oasis\_def\_var)
1146  \item {\tt ncpl [INTEGER; OUT]}: number of coupling exchanges in which the field
1147  is involved (i.e. when a field is sent to multiple targets)
1148  \item {\tt kinfo [INTEGER; OUT]}: returned info code
1149  \end{itemize}
1150
1151This routine returns the number of coupling exchanges in which the
1152field with that {\tt var\_id} is
1153involved. This number is needed to get the coupling frequencies with the
1154routine oasis\_get\_freqs, see below.
1155
1156\item {\tt CALL oasis\_get\_freqs(var\_id, mop, ncpl, cpl\_freqs, kinfo)}
1157\item {\tt CALL prism\_get\_freqs\_proto(var\_id, mop, ncpl, cpl\_freqs, kinfo)}
1158  \begin{itemize}
1159  \item {\tt var\_id [INTEGER; IN]}: field ID (from
1160  corresponding oasis\_def\_var)
1161  \item {\tt mop [INTEGER; IN]}: OASIS\_Out or OASIS\_In
1162  \item {\tt ncpl [INTEGER; IN]}: number of couplings in which the field
1163  is involved (i.e. when a field is sent to multiple targets)
1164   \item {\tt cpl\_freqs [INTEGER; DIMENSION(ncpl); OUT]}: coupling period(s)
1165  (in number of seconds) of field var\_id. There is one coupling period for
1166  each coupling exchange in which the field is involved
1167  \item {\tt kinfo [INTEGER; OUT]}: returned info code
1168  \end{itemize}
1169
1170This routine can be used to retrieve the coupling period(s) of field with
1171corresponding {\tt var\_id}, as defined in the {\it namcouple}
1172
1173\end{itemize}
1174
1175\section{OASIS3-MCT C API}
1176\label{APIC} 
1177
1178OASIS3-MCT is distributed with C bindings and can be called from models written
1179in C and in C++.  These bindings leverage the Fortran ISO\_C\_BINDING standard. 
1180The C bindings can be compiled into static or shared libraries by the
1181OASIS3-MCT TopMakefileOasis3 as documented in \ref{subsec_compile}.
1182
1183The C interfaces largely match
1184up with equivalent interfaces in Fortran.  An interface named
1185{\tt oasis\_interface} in Fortran can be expected to be named
1186{\tt oasis\_c\_interface} in C.
1187
1188All of the C interfaces return an integer error code which can be
1189tested against the {\tt OASIS\_Ok} (or the equivalent {\tt
1190  OASIS\_Success}) constant.
1191The {\tt OASIS\_CHECK\_ERR} macro aborts
1192OASIS3-MCT with a meaningful message in the debug files in case of failure.
1193For most of the functions,
1194the return code is consistent with the {\tt
1195  kinfo} argument in the Fortran interfaces (see section \ref{API}). However, for the put and get communication functions, the return error code only indicates
1196success or failure, while the detailed status is returned by the {\tt
1197  kinfo} argument (equivalent to the returned value of {\tt
1198  info} argument for the Fortran {\tt oasis\_put} and {\tt oasis\_get} routines, see section \ref{prismput}).
1199 
1200For example, the following constructs are equivalent in the two languages:
1201
1202\begin{verbatim}
1203call oasis_get_localcomm(localcomm, kinfo)
1204if (kinfo .ne. OASIS_Ok)  &
1205   &  call OASIS_Abort(comp_id, "oasis_get_localcomm", &
1206   &  "Runtime error", __FILE__, __LINE__)
1207\end{verbatim}
1208
1209and
1210
1211\begin{verbatim}
1212OASIS_CHECK_ERR(oasis_get_localcomm(localcomm))
1213\end{verbatim}
1214
1215For convenience a similar macro {\tt OASIS\_CHECK\_MPI\_ERR} has been
1216defined for testing the return code of any MPI function against {\tt
1217  MPI\_SUCCESS} and cleanly aborting OASIS3-MCT in case of failure.
1218
1219\vspace{0.2cm}
1220Use of these C bindings are illustrated in practical examples in
1221directories {\tt /C} in the different subdirectories in {\tt pyoasis/examples/}. Notes and deviations from
1222the Fortran standard are noted below.
1223
1224\vspace{0.2cm}
1225\begin{itemize}
1226
1227\item To use the OASIS3-MCT C bindings, the statement
1228\begin{verbatim}
1229#include "oasis_c.h"
1230\end{verbatim}
1231needs to be added to the C model source code.  All available
1232parameters macros and
1233interfaces are defined there.
1234
1235\item {\tt int oasis\_c\_init\_comp(int* compid, const char* comp\_name, const bool coupled)}
1236  \begin{itemize}
1237  \item This {\tt oasis\_c\_init\_comp} interface includes the {\tt coupled} flag but
1238    NO communicator argument; use  {\tt
1239      oasis\_c\_init\_comp\_with\_comm} below to indicate a
1240    communicator argument.
1241    \item The {\tt coupled} argument can receive the predefined mnemonic boolean constants {\tt
1242      OASIS\_Coupled} and {\tt OASIS\_Not\_Coupled}
1243  \end{itemize}
1244
1245\vspace{0.2cm}
1246\item {\tt int oasis\_c\_init\_comp\_with\_comm(int* compid, const char* comp\_name, const bool coupled, const MPI\_Comm commworld)}
1247  \begin{itemize}
1248  \item This alternative {\tt init\_comp} interface has the {\tt coupled} flag and a C {\tt MPI\_Comm} communicator
1249  \end{itemize}
1250
1251\vspace{0.2cm}
1252\item {\tt int oasis\_c\_get\_localcomm(MPI\_Comm* local\_comm)}
1253  \begin{itemize}
1254  \item Returns the local communicator by reference in a C {\tt MPI\_Comm*} type argument
1255  \end{itemize}
1256
1257\vspace{0.2cm}
1258\item {\tt int oasis\_c\_create\_couplcomm(const int icpl, const MPI\_Comm local\_comm, MPI\_Comm* coupl\_comm)}
1259  \begin{itemize}
1260  \item {\tt icpl} is a C {\tt int} type input argument (see {\tt
1261      oasis\_create\_couplcomm} in section \ref{subsubsec_Initialisation})
1262  \item {\tt loca\_comm} is a C {\tt MPI\_Comm} type input argument (by value)
1263
1264  \item {\tt coupl\_comm} is a C {\tt MPI\_Comm*} type output argument (by reference)
1265  \end{itemize}
1266
1267\vspace{0.2cm}
1268\item {\tt int oasis\_c\_set\_couplcomm(const MPI\_Comm coupl\_comm)}
1269  \begin{itemize}
1270  \item Takes a C {\tt MPI\_Comm} type as an input argument (by value)
1271  \end{itemize}
1272
1273\vspace{0.2cm}
1274\item {\tt int oasis\_c\_def\_partition(int* il\_part\_id, const int ig\_paral\_size, const int* ig\_paral, const int ig\_size, const char* name)}
1275  \begin{itemize}
1276  \item {\tt ig\_paral\_size} is the size of the array {\tt ig\_paral}: the
1277    {\tt oasis\_c.h} header provides a set of constants and macros for
1278    the size of every partition strategy, namely {\tt
1279      OASIS\_Serial\_Params}, {\tt OASIS\_Apple\_Params}, {\tt
1280      OASIS\_Box\_Params}, {\tt
1281      OASIS\_Orange\_Params(n\_segments)}, {\tt
1282      OASIS\_Points\_Params(n\_points)}
1283  \item the {\tt oasis\_c.h} header also provides a set of predefined
1284    constants for the storages positions in {\tt
1285    ig\_paral}, namely {\tt OASIS\_Strategy}, {\tt OASIS\_Segments}, {\tt
1286    OASIS\_Npoints}, {\tt OASIS\_Offset}, {\tt OASIS\_Length}, {\tt
1287    OASIS\_SizeX}, {\tt OASIS\_SizeY}, {\tt OASIS\_LdX}
1288  \item the partition strategy, to be stored in the {\tt
1289    OASIS\_Strategy} position of {\tt ig\_paral}, can take one of the
1290    following predefined constants values: {\tt OASIS\_Serial}, {\tt
1291      OASIS\_Apple}, {\tt OASIS\_Box}, {\tt OASIS\_Orange}, {\tt
1292      OASIS\_Points}
1293  \item for the cases in which the {\tt ig\_size} and {\tt name}
1294    arguments are
1295    not relevant for the partition definition, the placeholders {\tt
1296      OASIS\_No\_Gsize} and {\tt OASIS\_No\_Name} can be used instead
1297  \end{itemize}
1298  Here an example for the {\tt OASIS\_Part\_Apple} strategy
1299\begin{verbatim}
1300  int part_params[OASIS_Apple_Params];
1301  part_params[OASIS_Strategy] = OASIS_Apple;
1302  part_params[OASIS_Offset] = offset;
1303  part_params[OASIS_Length] = local_size;
1304  int part_id;
1305  OASIS_CHECK_ERR(oasis_c_def_partition(&part_id, OASIS_Apple_Params,
1306                                        part_params, OASIS_No_Gsize,
1307                                        OASIS_No_Name));
1308\end{verbatim}
1309
1310\vspace{0.2cm}
1311\item {\tt int oasis\_c\_start\_grids\_writing()}
1312
1313\vspace{0.2cm}
1314\item {\tt int oasis\_c\_write\_grid(const char* cgrid, const int nx\_global, const int ny\_global, const int nx\_loc, const int ny\_loc, const double* lon, const double* lat, const int il\_partid)}
1315  \begin{itemize}/local
1316    \item {\tt nx\_global} and {\tt ny\_global} are the first and second dimensions
1317      of the global grid
1318    \item {\tt nx\_loc} and {\tt ny\_loc} are the two dimensions
1319      of the local arrays {\tt lon} and {\tt lat}
1320    \item {\tt lon} and {\tt lat} are stored with a Fortran compatible
1321      (column major) ordering. For example, if they are declared as double
1322      pointers, they have to be {\tt lon[ny\_loc][nx\_loc]}
1323    \item {\tt il\_partid} is relevant only for parallel writing; in case
1324      of single proc invocations, the constant {\tt OASIS\_No\_Part}
1325      can be passed as a placeholder
1326  \end{itemize}
1327
1328\vspace{0.2cm}
1329\item {\tt int oasis\_c\_write\_corner(const char* cgrid, const int nx\_global, const int ny\_global, const int nc, const int nx\_loc, const int ny\_loc, const double* clon, const double* clat, const int il\_partid)}
1330  \begin{itemize}
1331    \item {\tt nx\_global} and {\tt ny\_global} are the first and second dimensions
1332      of the global grid
1333    \item {\tt nc} is the maximum number of corners per cell
1334    \item {\tt nx\_loc} and {\tt ny\_loc} are the two dimensions
1335      of the local arrays {\tt clon} and {\tt clat}
1336    \item {\tt clon} and {\tt clat} are stored with a Fortran compatible
1337      (column major) ordering. If they are declared as triple
1338      pointers, they have to be {\tt clon[nc][ny\_loc][nx\_loc]}
1339    \item {\tt il\_partid} is relevant only for parallel writing. In case
1340      of single proc invocations, the constant {\tt OASIS\_No\_Part}
1341      can be passed as a placeholder
1342  \end{itemize}
1343
1344\vspace{0.2cm}
1345\item {\tt int oasis\_c\_write\_mask(const char* cgrid, const int nx\_global, const int ny\_global, const int nx\_loc, const int ny\_loc, const int* mask, const int il\_partid, const char* companion)}
1346  \begin{itemize}
1347    \item {\tt nx\_global} and {\tt ny\_global} are the first and second dimensions
1348      of the global grid
1349    \item {\tt nx\_loc} and {\tt ny\_loc} are the two dimensions
1350      of the local array {\tt mask}
1351    \item {\tt mask} is stored with a Fortran compatible
1352      (column major) ordering. If it is  declared as a double
1353      pointer, it has to be {\tt mask[ny\_loc][nx\_loc]}
1354    \item {\tt il\_partid} is relevant only for parallel writing. In case
1355      of single proc invocations, the constant {\tt OASIS\_No\_Part}
1356      can be passed as a placeholder
1357    \item if no {\tt companion} grid attribute is needed, the constant
1358      {\tt OASIS\_No\_Companion} can be passed as a placeholder
1359  \end{itemize}
1360
1361\vspace{0.2cm}
1362\item {\tt int oasis\_c\_write\_frac(const char* cgrid, const int nx\_global, const int ny\_global, const int nx\_loc, const int ny\_loc, const double* frac, const int il\_partid, const char* companion)}
1363  \begin{itemize}
1364    \item {\tt nx\_global} and {\tt ny\_global} are the first and second dimensions
1365      of the global grid
1366    \item {\tt nx\_loc} and {\tt ny\_loc} are the two dimensions
1367      of the local array {\tt frac}
1368    \item {\tt frac} is stored with a Fortran compatible
1369      (column major) ordering. If it is  declared as a double
1370      pointer, it has to be {\tt frac[ny\_loc][nx\_loc]}
1371    \item {\tt il\_partid} is relevant only for parallel writing. In case
1372      of single proc invocations, the constant {\tt OASIS\_No\_Part}
1373      can be passed as a placeholder
1374    \item if no {\tt companion} grid attribute is needed, the constant
1375      {\tt OASIS\_No\_Companion} can be passed as a placeholder
1376  \end{itemize}
1377
1378\vspace{0.2cm}
1379\item {\tt int oasis\_c\_write\_area(const char* cgrid, const int nx\_global, const int ny\_global, const int nx\_loc, const int ny\_loc, const double* area, const int il\_partid)}
1380  \begin{itemize}
1381    \item {\tt nx\_global} and {\tt ny\_global} are the first and second dimensions
1382      of the global grid
1383    \item {\tt nx\_loc} and {\tt ny\_loc} are the two dimensions
1384      of the local array {\tt area}
1385    \item {\tt area} is stored with a Fortran compatible
1386      (column major) ordering. If it is  declared as a double
1387      pointer, it has to be {\tt area[ny\_loc][nx\_loc]}
1388    \item {\tt il\_partid} is relevant only for parallel writing. In case
1389      of single proc invocations, the constant {\tt OASIS\_No\_Part}
1390      can be passed as a placeholder
1391  \end{itemize}
1392
1393\vspace{0.2cm}
1394\item {\tt int oasis\_c\_write\_angle(const char* cgrid, const int nx\_global, const int ny\_global, const int nx\_loc, const int ny\_loc, const double* angle, const int il\_partid)}
1395  \begin{itemize}
1396    \item {\tt nx\_global} and {\tt ny\_global} are the first and second dimensions
1397      of the global grid
1398    \item {\tt nx\_loc} and {\tt ny\_loc} are the two dimensions
1399      of the local array {\tt angle}
1400    \item {\tt angle} is stored with a Fortran compatible
1401      (column major) ordering. If it is  declared as a double
1402      pointer, it has to be {\tt angle[ny\_loc][nx\_loc]}
1403    \item {\tt il\_partid} is relevant only for parallel writing. In case
1404      of single proc invocations, the constant {\tt OASIS\_No\_Part}
1405      can be passed as a placeholder
1406  \end{itemize}
1407
1408\vspace{0.2cm}
1409\item {\tt int oasis\_c\_terminate\_grids\_writing()}
1410
1411\vspace{0.2cm}
1412\item {\tt int oasis\_c\_def\_var(int* var\_id, const char* name, const int il\_part\_id, const bundle\_size, const int kinout, const int var\_type)}
1413  \begin{itemize}
1414  \item {\tt bundle\_size} is a scalar integer, corresponding to the
1415    second entry of the Fortran {\tt var\_nodims} array
1416  \item {\tt kinout} can receive one of the two predefined constants
1417    {\tt OASIS\_In} or {\tt OASIS\_Out} (also in the form
1418    {\tt OASIS\_IN} or {\tt OASIS\_OUT})
1419  \item {\tt var\_type} can receive one of the two predefined constants
1420    {\tt OASIS\_Real} or {\tt OASIS\_Double} (also in the form
1421    {\tt OASIS\_REAL} or {\tt OASIS\_DOUBLE})
1422  \end{itemize}
1423
1424\vspace{0.2cm}
1425\item {\tt int oasis\_c\_enddef()}
1426
1427\vspace{0.2cm}
1428\item {\tt int oasis\_c\_put(const int var\_id, const int date, const
1429  int x\_size, const int y\_size, const int bundle\_size, const int
1430  fkind, const int storage, const void* fld1, const bool
1431  write\_restart, int* kinfo)}
1432  \begin{itemize}
1433  \item This interface does not support higher order mapping through optional fields at this time.
1434  \item {\tt x\_size} and {\tt y\_size} are the dimensions of the local
1435    portion of the domain (i.e. fld1). The order of these dimensions must be the same than the order of the dimensions of the arrays in the {\tt
1436      grids.nc} file, which corresponds to the storage order in the
1437    corresponding internal OASIS3-MCT Fortran work arrays. \\
1438    Notice that for
1439    unstructured grids {\tt y\_size=1}
1440  \item {\tt bundle\_size} should be coherent with the argument of
1441    same name in the corresponding {\tt oasis\_c\_def\_var}
1442  \item {\tt fkind} can take one of the two predefined constants
1443    {\tt OASIS\_Real} or {\tt OASIS\_Double} (also in the form
1444    {\tt OASIS\_REAL} or {\tt OASIS\_DOUBLE}) should be coherent with
1445    the argument {\tt ktype} in {\tt oasis\_c\_def\_var}
1446  \item {\tt storage} can take one of the two predefined constants
1447    {\tt OASIS\_COL\_MAJOR} or \\
1448    {\tt OASIS\_ROW\_MAJOR}. \\
1449    In the first
1450    case, the array containing the field has to be declared as \\ {\tt
1451      field[bundle\_size][y\_size][x\_size]} (or any equivalent
1452    storage of total size \\
1453    {\tt bundle\_size*y\_size*x\_size} stored in
1454    column major order. \\
1455    In the second case, the field has to be
1456    declared as \\
1457    {\tt field[x\_size][y\_size][bundle\_size]} (or any
1458    equivalent row major storage). \\
1459    Notice that this choice implies an
1460    extra memory copy performed internally by OASIS3-MCT before acting on
1461    the field and is therefore to be avoided whenever possible.
1462  \item {\tt write\_restart} can take one of the two predefined constants
1463    {\tt OASIS\_Write\_Restart} or {\tt OASIS\_No\_Restart}
1464  \item {\tt kinfo} contains a return status to be compared against
1465    the values of the {\tt return\_codes} enumeration in {\tt oasis\_c.h} (equivalent to the returned value of {\tt
1466  info} argument for the Fortran {\tt oasis\_put} routine, see section \ref{prismput})
1467  \end{itemize}
1468
1469\vspace{0.2cm}
1470\item {\tt int oasis\_c\_get(const int var\_id, const int date, const
1471  int x\_size, const int y\_size, const int bundle\_size, const int
1472  fkind, const int storage, void* fld1, int* kinfo)}
1473  \begin{itemize}
1474  \item arguments are the same than for {\tt oasis\_c\_put}, except that {\tt kinfo} return
1475  status is equivalent to the returned value of {\tt
1476  info} argument for the Fortran {\tt oasis\_get} routine, see section \ref{prismput}.
1477  \end{itemize}
1478
1479\vspace{0.2cm}
1480\item {\tt int oasis\_c\_terminate()}
1481
1482\vspace{0.2cm}
1483\item {\tt int oasis\_c\_abort(const int compid, const char* routine\_name, const char* abort\_message, const char* file, const int line)}
1484
1485\vspace{0.2cm}
1486\item {\tt int oasis\_c\_get\_debug(int* debug)}
1487
1488\vspace{0.2cm}
1489\item {\tt int oasis\_c\_set\_debug(const int debug)}
1490
1491\vspace{0.2cm}
1492\item {\tt int oasis\_c\_get\_intercomm(MPI\_Comm* new\_comm, char* cdnam)}
1493  \begin{itemize}
1494  \item Returns a C MPI\_Comm type by reference
1495  \end{itemize}
1496
1497\vspace{0.2cm}
1498\item {\tt int oasis\_c\_get\_intracomm(MPI\_Comm* new\_comm, char* cdnam)}
1499  \begin{itemize}
1500  \item Returns a C MPI\_Comm type as by reference
1501  \end{itemize}
1502
1503\vspace{0.2cm}
1504\item {\tt int oasis\_c\_get\_multi\_intracomm(MPI\_Comm* new\_comm, const int cdnam\_size, char** cdnam, int* root\_ranks)}
1505  \begin{itemize}
1506  \item Returns a C MPI\_Comm type as by reference
1507  \item cdnam\_size is the size of the array cdnam
1508  \end{itemize}
1509
1510\vspace{0.2cm}
1511\item {\tt int oasis\_c\_put\_inquire(int var\_id, int date, int* kinfo)}
1512  \begin{itemize}
1513  \item {\tt kinfo} contains a return status to be compared against
1514    the values of the {\tt return\_codes} enumeration in {\tt oasis\_c.h}
1515  \end{itemize}
1516
1517\vspace{0.2cm}
1518\item {\tt int oasis\_c\_get\_ncpl(const int var\_id, int* ncpl)}
1519
1520\vspace{0.2cm}
1521\item {\tt int oasis\_c\_get\_freqs(const int var\_id, const int mop, const int ncpl, int*  cpl\_freqs)}
1522
1523\end{itemize}
1524
1525\section{OASIS3-MCT python API}
1526\label{APIpython}
1527
1528The source code of pyOASIS is in the directory  {\tt pyoasis/src}. Complete
1529documentation is available in  {\tt pyoasis/pyoasis.pdf}. The pyoasis interface
1530ultimately call the Fortran version.  This is done by wrapping the
1531Fortran in ISO-C bindings (see  {\tt lib/cbindings/fortran\_isoc}), then
1532wrapping the Fortran ISO-C bindings in C (see  {\tt lib/cbindings/c}), then
1533wrapping the C bindings in python (see  {\tt pyoasis/src}).  This method
1534provides both the C and python bindings.  Examples on how
1535to use pyOASIS are provided in  {\tt pyoasis/examples}. To run all tests including python, C, and
1536Fortran examples, use {\tt make test}. The python wrapper functions
1537are briefly described next.
1538
1539\begin{itemize}
1540\item{Creating a component using MPI}
1541\label{index:creating-a-component-using-mpi}
1542
1543In pyOASIS, components are instances of the {\tt Component} class. To
1544initialise a component, its name has to be supplied.
1545It is also possible to provide an optional  {\tt coupling\_flag} argument which
1546defaults to “True”, which means the component is coupled through OASIS3\_MCT.
1547
1548OASIS3\_MCT couples models which communicate using MPI.
1549If the global communicator at the start of the run is different from
1550the default  {\tt MPI\_COMM\_WORLD} communicator, the global
1551communicator has to be passed to the {\tt Component} class through the
1552third optional argument. By default, the
1553{\tt Component} class will set up MPI internally and provides methods
1554to get access to information such as rank and number of processes in
1555the local communicator gathering only the component processes.
1556
1557\begin{verbatim}
1558   ____________________________________________________________
1559   import pyoasis
1560   [...]
1561   comm = my_global_comm
1562   component_name = "component"
1563   coupling_flag = True
1564   comp = pyoasis.Component(component_name, coupling_flag, comm)
1565   print("Hello world from process " + str(comp.localcomm.rank)
1566            + " of " + str(comp.localcomm.size))
1567   ____________________________________________________________
1568\end{verbatim}
1569
1570To create a coupling communicator for a subset of processes, one can
1571use the method {\tt create\_couplcomm}, with a flag being {\tt True} for all these
1572processes; see {\tt pyoasis/examples/4-orange/python} for a practical example.
1573
1574If such a communicator already exists in the code, it should simply be
1575provided to OASIS3\_MCT with the method {\tt set\_couplcomm};
1576as in {\tt pyoasis/examples/6-apple\_and\_orange/python}.
1577
1578To set up an MPI intra communicator or inter communicator between the local component and
1579another component, one can use the methods {\tt get\_intracomm} or {\tt get\_intercomm};
1580\newline
1581as in {\tt pyoasis/examples/3-box/python} .
1582
1583To set up an MPI intra-communicator among some of the coupled components, listed in the {\tt comp\_list}  list,
1584one can use the method {\tt get\_multi\_intracomm}, as in {\tt 9-python\_fortran\_C-multi\_intracomm}.
1585
1586Also, the current OASIS3-MCT internal debug level ({\tt \$NLOGPRT}
1587value in the {\it namcouple}), can be retrieved as a property of a component,
1588namely {\tt debug\_level}, and can be changed by directly modifying this property,
1589as in {\tt pyoasis/examples/7-multiple-puts/python}.
1590
1591\item{Creating a partition}
1592\label{{index:creating-a-partition}}
1593
1594The data can be partitioned in various ways.
1595These correspond to the  {\tt SerialPartition}, {\tt ApplePartition},
1596{\tt BoxPartition}, {\tt OrangePartition} and {\tt PointsPartition}
1597classes which are inherited from the {\tt Partition} abstract class.
1598For details on the different ways to describe the partitions,
1599see OASIS3\_MCT User Guide, section 2.2.3 and examples {\tt 1\_serial},
1600{\tt 2\_apple}, {\tt 3\_box}, {\tt 4\_orange}, {\tt 5\_points} in {\tt pyoasis/examples}.
1601
1602The simplest situation is the serial partitioning where all the data is
1603held by a single process and only the number of points has to be
1604specified (see example {\tt 1\_serial})
1605
1606In the case of the Apple partitioning, each process contains a segment
1607of a linear domain. To initialise such a partitioning, an offset has to
1608be supplied for each rank as well as the number of data points that
1609will be stored locally (see example  {\tt 2\_apple}).
1610
1611When we use the Box partitioning, a two-dimensional domain is split
1612into several rectangles. The global offset, local extents in the x and
1613y directions and the global extent in the x direction have to be supplied
1614to the constructor. The global offset is the index of the corner of the local
1615rectangle (see example in {\tt 3\_box}) .
1616
1617The Orange partitioning consists of several segments of a linear domain (see an example with only one segment per process in  {\tt 4\_orange}.)
1618
1619The last type of partitioning is Points, where we have to
1620specify, in a list, the global indices of the points stored by the
1621process (see example in {\tt 5\_points}).
1622
1623\item{Defining the coupling grids}
1624\label{index:defining-the-coupling-grids}
1625
1626The grid data files, containing the definition of the grids onto which
1627the coupling data is defined, can be created by the user before the
1628run or can be written directly at run time by the components, either
1629by one component process to write the whole grid or by each process
1630holding a part of a grid. Details
1631about the grid definition can be found in section 2.2.4 of OASIS3\_MCT
1632User Guide. A full example of writing a grid in sequential and
1633parallel models can be found in {\tt examples/10\_grid} .
1634
1635To initialise  a grid and write the grid longitudes and latitudes, one
1636has to create an instance of the {\tt Grid} class. Then to write the grid cell corner longitudes and latitudes, areas, mask, cell valid fraction, angle, the
1637{\tt set\_corners}, {\tt set\_area}{\tt set\_mask}{\tt set\_frac}, and {\tt set\_angle} methods can be used respectively.
1638
1639\item{Declaring the coupling data}
1640\label{{index:declaring-the-coupling-data}}
1641
1642The coupling data is handled by the class {\tt Var}. Its constructor requires
1643its symbolic name, as it appears in the {\it namcouple} file, the partition and a flag indicating whether the
1644data is incoming or outgoing. The latter is an enumerated type and can
1645have the values {\tt pyoasis.OasisParameters.OASIS\_OUT} or
1646{\tt pyoasis.OasisParameters.OASIS\_IN}.
1647
1648The property {\tt is\_active} can be tested to check if the variable is
1649activated in the {\it namcouple} configuring file (see {\tt example 3-box/python}).
1650
1651The coupling period(s) of the data, as defined in the {\it namcouple}, can be
1652accessed with the property {\tt cpl\_freqs} and the number of coupling exchanges in
1653which the data is involved by {\tt len(cpl\_freqs)} (see example {\tt 7-multiple-puts/python}).
1654
1655The property {\tt put\_inquire} of the variable tells what would happen to the corresponding data at that date
1656below the corresponding send action. This maybe useful if, for
1657example, the calculation of a coupling field is costly and if one
1658wants to compute it only when it is really sent out. The
1659different possible return codes are listed in section 2.2.9 of
1660OASIS3\_MCT User Guide.
1661
1662\item{Ending the definition phase}
1663\label{{index:ending-the-definition-phase}}
1664
1665Then the definition of the component must be ended by calling the {\tt enddef()}
1666method. This must be done only once the partitioning and the variable data have been initalised.
1667
1668\item{Sending and receiving data}
1669\label{{index:sending-and-receiving-data}}
1670
1671pyOASIS expects data to be provided as a {\tt pyoasis.asarray} object:
1672
1673\begin{verbatim}
1674   ____________________________________________________________
1675   field = pyoasis.asarray(range(n_points))
1676   ____________________________________________________________
1677\end{verbatim}
1678
1679This is a numpy array but ordered in the Fortran way.
1680In C, multidimensional arrays store data in row\_major order where
1681contiguous elements are accessed by incrementing the rightmost index
1682while varying the other indices will correspond to increasing strides in
1683memory as we use indices further towards the left. By default, numpy arrays
1684use that ordering as well. Fortran, on the other hand, uses column\_major
1685order. In that case, contiguous elements are accessed by incrementing
1686the leftmost index. {\tt pyoasis.asarray} objects use the same ordering as
1687Fortran. As a consequence, it is not necessary to transform data in order to
1688use it in the OASIS3\_MCT Fortran library.
1689
1690The sending and receiving actions may be called by the component at
1691each timestep. The date argument is automatically analysed and actions
1692are actually performed only if date corresponds to a time for which it
1693should be activated, given the period indicated by the user in the
1694namcouple. See OASIS3\_MCT User Guide section 2.2.7 for details.
1695
1696The data is sent with the {\tt put} function.
1697
1698\begin{verbatim}
1699   ____________________________________________________________
1700   date = int(0)
1701   variable.put(date, field)
1702   ____________________________________________________________
1703\end{verbatim}
1704
1705Conversely, it is received with the {\tt get} function, which fills the {\tt pyoasis.asarray} object.
1706
1707\begin{verbatim}
1708   ____________________________________________________________
1709   variable.get(date, field)
1710   ____________________________________________________________
1711\end{verbatim}
1712
1713\item{Termination}
1714\label{{index:termination}}
1715
1716Finally, the coupling is terminated with the destruction of
1717the component:
1718
1719\begin{verbatim}
1720   ____________________________________________________________
1721   del comp
1722   ____________________________________________________________
1723\end{verbatim}
1724
1725
1726\item{Exceptions and aborting}
1727\label{{index:exceptions-and-aborting}}
1728
1729When an error occurs in OASIS3\_MCT, the code coupler returns an error
1730code and an {\tt OasisException} is raised. In practice, OASIS3\_MCT will
1731internally handle the error, write an error message in its
1732debug log files and to the screen, and abort before the exception is raised. It
1733may also happen that the code aborts before the error message appears
1734on the screen.
1735
1736When an error is caught by the pyOASIS wrapper, such as an incorrect parameter
1737or a wrong argument type, a {\tt PyOasisException} is raised.
1738
1739In the following example, where we attempt to initialise a component,
1740a {\tt PyOasisException} will be raised as the user supplies an empty
1741name :
1742
1743\begin{verbatim}
1744   ____________________________________________________________
1745   try:
1746         comp = pyoasis.Component("")
1747   except (pyoasis.PyOasisException) as exception:
1748           pyoasis.pyoasis_abort(exception)
1749   ____________________________________________________________
1750\end{verbatim}
1751
1752The function {\tt pyoasis.pyoasis\_abort} takes an exception as argument.
1753It stops the execution  of all the processes after having displayed an error message and
1754written information in the log files about the error and the context in
1755which it took place.
1756
1757Another function is available, {\tt pyoasis.oasis\_abort},
1758for the cases where a voluntary abort is needed in the code where or
1759not an exception has been raised. Its interface mimics the
1760corresponding OASIS3\_MCT functio {\tt oasis\_abort}.
1761
1762\end{itemize}
1763
1764\subsection{Fortran python API correspondence}
1765
1766Figures \ref{python_corresp_init} , \ref{python_corresp_grid} and \ref{python_corresp_terminate} show the Fortran python API correspondence for different parts of the API. Different examples implementing the different parts of the API with the Fortran, C and python interfaces are also provided as practical illustrations in directory {\tt pyoasis/examples} and are described in section \ref{subsec_equivalent}.
1767
1768\begin{figure}[h]
1769  \includegraphics[scale=.45]{figures/corresp_init.pdf}
1770  \caption{Fortran python AP correspondence for the initialisation, communication and partition definition.}
1771  \label{python_corresp_init}
1772\end{figure}
1773
1774\begin{figure}[h]
1775  \includegraphics[scale=.45]{figures/corresp_grid.pdf}
1776  \caption{Fortran python AP correspondence for the grid definition, variable declaration and end of definition phase.}
1777  \label{python_corresp_grid}
1778\end{figure}
1779
1780\begin{figure}[h]
1781  \includegraphics[scale=.45]{figures/corresp_terminate.pdf}
1782  \caption{Fortran python AP correspondence for the coupling field exchanges, termination and auxialiary routines.}
1783  \label{python_corresp_terminate}
1784\end{figure}
1785
1786\newpage
1787
1788\section{Additional notes on coupling functionality}
1789\label{sec_notes}
1790
1791\subsection{A brief overview of MCT}
1792\label{subsec_MCT}
1793
1794As described elsewhere, OASIS3-MCT leverages the MCT 2.11 coupling infrastructure
1795developed at Argonne National Laboratory.  That infrastructure is designed
1796to couple fields on static grids between model components.  The fields
1797can be decomposed using MPI across mutiple processes, and the decomposition
1798and number of proceses involved can be arbitrary in each component.  MCT
1799supports both communication of data between unique MPI non-overlapping
1800communicators and
1801within a single MPI communicator, although these two operations are functionally
1802independent within MCT. 
1803
1804MCT also provides the ability to map (i.e. interpolate or
1805regrid) data between grids as long as the interpolation is linear and can
1806be computed by a linear sparse matrix multiply. 
1807MCT does not compute interpolation weights, but
1808it has interfaces that allow those weights to be passed into MCT.  Within
1809MCT, mapping and communication are also treated as independent features.
1810
1811OASIS3-MCT supports both mapping and communication of data through MCT.  As
1812a results, mapping and coupling are implemented, in many ways, as separate
1813steps in the underlying implementation.  A coupling field that also requires
1814mapping will carry out the mapping on either the source or destination component
1815on the associated processes, while coupling between components will be done
1816either before or after mapping.  In other words, a coupling field can be
1817interpolated to
1818the destination grid on the source processes then communicated to the destination
1819component OR a coupling field can be communicated to the destination component
1820on the source grid then mapped to the destination grid on the destination
1821component.  At the present time, it is not possible to map the field as part
1822of the communication rearrangement, although in theory, that capability should
1823be possible to implement in the future, and OASIS3-MCT developers are considering
1824it.  The separation of mapping and communication is handled by the OASIS
1825layer.  Users only need to be aware of a few options that can be set to fine
1826tune the performance of these operations.
1827
1828OASIS3-MCT also imposes a few other constraints on the usage.  MCT
1829does NOT support haloed communication.  There must be a 1-to-1 relationship
1830between grid point values on the source grid and on the destination grid.
1831A user cannot send a single grid point value to multiple destination gridcells
1832or processes via OASIS3-MCT.  The partitions defined by the user that define
1833the field decomposition cannot reference the same global gridcell more than once.
1834
1835OASIS3-MCT does not support dynamically varying grids nor dynamically varying
1836decompositions at the present time.  MCT is NOT currently setup to support those
1837features.  In the future, it is possible that some dynamic grid capabilities
1838might be supported through OASIS3-MCT but requirements and implementation are
1839still being assessed.
1840
1841Since the OASIS3-MCT\_4.0 release, OASIS3-MCT includes a mixed MPI+OpenMP parallel version of the SCRIP library for the calculation of the remapping weights (see section \ref{subsec_interp}). But besides this aspect, MCT itself, and therefore the communication layer in OASIS, has only minimal OpenMP support at the
1842present time.  Users are discouraged from calling OASIS3-MCT {\tt oasis\_put} and {\tt oasis\_get}
1843from a threaded region.  And it is not possible to define a partition (decomposition)
1844across multiple active threads.  Most of the work that OASIS3-MCT does related to decomposition rearrangement for communication or mapping per se is MPI-based.  There
1845are some floating point operations in the sparse matrix multiple as well as in
1846some of the OASIS3-MCT transforms; but generally, these are highly parallel and
1847would often not benefit from use of OpenMP.  OASIS3-MCT is designed primarily to
1848support large memory parallel applications that require efficient and scalable
1849coupling and mapping capabilities.
1850
1851MCT only supports coupling of integer or floating point data.  Fields based on
1852logicals and character strings cannot be coupled.
1853
1854\subsection{Coupling scalar values}
1855\label{subsec_scalar}
1856
1857OASIS3-MCT does not have a distinct feature to support scalar coupling.  By
1858scalar coupling, we mean  scalar variables such as date and time,
1859logical flags, integer or real parameters, or other scalar data that might be
1860defined identically across all MPI tasks in a component or even just on the
1861root task or a subset of tasks. 
1862It is often desirable to communicate scalar data between components
1863to coordinate scientific or technical features.  There isn't a feature that
1864supports this type of coupling specifically, but it is possible to do so using 
1865available interfaces.  The most robust implementation is probably to setup
1866root to root coupling of scalars between components.  To do so :
1867
1868\begin{itemize}
1869\item Determine the number and type of scalars to couple
1870\item Allocate an array in each component of that size
1871\item Initialize a partition using the POINTS approach (see section \ref{subsubsec_pointspart}) with the size of the array assigned to the root process in each component and no portion of the partition allocated to other processes. 
1872\item Define coupling field names for use in both the model components and the namcouple file and declare variables as usual.  Create a set of namcouple inputs.  You will not need any mapping.
1873\item Use the {\tt oasis\_get} and {\tt oasis\_put} interfaces to communicate data between components.
1874\item If required, broadcast the scalars after being received from the root process to the other processes, outside the OASIS3-MCT layer.
1875\end{itemize}
1876
1877This can be extended as needed to send or receive from non root processes.  But
1878you cannot send the scalar data to multiple processes as this violates the halo
1879restriction in MCT.  Users need to be aware which processes contain valid scalar
1880data both on the source and destination side and to manage data synchronization
1881between processes within a component outside the OASIS3-MCT layer.
1882
1883\vspace{-0.3cm}
1884\subsection{The lag concept}
1885\label{subsec_lag}
1886
1887Using the OASIS3-MCT coupling library, the user has the flexibility
1888to reproduce different coupling algorithms defining {\tt LAG} values for the different coupling fields .
1889In the components, the
1890sending and receiving routines, respectively {\tt oasis\_put} and {\tt
1891  oasis\_get}, can be called at each component timestep, with the
1892appropriate {\tt date} argument giving the actual time (at the
1893beginning of the timestep), expressed in number of seconds since the
1894start of the run, or in any other time units as long as the same are
1895used in all components and in the {\it namcouple} (see section
1896\ref{prismput}). This {\tt date} argument is automatically analysed by
1897the coupling library and depending on the coupling period and the lag chosen by the user for the coupling field in the {\it namcouple} ({\tt LAG}), different coupling algorithms can be reproduced
1898without modifying the component codes themselves.
1899
1900The lag ({\tt LAG}) must be
1901expressed in the time unit used (that must be the same in the components
1902and in the {\it namcouple}, see section \ref{prismput}) and can be
1903positive or negative but should never be larger (in absolute
1904magnitude) than the coupling period of any field due to problems with
1905restartability and dead-locking. When a component calls a {\tt
1906  oasis\_put}, the value of the lag is automatically added to the
1907value of the {\tt date} argument and the ``put'' is actually performed
1908when the sum {\tt date+lag} is a coupling time; in the target
1909component, this ``put'' will match a {\tt oasis\_get} for which the
1910{\tt date} argument is the same coupling time.
1911% A positive lag indicates the put will occur sooner than zero lag and
1912% a negative lag tells the coupler to send the data later than zero
1913% lag.
1914So the lag only shifts the time at which the data is sent but not
1915the time at which the data is received.
1916 
1917When the lag is positive, a restart file must be available to initiate
1918the coupling.  For a field with positive lag, the source
1919component automatically reads the field in the restart file
1920during the coupling initialization phase (below the {\tt
1921  oasis\_enddef}) and send the data to match the {\tt oasis\_get}
1922performed at time=0 in the target component. The final coupling
1923data on the source side will then be automatically written to the
1924restart file for use in the next run\footnote{When there is a lag, the first and last instance of the source field
1925in the debug netCDF file (EXPOUT fields, see section
1926\ref{subsec_namcouplesecond}) always correspond respectively to the
1927field read from and written to the restart file.}.
1928
1929On the 4 figures in this section, short black arrows correspond to
1930  {\tt oasis\_put} or {\tt oasis\_get} called in the component
1931  that do not lead to any ``put" or receiving action; long black
1932  arrows correspond to {\tt oasis\_put} or {\tt oasis\_get} called in
1933  the components that do actually lead to a ``put" or ``get''
1934  action; long red arrows correspond to {\tt oasis\_put} or {\tt
1935    oasis\_get} called in the component models that lead to a reading
1936  or writing of the coupling field from or to a coupling restart file.
1937 
1938\begin{enumerate}
1939
1940\item LAG concept first example
1941 
1942  A first coupling algorithm, exploiting the LAG concept, is
1943  illustrated on figure \ref{fig:lag_concept_1}.
1944
1945  \begin{figure}
1946    \includegraphics[scale=.6]{figures/fig_lag_concept_1}
1947    \caption{LAG concept first example}
1948    \label{fig:lag_concept_1}
1949  \end{figure}
1950
1951  During a coupling timestep, model A receives $F_2$ and then sends
1952  $F_1$; its timestep length is 4. During a coupling timestep, model B
1953  receives $F_1$ and then sends $F_2$; its timestep length is 6.
1954  $F_1$ and $F_2$ coupling periods are respectively 12 and 24. If
1955  $F_1$/$F_2$ ``put" action by model A/B was used at a coupling
1956  timestep to match the model B/A ``get" action, a deadlock would
1957  occur as both models would be initially waiting on a ``get"
1958  action. To prevent this, $F_1$ and $F_2$ produced at the timestep
1959  before have to be used to match respectively the model B and model A
1960  ``get" actions.
1961
1962  This implies that a lag of respectively 4 and 6 seconds must be
1963  defined for $F_1$ and $F_2$. For $F_1$, the {\tt oasis\_put}
1964  performed at time 8 and 20 by model A will then lead to ``put"
1965  actions (as 8 + 4 = 12 and 20 + 4 = 24 which are coupling periods)
1966  that match the ``get" actions performed by {\tt oasis\_get} called by model B
1967  at times 12 and 24.  For $F_2$, the {\tt oasis\_put}
1968  performed at time 18 by model B then leads to a ``put" action (as 18
1969  + 6 = 24 which is a coupling period) that matches the ``get" action
1970  performed at time 24 by the {\tt oasis\_get} called by model A.
1971
1972  At the beginning of the run, as their LAG index is greater than 0,
1973  the first {\tt oasis\_get} of $F_1$ and $F_2$ will automatically be
1974  fulfilled with fields read from their respective coupling restart
1975  files. The user therefore has to create those coupling restart files
1976  before the first run in the experiment. At the end of the run, $F_1$
1977  having a lag greater than 0, is automatically written to its
1978  coupling restart file below the last $F_1$ {\tt oasis\_put} as the
1979  {\tt date+lag} equals the total run time. The analogue is true for
1980  $F_2$. These coupling restart fields will automatically be read in at the beginning
1981  of the next run below the respective {\tt oasis\_get}.
1982
1983\item LAG concept second example
1984
1985  A second coupling algorithm exploiting the LAG concept is
1986  illustrated on figure \ref{fig:lag_concept_2}. During its timestep,
1987  model A receives $F_2$, sends $F_3$ and then $F_1$; its timestep
1988  length is 6. During its timestep, model B receives $F_1$, receives
1989  $F_3$ and then sends $F_2$; its timestep length is also 6.  $F_1$,
1990  $F_2$ and $F_3$ coupling periods are both supposed to be equal to
1991  12.
1992 
1993  \begin{figure}
1994    \includegraphics[scale=.6]{figures/fig_lag_concept_2}
1995    \caption{LAG concept second example}
1996    \label{fig:lag_concept_2}
1997  \end{figure}
1998
1999  For $F_1$ and $F_2$ the situation is similar to the first
2000  example. Without any lag specified and without any restart file, a deadlock
2001  would occur as both models would be waiting on a ``get" action. To
2002  prevent this, $F_1$ and $F_2$ produced at the timestep before have
2003  to be used to match the model A and model B ``get" actions, which
2004  means that a lag of 6 must be defined for both $F_1$ and $F_2$. For
2005  both coupling fields, the {\tt oasis\_put} performed at times 6 and
2006  18 by the source model then lead to ``put" actions (as 6 + 6 = 12
2007  and 18 + 6 = 24 which are coupling periods) that match the ``get"
2008  action performed at time 12 and 24 by the {\tt oasis\_get} called
2009  by the target model.
2010
2011  For $F_3$, sent by model A and received by model B, no lag needs to
2012  be defined: the coupling field produced by model A at the coupling
2013  timestep can be ``consumed'' by model B without causing a deadlock
2014  situation.
2015
2016  As in the first example, the {\tt oasis\_get} performed at the
2017  beginning of the run for $F_1$ and $F_2$, will automatically receive
2018  data read from their coupling restart files, and the last {\tt
2019    oasis\_put} performed at the end of the run automatically write
2020  them to their coupling restart file. For $F_3$, no coupling restart
2021  file is needed.
2022
2023  We see here how the introduction of appropriate LAG indices results
2024  in receiving in the target component the coupling fields produced by the
2025  source component the time step before; this is, in some coupling
2026  configurations, essential to avoid deadlock situations.
2027
2028\end{enumerate}
2029
2030\vspace{-0.3cm}
2031\subsection{The sequence concept}
2032\label{subsec_sec}
2033
2034The order of coupling operations in the system is determined solely by
2035the order of calls to send ({\tt oasis\_put} or ``put'') and receive ({\tt oasis\_get} or ``get'') data in the components
2036in conjunction with the setting of the lag in the {\it namcouple}.
2037Data that is received is always blocking while data that is sent is non-blocking with respect to the component making that call.  It
2038is possible to deadlock the system if the relative orders of puts and
2039gets in different components are not compatible.
2040
2041With OASIS3-MCT, the sequence (SEQ) index in the {\it namcouple} file
2042now provides the coupling layer with an ability to detect a deadlock
2043before it happens and exit.  It does this by tracking the order of get
2044and put calls in components compared to the SEQ specified in the {\it
2045  namcouple}.  If there are any inconsistencies, the component will abort
2046gracefully with a useable error message before the system deadlocks.
2047If there are any coupling dependencies in the system, use of the SEQ
2048index is recommended for diagnosis but has no impact on the ultimate
2049solution and is NOT required.
2050
2051In the following two examples, there are two
2052models, each ``put" a field to the other at every coupling period
2053without any lags.  In the first case, there is no dependency as each
2054model first sends and then receives some data.
2055
2056\begin{verbatim}
2057     model1        model2
2058     ------        ------
2059    put(fld1)     put(fld2)
2060    get(fld2)     get(fld1)
2061\end{verbatim}
2062
2063In this
2064case, there is no sequencing dependency and the value of SEQ must be
2065identical (or unset) in the {\it namcouple} description of the fld1
2066and fld2 coupling.  If by mistake, SEQ is set to 1 for fld1 and 2 for fld2,
2067then the coupled model will abort because at runtime, the coupler will
2068detect in model 2 that fld2 was sent before fld1 was received which
2069is out of sequence as defined by the SEQ settings.
2070
2071In the next example, there is a dependency in the sequencing.
2072
2073\begin{verbatim}
2074     model1        model2
2075     ------        ------
2076    put(fld1)     get(fld1)
2077                  fld2=g(fld1)
2078    get(fld2)     put(fld2)
2079\end{verbatim}
2080
2081In model2, fld2 depends on fld1. If SEQ is not used and if, for example, model1 does not have the
2082consistent ordering of the put and get shown above (required by model2), then the models would deadlock and hang. If this dependency is known, there is a benefit in setting SEQ=1 for fld1 and SEQ=2 for fld2; at
2083runtime, if the sequencing of model1 or model2 does not match the
2084above diagram, then the  coupling layer will detect it and will exit gracefully with an error message.
2085
2086Again, the SEQ namecouple setting is only diagnostic and is not
2087required.
2088
2089
2090
2091
2092
Note: See TracBrowser for help on using the repository browser.