Exemple #1
0
    def _write_dt_array_wrapper(self, t, element, dims, sizeof_fortran_t):
        """
        Write fortran get/set/len routines for a (1-dimensional) derived-type array.

        Parameters
        ----------
        t : `fortran.Type` node
            Derived-type node of the parse tree.

        el : `fortran.Element` node
            An element of a module which is derived-type array

        dims : `tuple` of `int`s
            The dimensions of the element

        sizeof_fortan_t : `int`
            The size, in bytes, of a pointer to a fortran derived type ??
        """
        if element.type.startswith('type') and len(
                ArrayDimensionConverter.split_dimensions(dims)) != 1:
            return

        self._write_array_getset_item(t, element, sizeof_fortran_t, 'get')
        self._write_array_getset_item(t, element, sizeof_fortran_t, 'set')
        self._write_array_len(t, element, sizeof_fortran_t)
Exemple #2
0
    def _write_dt_array_wrapper(self, t, element, dims,
                               sizeof_fortran_t):
        """
        Write fortran get/set/len routines for a (1-dimensional) derived-type array.

        Parameters
        ----------
        t : `fortran.Type` node
            Derived-type node of the parse tree.

        el : `fortran.Element` node
            An element of a module which is derived-type array

        dims : `tuple` of `int`s
            The dimensions of the element

        sizeof_fortan_t : `int`
            The size, in bytes, of a pointer to a fortran derived type ??
        """
        if element.type.startswith('type') and len(ArrayDimensionConverter.split_dimensions(dims)) != 1:
            return

        self._write_array_getset_item(t, element, sizeof_fortran_t, 'get')
        self._write_array_getset_item(t, element, sizeof_fortran_t, 'set')
        self._write_array_len(t, element, sizeof_fortran_t)
Exemple #3
0
    def write_dt_array_wrapper(self, node, el, dims):
        if el.type.startswith('type') and len(
                ArrayDimensionConverter.split_dimensions(dims)) != 1:
            return

        func_name = 'init_array_%s' % el.name
        node.dt_array_initialisers.append(func_name)
        cls_name = normalise_class_name(ft.strip_type(el.type),
                                        self.class_names)
        mod_name = self.types[ft.strip_type(el.type)].mod_name
        cls_mod_name = self.py_mod_names.get(mod_name, mod_name)

        dct = dict(el_name=el.name,
                   func_name=func_name,
                   mod_name=node.name,
                   type_name=ft.strip_type(el.type).lower(),
                   f90_mod_name=self.f90_mod_name,
                   prefix=self.prefix,
                   self='self',
                   selfdot='self.',
                   parent='self',
                   doc=format_doc_string(el),
                   cls_name=cls_name,
                   cls_mod_name=cls_mod_name + '.')

        if isinstance(node, ft.Module):
            dct['parent'] = 'f90wrap.runtime.empty_type'
            if self.make_package:
                dct['selfdot'] = ''
                dct['self'] = ''
        if self.make_package:
            dct['cls_mod_name'] = ''
            if cls_mod_name != self.current_module:
                self.imports.add(
                    (self.py_mod_name + '.' + cls_mod_name, cls_name))

        self.write('def %(func_name)s(%(self)s):' % dct)
        self.indent()
        if isinstance(node, ft.Module) and self.make_package:
            self.write('global %(el_name)s' % dct)
        self.write(
            '''%(selfdot)s%(el_name)s = f90wrap.runtime.FortranDerivedTypeArray(%(parent)s,
                                %(f90_mod_name)s.%(prefix)s%(mod_name)s__array_getitem__%(el_name)s,
                                %(f90_mod_name)s.%(prefix)s%(mod_name)s__array_setitem__%(el_name)s,
                                %(f90_mod_name)s.%(prefix)s%(mod_name)s__array_len__%(el_name)s,
                                %(doc)s, %(cls_mod_name)s%(cls_name)s)''' %
            dct)
        self.write('return %(selfdot)s%(el_name)s' % dct)
        self.dedent()
        self.write()
Exemple #4
0
    def write_dt_array_wrapper(self, node, el, dims):
        if el.type.startswith('type') and len(ArrayDimensionConverter.split_dimensions(dims)) != 1:
            return

        func_name = 'init_array_%s' % el.name
        node.dt_array_initialisers.append(func_name)
        cls_name = normalise_class_name(ft.strip_type(el.type), self.class_names)
        mod_name = self.types[ft.strip_type(el.type)].mod_name
        cls_mod_name = self.py_mod_names.get(mod_name, mod_name)

        dct = dict(el_name=el.name,
                   func_name=func_name,
                   mod_name=node.name,
                   type_name=ft.strip_type(el.type).lower(),
                   f90_mod_name=self.f90_mod_name,
                   prefix=self.prefix,
                   self='self',
                   selfdot='self.',
                   parent='self',
                   doc=format_doc_string(el),
                   cls_name=cls_name,
                   cls_mod_name=cls_mod_name + '.')

        if isinstance(node, ft.Module):
            dct['parent'] = 'f90wrap.runtime.empty_type'
            if self.make_package:
                dct['selfdot'] = ''
                dct['self'] = ''
        if self.make_package:
            dct['cls_mod_name'] = ''
            if cls_mod_name != self.current_module:
                self.imports.add((self.py_mod_name + '.' + cls_mod_name, cls_name))

        self.write('def %(func_name)s(%(self)s):' % dct)
        self.indent()
        if isinstance(node, ft.Module) and self.make_package:
            self.write('global %(el_name)s' % dct)
        self.write('''%(selfdot)s%(el_name)s = f90wrap.runtime.FortranDerivedTypeArray(%(parent)s,
                                %(f90_mod_name)s.%(prefix)s%(mod_name)s__array_getitem__%(el_name)s,
                                %(f90_mod_name)s.%(prefix)s%(mod_name)s__array_setitem__%(el_name)s,
                                %(f90_mod_name)s.%(prefix)s%(mod_name)s__array_len__%(el_name)s,
                                %(doc)s, %(cls_mod_name)s%(cls_name)s)''' % dct)
        self.write('return %(selfdot)s%(el_name)s' % dct)
        self.dedent()
        self.write()
Exemple #5
0
    def _write_sc_array_wrapper(self, t, el, dims, sizeof_fortran_t):
        """
        Write wrapper for arrays of intrinsic types

        Parameters
        ----------
        t : `fortran.Type` node
            Derived-type node of the parse tree.

        el : `fortran.Element` node
            An element of a module which is derived-type array

        dims : `tuple` of `int`s
            The dimensions of the element

        sizeof_fortan_t : `int`
            The size, in bytes, of a pointer to a fortran derived type ??

        """
        if isinstance(t, ft.Type):
            this = 'this, '
        else:
            this = 'dummy_this, '

        self.write('subroutine %s%s__array__%s(%snd, dtype, dshape, dloc)' %
                   (self.prefix, t.name, el.orig_name, this))
        self.indent()

        if isinstance(t, ft.Module):
            self.write_uses_lines(
                t, {t.name: ['%s_%s => %s' % (t.name, el.name, el.name)]})
        else:
            self.write_uses_lines(t)

        self.write('implicit none')
        if isinstance(t, ft.Type):
            self.write_type_lines(t.name)
            self.write('integer, intent(in) :: this(%d)' % sizeof_fortran_t)
            self.write('type(%s_ptr_type) :: this_ptr' % t.name)
        else:
            self.write('integer, intent(in) :: dummy_this(%d)' %
                       sizeof_fortran_t)

        self.write('integer, intent(out) :: nd')
        self.write('integer, intent(out) :: dtype')
        try:
            rank = len(ArrayDimensionConverter.split_dimensions(dims))
            if el.type.startswith('character'):
                rank += 1
        except ValueError:
            rank = 1
        self.write('integer, dimension(10), intent(out) :: dshape')
        self.write('integer*%d, intent(out) :: dloc' % np.dtype('O').itemsize)
        self.write()
        self.write('nd = %d' % rank)
        self.write('dtype = %s' %
                   ft.fortran_array_type(el.type, self.kind_map))
        if isinstance(t, ft.Type):
            self.write('this_ptr = transfer(this, this_ptr)')
            array_name = 'this_ptr%%p%%%s' % el.orig_name
        else:
            array_name = '%s_%s' % (t.name, el.orig_name)

        if 'allocatable' in el.attributes:
            self.write('if (allocated(%s)) then' % array_name)
            self.indent()
        if el.type.startswith('character'):
            first = ','.join(['1' for i in range(rank - 1)])
            self.write('dshape(1:%d) = (/len(%s(%s)), shape(%s)/)' %
                       (rank, array_name, first, array_name))
        else:
            self.write('dshape(1:%d) = shape(%s)' % (rank, array_name))
        self.write('dloc = loc(%s)' % array_name)
        if 'allocatable' in el.attributes:
            self.dedent()
            self.write('else')
            self.indent()
            self.write('dloc = 0')
            self.dedent()
            self.write('end if')

        self.dedent()
        self.write('end subroutine %s%s__array__%s' %
                   (self.prefix, t.name, el.orig_name))
        self.write()
Exemple #6
0
    def _write_sc_array_wrapper(self, t, el, dims, sizeof_fortran_t):
        """
        Write wrapper for arrays of intrinsic types

        Parameters
        ----------
        t : `fortran.Type` node
            Derived-type node of the parse tree.

        el : `fortran.Element` node
            An element of a module which is derived-type array

        dims : `tuple` of `int`s
            The dimensions of the element

        sizeof_fortan_t : `int`
            The size, in bytes, of a pointer to a fortran derived type ??

        """
        if isinstance(t, ft.Type):
            this = 'this, '
        else:
            this = 'dummy_this, '

        self.write('subroutine %s%s__array__%s(%snd, dtype, dshape, dloc)' % (self.prefix, t.name, el.name, this))
        self.indent()

        if isinstance(t, ft.Module):
            self.write_uses_lines(t, {t.name: ['%s_%s => %s' % (t.name, el.name, el.name)]})
        else:
            self.write_uses_lines(t)

        self.write('implicit none')
        if isinstance(t, ft.Type):
            self.write_type_lines(t.name)
            self.write('integer, intent(in) :: this(%d)' % sizeof_fortran_t)
            self.write('type(%s_ptr_type) :: this_ptr' % t.name)
        else:
            self.write('integer, intent(in) :: dummy_this(%d)' % sizeof_fortran_t)

        self.write('integer, intent(out) :: nd')
        self.write('integer, intent(out) :: dtype')
        try:
            rank = len(ArrayDimensionConverter.split_dimensions(dims))
            if el.type.startswith('character'):
                rank += 1
        except ValueError:
            rank = 1
        self.write('integer, dimension(10), intent(out) :: dshape')
        self.write('integer*%d, intent(out) :: dloc' % np.dtype('O').itemsize)
        self.write()
        self.write('nd = %d' % rank)
        self.write('dtype = %s' % ft.fortran_array_type(el.type, self.kind_map))
        if isinstance(t, ft.Type):
            self.write('this_ptr = transfer(this, this_ptr)')
            array_name = 'this_ptr%%p%%%s' % el.name
        else:
            array_name = '%s_%s' % (t.name, el.name)

        if 'allocatable' in el.attributes:
            self.write('if (allocated(%s)) then' % array_name)
            self.indent()
        if el.type.startswith('character'):
            first = ','.join(['1' for i in range(rank - 1)])
            self.write('dshape(1:%d) = (/len(%s(%s)), shape(%s)/)' % (rank, array_name, first, array_name))
        else:
            self.write('dshape(1:%d) = shape(%s)' % (rank, array_name))
        self.write('dloc = loc(%s)' % array_name)
        if 'allocatable' in el.attributes:
            self.dedent()
            self.write('else')
            self.indent()
            self.write('dloc = 0')
            self.dedent()
            self.write('end if')

        self.dedent()
        self.write('end subroutine %s%s__array__%s' % (self.prefix, t.name, el.name))
        self.write()