5 ==========================================================
6 CALCIUM C and Fortran coupling library
7 ==========================================================
8 This section is the CALCIUM coupling library reference for C and Fortran.
9 For python, see module :mod:`calcium`.
11 Presentation is done in the following order:
13 - include files : constants
14 - functions to connect and disconnect
15 - functions to export data
16 - functions to import data
17 - functions to erase data
23 All constants used with the coupling library are defined in an include file.
33 .. code-block:: fortran
39 - Error codes (see :ref:`errcodes`)
40 - Dependency modes (CP_TEMPS, CP_ITERATION or CP_SEQUENTIEL)
41 - Disconnection modes (CP_CONT or CP_ARRET)
46 Functions to connect and disconnect
47 ===============================================
51 .. function:: int info = cp_cd(long *compo, char *instance_name)
53 Initialize the connection with YACS.
57 .. function:: call cpcd(compo, instance_name, info)
59 :param compo: component address
61 :param instance_name: instance name given by YACS
62 :type instance_name: 64 characters string, output
63 :param info: error code (possible codes: CPMACHINE)
64 :type info: int, return
69 .. function:: int info = cp_fin(long *compo, int directive)
71 Close the connection with YACS.
75 .. function:: call cpfin(compo, directive, info)
77 :param compo: component address
79 :param directive: indicate how variables will be handled after disconnection. If directive = CP_CONT,
80 variables produced by this component are defined constant beyond the last time or iteration number. If
81 directive = CP_ARRET, variables are not defined beyond the last step.
83 :param info: error code (possible codes: CPDNDI)
84 :type info: int, return
87 Functions to export data
88 ===============================
89 Writing requests is the way for a component to export data through one of its
90 output ports. nm_var is the port name.
92 Each request exports n values from the var_xxx array. Exported values are defined
93 at time t if mode is CP_TEMPS or at iteration number i if mode is CP_ITERATION.
95 It is mandatory to export data at increasing times or iteration numbers.
96 The export is done in an asynchronous way. Control is given back to the caller as
97 soon as data have been sent.
99 There is generally one request for each data type to export. Each request can be used with one and only port type.
100 For long type, two requests are available (cp_elg and cp_eln). One (cp_elg) is working with CALCIUM_integer port type
101 and the second (cp_eln) is working with CALCIUM_long port type.
103 The available port types are :
115 .. function:: int info = cp_ere(long *compo, int dep, float t, int i, char *nm_var, int n, float *var_real)
117 for single precision floating point values (C float type and CALCIUM_float port type)
118 .. function:: int info = cp_edb(long *compo, int dep, double td, int i, char *nm_var, int n, double *var_double)
120 for double precision floating point values (C double type and CALCIUM_double port type)
121 .. function:: int info = cp_ecp(long *compo, int dep, float t, int i, char *nm_var, int n, float *var_complex)
123 for complex values (C float type and CALCIUM_complex port type)
124 .. function:: int info = cp_een(long *compo, int dep, float t, int i, char *nm_var, int n, int *var_integer)
126 for integer values (C int type and CALCIUM_integer port type)
127 .. function:: int info = cp_elg(long *compo, int dep, float t, int i, char *nm_var, int n, long *var_long)
129 for integer values (C long type and CALCIUM_integer port type)
130 .. function:: int info = cp_eln(long *compo, int dep, float t, int i, char *nm_var, int n, long *var_long)
132 for integer values (C long type and CALCIUM_long port type)
133 .. function:: int info = cp_elo(long *compo, int dep, float t, int i, char *nm_var, int n, int *var_boolean)
135 for boolean values (C int type and CALCIUM_logical port type)
136 .. function:: int info = cp_ech(long *compo, int dep, float t, int i, char *nm_var, int n, char **var_string, int strSize)
138 for string values (C char* type and CALCIUM_string port type)
142 .. function:: CALL CPERE(LONGP compo, INTEGER dep, REAL*4 t, INTEGER i, nm_var, INTEGER n, REAL*4 var_real, INTEGER info)
143 .. function:: CALL CPEDB(LONGP compo, INTEGER dep, REAL*8 td, INTEGER i, nm_var, INTEGER n, REAL*8 var_double, INTEGER info)
144 .. function:: CALL CPECP(LONGP compo, INTEGER dep, REAL*4 t, INTEGER i, nm_var, INTEGER n, REAL*4 var_complex, INTEGER info)
145 .. function:: CALL CPEEN(LONGP compo, INTEGER dep, REAL*4 t, INTEGER i, nm_var, INTEGER n, INTEGER var_integer, INTEGER info)
146 .. function:: CALL CPELG(LONGP compo, INTEGER dep, REAL*4 t, INTEGER i, nm_var, INTEGER n, INTEGER*8 var_long, INTEGER info)
148 can only be used on 64 bits architecture.
149 .. function:: CALL CPELN(LONGP compo, INTEGER dep, REAL*4 t, INTEGER i, nm_var, INTEGER n, INTEGER*8 var_long, INTEGER info)
151 can only be used on 64 bits architecture.
152 .. function:: CALL CPEIN(LONGP compo, INTEGER dep, REAL*4 t, INTEGER i, nm_var, INTEGER n, INTEGER*4 var_int, INTEGER info)
153 .. function:: CALL CPELO(LONGP compo, INTEGER dep, REAL*4 t, INTEGER i, nm_var, INTEGER n, INTEGER*4 var_boolean,INTEGER info)
154 .. function:: CALL CPECH(LONGP compo, INTEGER dep, REAL*4 t, INTEGER i, nm_var, INTEGER n, var_string, INTEGER info)
156 :param compo: component address
158 :param dep: dependency type - CP_TEMPS (time dependency) or CP_ITERATION (iteration dependency)
160 :param t: time value if dep=CP_TEMPS
162 :param td: time value if dep=CP_TEMPS
164 :param i: iteration number if dep=CP_ITERATION
166 :param nm_var: port name
167 :type nm_var: string (64 characters)
168 :param n: number of values to export (from var_xxx array)
170 :param var_real: array containing the values to export
171 :type var_real: float array
172 :param var_complex: array containing the values to export (array size is twice the number of complex numbers)
173 :type var_complex: float array
174 :param var_integer: array containing the values to export
175 :type var_integer: int array
176 :param var_long: array containing the values to export
177 :type var_long: long array
178 :param var_boolean: array containing the values to export
179 :type var_boolean: int array
180 :param var_string: array containing the values to export
181 :type var_string: array of strings
182 :param var_double: array containing the values to export
183 :type var_double: double array
184 :param strSize: size of strings in var_string
186 :param info: error code (possible codes: CPIT, CPITVR, CPNMVR, CPNTNULL, CPIOVR, CPTPVR or CPCTVR)
187 :type info: int, return
190 LONGP is a Fortran type that is same size as the C long type, so, most of a time, INTEGER\*4 for 32 bits architecture
191 and INTEGER\*8 for 64 bits architecture.
194 CPELG (or cp_elg) can produce wrong results (conversion problem) on 64 bits architecture if the KERNEL module is built
195 with the default option (--with-cal_int=int)
197 See :ref:`fortran64bits` for more details.
200 Functions to import data
201 =============================
202 Reading requests is the way for a component to import data through one of its
203 input ports. nm_var is the port name. Import is only possible if the input port
204 is connected to an output port.
206 Reading requests can be of two kinds:
210 A standard request imports data at a given time or iteration number. In case of time dependency, the effective time
211 is calculated by YACS from the interval time (ti, tf) and the interpolation scheme that is given in the coupling file.
213 A sequential request imports data in a sequential way. Each request returns
214 the next data with its associated time or iteration number.
216 There is generally one request for each data type to import. Each request can be used with one and only port type.
217 For long type, two requests are available (cp_llg and cp_lln). One (cp_llg) is working with CALCIUM_integer port type
218 and the second (cp_lln) is working with CALCIUM_long port type.
222 .. function:: int info = cp_lre(long *compo, int dep, float *ti, float *tf, int *i, char *nm_var, int len, int *n, float *var_real)
223 .. function:: int info = cp_ldb(long *compo, int dep, double *tid, double *tfd, int *i, char *nm_var, int len, int *n, double *var_double)
224 .. function:: int info = cp_lcp(long *compo, int dep, float *ti, float *tf, int *i, char *nm_var, int len, int *n, float *var_complex)
225 .. function:: int info = cp_len(long *compo, int dep, float *ti, float *tf, int *i, char *nm_var, int len, int *n, int *var_integer)
226 .. function:: int info = cp_llg(long *compo, int dep, float *ti, float *tf, int *i, char *nm_var, int len, int *n, long *var_long)
227 .. function:: int info = cp_lln(long *compo, int dep, float *ti, float *tf, int *i, char *nm_var, int len, int *n, long *var_long)
228 .. function:: int info = cp_llo(long *compo, int dep, float *ti, float *tf, int *i, char *nm_var, int len, int *n, int *var_boolean)
229 .. function:: int info = cp_lch(long *compo, int dep, float *ti, float *tf, int *i, char *nm_var, int len, int *n, char **var_string, int strSize)
233 .. function:: CALL CPLRE(LONGP compo, INTEGER dep, REAL*4 ti, tf, INTEGER i, nm_var,INTEGER len, INTEGER n,REAL*4 var_real, INTEGER info)
234 .. function:: CALL CPLDB(LONGP compo, INTEGER dep, REAL*8 tid, tf, INTEGER i, nm_var,INTEGER len, INTEGER n,REAL*8 var_double, INTEGER info)
235 .. function:: CALL CPLCP(LONGP compo, INTEGER dep, REAL*4 ti, tf, INTEGER i, nm_var,INTEGER len, INTEGER n,REAL*4 var_complex, INTEGER info)
236 .. function:: CALL CPLEN(LONGP compo, INTEGER dep, REAL*4 ti, tf, INTEGER i, nm_var,INTEGER len, INTEGER n,INTEGER var_integer, INTEGER info)
237 .. function:: CALL CPLLG(LONGP compo, INTEGER dep, REAL*4 ti, tf, INTEGER i, nm_var,INTEGER len, INTEGER n,INTEGER*8 var_long, INTEGER info)
239 can only be used on 64 bits architecture.
240 .. function:: CALL CPLLN(LONGP compo, INTEGER dep, REAL*4 ti, tf, INTEGER i, nm_var,INTEGER len, INTEGER n,INTEGER*8 var_long, INTEGER info)
242 can only be used on 64 bits architecture.
243 .. function:: CALL CPLIN(LONGP compo, INTEGER dep, REAL*4 ti, tf, INTEGER i, nm_var,INTEGER len, INTEGER n,INTEGER*4 var_int, INTEGER info)
244 .. function:: CALL CPLLO(LONGP compo, INTEGER dep, REAL*4 ti, tf, INTEGER i, nm_var,INTEGER len, INTEGER n,INTEGER*4 var_boolean,INTEGER info)
245 .. function:: CALL CPLCH(LONGP compo, INTEGER dep, REAL*4 ti, tf, INTEGER i, nm_var,INTEGER len, INTEGER n,var_string, INTEGER info)
247 :param compo: component address
249 :param dep: dependency type - CP_TEMPS (time dependency) or CP_ITERATION (iteration dependency)
251 :param ti: interval start time if dep=CP_TEMPS or associated time if dep=CP_SEQUENTIEL
252 :type ti: float, inout
253 :param tf: interval end time if dep=CP_TEMPS
255 :param tid: interval start time if dep=CP_TEMPS or associated time if dep=CP_SEQUENTIEL
256 :type tid: double, inout
257 :param tfd: interval end time if dep=CP_TEMPS
259 :param i: iteration number if dep=CP_ITERATION or dep=CP_SEQUENTIEL
261 :param nm_var: port name
262 :type nm_var: string (64 characters)
263 :param len: size of var_xxx array
265 :param n: effective number of values imported (into var_xxx array)
267 :param var_real: array containing the values imported
268 :type var_real: float array, out
269 :param var_complex: array containing the values imported (array size is twice the number of complex numbers)
270 :type var_complex: float array, out
271 :param var_integer: array containing the values imported
272 :type var_integer: int array, out
273 :param var_long: array containing the values imported
274 :type var_long: long array, out
275 :param var_boolean: array containing the values imported
276 :type var_boolean: int array, out
277 :param var_string: array containing the values imported
278 :type var_string: array of strings (char*), out
279 :param var_double: array containing the values imported
280 :type var_double: double array, out
281 :param strSize: size of strings in var_string
283 :param info: error code (possible codes: CPIT, CPITVR, CPNMVR, CPNTNULL, CPIOVR, CPTPVR, CPLIEN, CPATTENTE, CPLGVR or CPSTOP)
284 :type info: int, return
287 LONGP is a Fortran type that is same size as the C long type, so, most of a time, INTEGER\*4 for 32 bits architecture
288 and INTEGER\*8 for 64 bits architecture.
291 CPLLG (or cp_llg) can produce wrong results (conversion problem) on 64 bits architecture if the KERNEL module is built
292 with the default option (--with-cal_int=int)
294 See :ref:`fortran64bits` for more details.
296 Functions to erase data
297 ==========================
298 The functions cp_fini and cp_fint are used to request that all values of the specified
299 variable defined for iteration number or time before a given one be erased.
301 The functions cp_effi and cp_efft are used to request that all values of the specified
302 variable defined for iteration number or time after a given one be erased.
306 .. function:: int info = cp_fini(long *compo, char *nm_var, int i)
308 Erase all values of port nm_var before iteration i
312 .. function:: call cpfini(compo, nm_var, i, info)
314 :param compo: component address
316 :param nm_var: port name
318 :param i: iteration number
320 :param info: error code
321 :type info: int, return
325 .. function:: int info = cp_fint(long *compo, char *nm_var, float t)
327 Erase all values of port nm_var before time t
331 .. function:: call cpfint(compo, nm_var, t, info)
333 :param compo: component address
335 :param nm_var: port name
339 :param info: error code
340 :type info: int, return
344 .. function:: int info = cp_effi(long *compo, char *nm_var, int i)
346 Erase all values of port nm_var after iteration i
350 .. function:: call cpfini(compo, nm_var, i, info)
352 :param compo: component address
354 :param nm_var: port name
356 :param i: iteration number
358 :param info: error code
359 :type info: int, return
363 .. function:: int info = cp_efft(long *compo, char *nm_var, float t)
365 Erase all values of port nm_var after time t
369 .. function:: call cpfint(compo, nm_var, t, info)
371 :param compo: component address
373 :param nm_var: port name
377 :param info: error code
378 :type info: int, return
384 If you want to export a single precision real array and import an integer array in iteration mode,
385 you could write a subroutine as follows. Connection and disconnection must be done only once.
387 .. code-block:: fortran
389 subroutine coupling(compo)
393 integer i, info, n, ai(10), nval
396 call cpcd(compo,name,info)
399 C export 10 real values at iteration 1 on port outa
400 call cpere(compo,CP_ITERATION,t,i,'outa',n,af,info)
401 C import 10 integer values at iteration 1 on port ina
402 call cplen(compo,CP_ITERATION,ti,tf,i,'ina',n,nval,ai,info)
405 call cpfin(compo,CP_CONT,info)
410 If you want to export a double precision real array and import an integer array in time mode,
411 you could write a function as follows. Connection and disconnection must be done only once.
416 void coupling(void* compo)
420 double af[10], td,tf;
423 info = cp_cd(compo,name);
427 // export 10 double values at time 0. on port outa
428 info = cp_edb(compo,CP_TEMPS,td,i,"outa",n,af);
430 // import 10 integer values at interval time (0.,1.) on port ina
431 // (by default, it is imported at start time 0.)
432 info = cp_len(compo,CP_TEMPS,&td,&tf,&i,"ina",n,&nval,ai);
434 info = cp_fin(compo,CP_CONT);
444 ========= ============ =================================
445 Code Value Explanation
446 ========= ============ =================================
448 CPERIU 1 Emitter unknown
449 CPNMVR 2 Variable name unknown
450 CPIOVR 3 Different input/output codes in code and supervisor
451 CPTP 4 Variable type unknown
452 CPTPVR 5 Different variable types in code and supervisor
453 CPIT 6 Dependency mode unknown
454 CPITVR 7 Different dependency modes in code and supervisor
455 CPRENA 8 Unauthorized request
456 CPDNTP 9 Unauthorized disconnection request type
457 CPDNDI 10 Unauthorized disconnection directive
458 CPNMCD 11 Code name unknown
459 CPNMIN 12 Instance name unknown
460 CPATTENTE 13 Waiting request
462 CPNTNUL 15 Zero value number
463 CPLGVR 16 Insufficient variable length
464 CPSTOP 17 Instance is going to stop
465 CPATAL 18 Unexpected instance stop
466 CPNOCP 19 Manual execution
467 CPCTVR 20 Output variable not connected
468 CPPASNULL 21 Number of steps to execute is nul
469 CPMACHINE 22 Computer not declared
470 CPGRNU 23 Environment variable COUPLAGE_GROUPE is not set
471 CPGRIN 24 Instance group given by COUPLAGE_GROUPE is wrong
472 CPERRFICH 26 Format error in input file
473 CPNORERR 27 Request ignored because of switching to NORMAL mode
474 CPRUNERR 28 Supervisor is in normal execution mode
475 CPOPT 29 Unknown option
476 CPVALOPT 30 Option value is wrong
477 CPECREFF 31 Impossible to write because of an erasing request
478 CPLIEN 32 Reading of a variable wrongly connected
479 CPDECL 35 Error in declaration
480 CPINEXEC 36 Error in instance launching
481 CPCOM 37 Communication error
482 CPMODE 39 Execution mode not defined
483 CPINSTDEC 40 Disconnected instance
484 ========= ============ =================================
488 Some considerations about architecture (32, 64 bits) and programming language (C, Fortran)
489 =============================================================================================
490 Depending on the architecture and the language, types have varying sizes.
491 For example, below we compare the size of C and Fortran types for two Linux distributions. One is Debian etch 32 bits
492 and the other is Debian lenny 64 bits.
494 **Type size (in bytes) in C:**
496 ======================= ==================== ===================
497 Architecture, compiler 32 bits, gcc 3.3 64 bits, gcc 4.3
498 ======================= ==================== ===================
506 ======================= ==================== ===================
508 **Type size (in bytes) in Fortran:**
510 ======================= ================== ======================= =======================================================
511 Architecture, compiler 32 bits, g77 3.3 64 bits, gfortran 4.3 64bits, gfortran -fdefault-integer-8 -fdefault-real-8
512 ======================= ================== ======================= =======================================================
516 double precision 8 8 8
518 ======================= ================== ======================= =======================================================
520 With another architecture or compiler, sizes can be different.
522 Most of a time, Fortran INTEGER is mapped on C int type. So it is 4 bytes wide and equivalent to INTEGER\*4.
523 This is the case for 32 bits architecture and 64 bits architecture with standard fortran options.
525 It is possible, with special options, to map Fortran INTEGER on C long type (-i8 with intel compiler or
526 -fdefault-integer-8 with gnu fortran, for example). In this case, using the standard CALCIUM API can be cumbersome.
528 It is possible to build the SALOME KERNEL module with a special option (--with-cal_int=long) to match this kind of mapping.
530 By using the cp_een call (or CPEEN fortran call), it is possible to write a code that is independent from the mapping
531 and that can always use fortran INTEGER type.