Basilisk CFD
Adaptive Cartesian mesh PDE framework
Loading...
Searching...
No Matches
cvmix.h
Go to the documentation of this file.
1/** @file cvmix.h
2 */
3/**
4# Interface with the Community Ocean and Vertical Mixing (CVMix) project
5
6Community Ocean Vertical Mixing
7([CVMix](https://github.com/CVMix/CVMix-description/raw/master/cvmix.pdf
8)) is a software package that aims to provide transparent, robust,
9flexible, well documented, shared Fortran source code for use in
10parameterizing vertical mixing processes in numerical ocean models.
11
12This header file provides a C-language interface for Basilisk (and
13other projects, since the interface does not depend on Basilisk
14itself).
15
16## Installation
17
18[CVMix](https://github.com/CVMix) needs to be
19[installed](http://cvmix.github.io/#building) first and its source
20code must be accessible, since the C interface is generated
21automatically from the Fortran sources.
22
23Once this done the C interface can be built using this [Makefile]()
24and the correct location for the sources and/or Fortran compiler. For
25example:
26
27~~~bash
28cd \f$BASILISK/src/cvmix/
29CVMIX=\f$HOME/local/cvmix F90=gfortran FCFLAGS="-Wall -O2" make
30~~~
31
32## Interoperability with Fortran 90
33
34Aside from standard considerations on compatibility of different types
35between C and Fortran (as documented
36[here](https://northstar-www.dartmouth.edu/doc/solaris-forte/manuals/fortran/prog_guide/11_cfort.html)
37for example), the main difficulty is interoperability of ["assumed
38size
39arrays"](https://thinkingeek.com/2017/01/14/gfortran-array-descriptor/).
40
41The approach taken here is a bit of a hack and should be replaced with
42a more portable way of doing things [when it becomes
43available](https://gcc.gnu.org/onlinedocs/gfortran/Further-Interoperability-of-Fortran-with-C.html#Further-Interoperability-of-Fortran-with-C).
44
45For the moment, it relies on the assumption that the "shaped array
46descriptor" works in a similar manner to that of
47[gfortran](https://gcc.gnu.org/onlinedocs/gfortran). These assumptions are:
48
491. The first element of the array descriptor is a pointer to the data
50stored in the array.
51
522. The size occupied by the array descriptor is smaller than or equal
53to the size of the data structure below (i.e. `sizeof(cvmix_1d)`).
54
553. The only types used by CVMix are `integer`, `cvmix_r8`,
56`character`, `logical` and derived types composed of these.
57
584. Fortran derived types never use more memory than the corresponding
59C structure.
60
61If any of these assumptions is violated, the most likely result will
62be a low-level memory fault (i.e. segmentation fault, stack smashing
63etc.). */
64
65#pragma autolink -L$BASILISK/cvmix -lcvmixc -lgfortran
66
67/**
68We define a data structure describing the array descriptor of gfortran
69as documented
70[here](https://github.com/gcc-mirror/gcc/blob/master/gcc/fortran/trans-types.c#L1259). Note
71that the goal is only to define a `cvmix_1d` structure which occupies
72the right amount of memory so that conditions 2 and 4 above are
73verified. The actual members are never used, with the exception of
74`gfc_array_descriptor.a` which corresponds to assumption 1 above. */
75
76typedef int indexing;
77
79{
80 size_t elem_len;
82 signed char rank;
83 signed char type;
84 signed short attribute;
85};
86
93
95{
96 double * a;
100 int padding[2]; // this seems necessary!!!
101};
102
103typedef double cvmix_r8;
104typedef int logical;
105typedef int integer;
107typedef cvmix_1d cvmix_nd; // for n-rank arrays (not used)
108
109typedef struct {
110 double * a;
114 int padding[2]; // this seems necessary!!!
115} cvmix_2d;
116
118
119#define strlencheck(s) (s != NULL ? strlen(s) : 0)
120
121#include "kinds_and_types.h"
122#include "put_get.h"
123
124extern void cvmix_redirect_stdout_ (void);
125extern void allocate1d_ (const int * len, cvmix_1d * mem);
126extern void deallocate1d_ (cvmix_1d * mem);
127extern void allocate2d_ (const int * len1, const int * len2, cvmix_2d * mem);
128extern void deallocate2d_ (cvmix_2d * mem);
129extern void sizeofall_ (void);
130
132
134{
135 cvmix_1d a;
136 allocate1d_ (&len, &a);
137 return a;
138}
139
144
146{
147 fprintf (stderr, "a: %p offset: %d dtype: %ld %d %d %d %d dim: %d %d %d\n",
148 (void *) a->a, a->offset, a->dtype.elem_len,
149 a->dtype.version, a->dtype.rank, a->dtype.type,
150 a->dtype.attribute,
151 a->dim[0].stride, a->dim[0].lbound, a->dim[0].ubound);
152}
153
155{
156 cvmix_2d a;
157 allocate2d_ (&len1, &len2, &a);
158 return a;
159}
160
165
166#define cvmix_deallocate(a) cvmix_deallocate_(a)
167#define cvmix_2d(c,len1,i,j) (c).a[(i) + (j)*(len1)]
const vector a
Definition all-mach.h:59
int x
Definition common.h:76
void cvmix_deallocate_(cvmix_data_type *CVmix_vars)
cvmix_1d cvmix_allocate_1d(int len)
Definition cvmix.h:133
cvmix_2d cvmix_allocate_2d(int len1, int len2)
Definition cvmix.h:154
void allocate2d_(const int *len1, const int *len2, cvmix_2d *mem)
int integer
Definition cvmix.h:105
void allocate1d_(const int *len, cvmix_1d *mem)
void deallocate2d_(cvmix_2d *mem)
int logical
Definition cvmix.h:104
cvmix_1d cvmix_nd
Definition cvmix.h:107
void cvmix_1d_print(cvmix_1d *a)
Definition cvmix.h:145
int indexing
We define a data structure describing the array descriptor of gfortran as documented here.
Definition cvmix.h:76
void cvmix_redirect_stdout_(void)
void deallocate1d_(cvmix_1d *mem)
cvmix_r8 cvmix_one
Definition cvmix.h:117
double cvmix_r8
Definition cvmix.h:103
cvmix_r8 cvmix_zero
Definition cvmix.h:117
void cvmix_deallocate_1d(cvmix_1d a)
Definition cvmix.h:140
void sizeofall_(void)
void cvmix_deallocate_2d(cvmix_2d a)
Definition cvmix.h:161
#define cvmix_2d(c, len1, i, j)
Definition cvmix.h:167
double * a
Definition cvmix.h:110
indexing offset
Definition cvmix.h:111
indexing lbound
Definition cvmix.h:90
indexing ubound
Definition cvmix.h:91
indexing stride
Definition cvmix.h:89
signed char type
Definition cvmix.h:83
size_t elem_len
Definition cvmix.h:80
signed char rank
Definition cvmix.h:82
signed short attribute
Definition cvmix.h:84
int version
Definition cvmix.h:81
indexing offset
Definition cvmix.h:97
struct dtype_type dtype
Definition cvmix.h:98
struct descriptor_dimension dim[1]
Definition cvmix.h:99