f77test.cc 4.47 KB
Newer Older
thomas.forbriger's avatar
thomas.forbriger committed
1
2
3
4
5
/*! \file f77test.cc
 * \brief test interfacing Fortran code (implementation)
 * 
 * ----------------------------------------------------------------------------
 * 
6
 * $Id: f77test.cc,v 1.7 2002-12-29 23:02:16 forbrig Exp $
thomas.forbriger's avatar
thomas.forbriger committed
7
8
9
10
 * \author Thomas Forbriger
 * \date 22/12/2002
 * 
 * test interfacing Fortran code (implementation)
11
12
 *
 * \sa \ref page_fortran
thomas.forbriger's avatar
thomas.forbriger committed
13
14
15
16
17
 * 
 * Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt) 
 * 
 * REVISIONS and CHANGES 
 *  - 22/12/2002   V1.0   Thomas Forbriger
18
19
20
 *  - 29/12/2002   V1.1   (thof)
 *                        - new uses new function aff::subarray
 *                        - new uses new function aff::slice
thomas.forbriger's avatar
thomas.forbriger committed
21
22
23
24
 * 
 * ============================================================================
 */

25
26
27
28
29
/*! \example tests/f77test.cc
 *
 * Passing arrays to Fortran 77 code and retrieving array structures
 * from Fortran 77 common blocks.
 *
thomas.forbriger's avatar
thomas.forbriger committed
30
31
32
33
34
35
36
37
 * This test program gives an example of the usage of the following classes,
 * functions, and preprocessor macros:
 *   - aff::FortranArray
 *   - aff::FortranShape
 *   - aff::Array
 *   - #DUMP
 *   - #CODE
 *
38
 * \sa tests/f77test.cc
39
 * \sa \ref page_fortran
40
41
42
 */

#define AFF_F77TEST_CC_VERSION \
43
  "AFF_F77TEST_CC   V1.1"
44
#define AFF_F77TEST_CC_CVSID \
45
  "$Id: f77test.cc,v 1.7 2002-12-29 23:02:16 forbrig Exp $"
46
47
48
49
50

#include <aff/array.h>
#include <aff/fortranshape.h>
#include <aff/dump.h>
#include <aff/shaper.h>
51
52
53
#include <aff/subarray.h>
#include <aff/slice.h>
#include "f77proto.h"
54
55
56
57

using std::cout;
using std::endl;
using namespace aff;
thomas.forbriger's avatar
thomas.forbriger committed
58

59
/*----------------------------------------------------------------------*/
thomas.forbriger's avatar
thomas.forbriger committed
60

61
62
63
64
65
66
67
//! print headline
void section(const char* s, const char l='-')
{
  cout << endl << s << endl;
  const char* p=s;
  while (*p) { cout << l; ++p; }
  cout << endl;
thomas.forbriger's avatar
thomas.forbriger committed
68
69
}

70
71
/*======================================================================*/

72
73
74
75
/*! \brief test array interface to Fortran 77
 *
 * \sa \ref page_fortran
 */
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
int main()
{
  cout << AFF_F77TEST_CC_VERSION << endl;
  cout << AFF_F77TEST_CC_CVSID << endl;

  // First we test the shape class that should support passing arrays to
  // Fortran 77 functions
  section("FortranShape:", '=');
  {
    section("Full Layout");
    // we create a test array with known Fortran layout
    CODE(Strided strided(Shaper(1,10,20)(1,5,10)(1,30)(20)));
    // dump this shape
    DUMP(strided);
    // create Fortran shape from this (should be identical to known)
    CODE(aff::util::FortranShape fs1(strided));
    // and dump this
    DUMP(fs1.first());
    DUMP(fs1.last());
    DUMP(fs1.dimlast());
    cout << "fs1.offset(): " << fs1.offset() << endl;

    section("Sliced Subshape");
    // now take shape of a subarray
    CODE(Strided subshape(strided));
    CODE(subshape.shrink(0,2).shrink(1,3,5).shrink(3,10,20));
    CODE(subshape.collapse(2,15));
    // create Fortran shape from this
    CODE(aff::util::FortranShape fs2(subshape));
    // and dump this
    DUMP(fs2.first());
    DUMP(fs2.last());
    DUMP(fs2.dimlast());
    cout << "fs2.offset(): " << fs2.offset() << endl;
  }
111
112
113
114
115

  /*----------------------------------------------------------------------*/

  section("Pass array to Fortran via subroutine arguments:", '=');
  {
116
    // create an array and fill it
117
118
    CODE(Array<int> A(Shaper(-3,3)(9)(-1,1)));
    CODE(A=-55);
119
    // create a subarray view and fill this through Fortran
120
    CODE(Array<int> B=subarray(A)(-2,2)(3,7)(0));
121
    CODE(f77interface::fill(B));
122
    // dump the result
123
    CODE(dump_array(A));
124
    // do it again for a slice
125
    CODE(f77interface::fill(slice(A)()(2)));
126
127
    CODE(dump_array(A));
  }
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

  /*----------------------------------------------------------------------*/

  section("Access to common block:", '=');
  {
    // prepare to vectors to pass to fillarray
    CODE(Array<float> v1(5));
    CODE(Array<float> v2(3));
    CODE(for(int i=v1.f(0); i<=v1.l(0); i++) { v1(i)=2.*i; });
    CODE(for(int i=v2.f(0); i<=v2.l(0); i++) { v2(i)=.5*i; });
    // fill common block through Fortran 77 subroutine
    CODE(f77interface::fillarray(v1, v2));
    // get a view on the common block and dump it
    CODE(f77interface::Tzarray Z(f77interface::viewcommon()));
    CODE(dump_array(Z));
    // call Fortran subroutine sum and dump result
    CODE(dump_array(f77interface::sums()));
    CODE(typedef f77interface::Tzarray::Tvalue Tzvalue);
    // write directly to common block through a subarray
147
    CODE(subarray(Z)(2,4)=Tzvalue(-10.));
148
149
150
151
    // and dump the effect
    CODE(dump_array(Z));
    CODE(dump_array(f77interface::sums()));
  }
152
153
} // main

thomas.forbriger's avatar
thomas.forbriger committed
154
/* ----- END OF f77test.cc ----- */