[petsc-users] Back to struct in Fortran to represent field with dof > 1

Barry Smith bsmith at mcs.anl.gov
Thu Sep 26 09:09:48 CDT 2013


   Christophe,

     Despite my LinkIn Endorsement for expertise in Fortran :-) I cannot pretend to be an expert in FortranXX but I have cooked up an example demonstrating accessing the Vec entries as if they are in an array of derived types. I've attached the example code; there needs to be a small C stub that defines the functions for your specific derived type name. 
Note that it will only work I think if your N is a compile time constant.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: ex1.c
Type: application/octet-stream
Size: 1378 bytes
Desc: not available
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20130926/f29812ad/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ex1f90.F
Type: application/octet-stream
Size: 7274 bytes
Desc: not available
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20130926/f29812ad/attachment-0001.obj>
-------------- next part --------------


    It worked with 
~/Downloads$ gfortran --version
GNU Fortran (GCC) 4.8.1 20130404 (prerelease)


    I do not understand exactly why it works since it uses F90Array1dCreate(fa,PETSC_SCALAR,1,len,ptr PETSC_F90_2PTR_PARAM(ptrd)); which has a single PETSC_SCALAR as a building block but ? I hope it works for you. If it doesn't, let us know the compiler you are using and we may be able to get it working for that compiler.

   Barry



On Sep 26, 2013, at 4:41 AM, Christophe Ortiz <christophe.ortiz at ciemat.es> wrote:

> Hi all,
> 
> Me again !
> 
> I have read in previous posts that it is hard in Fortran to declare something similar to a typedef struct in C to manage a multicomponent problem. 
> 
> Is it still the case ? Has the problem been solved ?
> 
> I am asking because my plan is to implement a multicomponent problem (in 1D), with many components that will be organized in arrays of two dimensions. In C I could define
> 
> typedef struct{
> PetscScalar U;
> PetscScalar V;
> PetscScalar A[N][N];
> } Field;
> 
> and then I could calculate the residual function with
> 
> F[i].U = ...
> F[i].V = ...
> F[i].A[k][n] = ...
> 
> which is quite convenient.
> 
> If in Fortran it is not possible to use a struct as in C, I am afraid I'll have to deal with
> 
> F(jdof,i) = ...
> 
> where I will have only jdof to address U, V and A[ ][ ], which can be difficult and not very convenient I guess. Before I move all my code to C, does anyone have an alternative idea to manage this multi(many)component problem in Fortran ?
> 
> Many thanks in advance for your help and suggestion !
> 
> Christophe



More information about the petsc-users mailing list