TF++, Miscellaneous classes and modules in C++:
blitztest.cc
Go to the documentation of this file.
1 
34 #define BLITZTEST_VERSION \
35  "BLITZTEST V1.0 test all blitz support modules"
36 
37 #include <iostream>
38 #include <blitz/array.h>
39 #include <tfxx/error.h>
40 #include <tfxx/commandline.h>
41 #include <tfxx/blitzutil.h>
42 #include <tfxx/blitzfortranio.h>
43 
44 using std::cout;
45 using std::cerr;
46 using std::endl;
47 
48 
57 int main(int iargc, char* argv[])
58 {
59 
60  // define usage information
61  char usage_text[]=
62  {
64  "usage: blitztest [-v] [-if file] [-of file]" "\n"
65  " or: blitztest --help|-h" "\n"
66  };
67 
68  // define full help text
69  char help_text[]=
70  {
71  "-v be verbose" "\n"
72  "-of file write output to \"file\"" "\n"
73  "-if file read input from \"file\"" "\n"
74  BLITZTEST_CVSID
75  };
76 
77  // define commandline options
78  using namespace tfxx::cmdline;
79  static Declare options[]=
80  {
81  // 0: print help
82  {"help",arg_no,"-"},
83  // 1: verbose mode
84  {"v",arg_no,"-"},
85  // 2: output file
86  {"of",arg_yes,"junk"},
87  // 3: input file
88  {"if",arg_yes,"junk"},
89  {NULL}
90  };
91 
92  // no arguments? print usage...
93  if (iargc<1)
94  {
95  cerr << usage_text << endl;
96  exit(0);
97  }
98 
99  // collect options from commandline
100  Commandline cmdline(iargc, argv, options);
101 
102  // help requested? print full help text...
103  if (cmdline.optset(0))
104  {
105  cerr << usage_text << endl;
106  cerr << help_text << endl;
107  exit(0);
108  }
109 
110  bool verbose=cmdline.optset(1);
111  std::string outfile=cmdline.string_arg(2);
112  std::string infile=cmdline.string_arg(3);
113 
114  if (verbose)
115  {
116  cout << BLITZTEST_VERSION << endl << endl
117  << "Creating arrays:" << endl;
118  }
119 
120  blitz::Array<float, 2> A(blitz::Range(3,6),blitz::Range(1,3),
121  blitz::fortranArray);
122  blitz::Array<float, 2> B(blitz::Range(3,6),blitz::Range(1,3));
123 
124  {
125  blitz::firstIndex i;
126  blitz::secondIndex j;
127  A=100+10*i+j;
128  B=200+10*i+j;
129  }
130 
131  cout << endl << "Fortran array: " << endl
132  << "==============" << endl << A << endl;
133  A.dumpStructureInformation();
134 
135  cout << endl << "C array: " << endl
136  << "========" << endl << B << endl;
137  B.dumpStructureInformation();
138 
139  if (verbose) cout << endl << "writing to " << outfile << "..." << endl;
140  {
141  std::ofstream os(outfile.c_str());
143  fo << A << B;
144  }
145 
146  blitz::Array<float, 2> C,D,E(blitz::fortranArray),F(blitz::fortranArray);
147 
148  if (verbose) cout << endl << "reading from " << infile
149  << " into C arrays..." << endl;
150  try
151  {
152  std::ifstream is(infile.c_str());
154  fi >> C >> D;
155  }
156  catch (tfxx::error::Exception)
157  {
158  cout.flush();
159  TFXX_abort("sorry...");
160  }
161 
162  if (verbose) cout << endl << "reading from " << infile
163  << " into Fortran arrays..." << endl;
164  {
165  std::ifstream is(infile.c_str());
167  fi >> E >> F;
168  }
169 
170  cout << endl << "C array from Fortran array:" << endl
171  << "===========================" << endl << C << endl;
172  C.dumpStructureInformation();
173 
174  cout << endl << "C array from C array:" << endl
175  << "=====================" << endl << D << endl;
176  D.dumpStructureInformation();
177 
178  cout << endl << "Fortran array from Fortran array:" << endl
179  << "=================================" << endl << E << endl;
180  E.dumpStructureInformation();
181 
182  cout << endl << "Fortran array from C array:" << endl
183  << "===========================" << endl << F << endl;
184  F.dumpStructureInformation();
185 
186  /*----------------------------------------------------------------------*/
187 
188  cout << endl << "Test shape copy function" << endl
189  << "========================" << endl;
190 
191  blitz::Array<int,3> G(blitz::Range(3,4),
192  blitz::Range(3,5),
193  blitz::Range(2,4),
194  blitz::fortranArray);
195  {
196  blitz::firstIndex i;
197  blitz::secondIndex j;
198  blitz::thirdIndex k;
199  G=i*100+j*10+k;
200  }
201 
202  cout << endl << "Fortran array for type int:" << endl
203  << "---------------------------" << endl << G << endl;
204  G.dumpStructureInformation();
205 
206  blitz::Array<std::complex<double>,3> H;
207  tfxx::blitzutil::setToSameDomain(H,G.domain());
208 
209  cout << endl << "Array of same domain and type complex<double>:" << endl
210  << "----------------------------------------------"
211  << endl << H << endl;
212  H.dumpStructureInformation();
213 
215  "setToSameDomain failed!");
216 }
217 
218 /* ----- END OF blitztest.cc ----- */
write FORTRAN binary data ,This class writes FORTRAN binary data. FORTRAN binary data is always writt...
Definition: fortranio.h:200
#define TFXX_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:175
bool sameDomain(const blitz::Array< T1, N1 > &a1, const blitz::Array< T2, N2 > &a2)
test for conforming domains
Definition: blitzutil.h:75
option requires an argument
Definition: commandline.h:102
bool optset(const int &iopt) const
true if option # iopt was set on commandline
Definition: commandline.h:213
#define BLITZTEST_VERSION
Definition: blitztest.cc:34
Namespace containing all components of module commandline. ,.
Definition: commandline.cc:41
void setToSameDomain(blitz::Array< T, N > &array, const blitz::RectDomain< N > &domain, const blitz::GeneralArrayStorage< N > &storage=blitz::fortranArray)
create a second array spanning the same domain
Definition: blitzutil.h:90
read FORTRAN binary data ,This class reads FORTRAN binary data. FORTRAN binary data is always written...
Definition: fortranio.h:137
Evaluates commandline by calling long_getopt. ,You may instantiate a Commandline object by passing th...
Definition: commandline.h:201
int main(int iargc, char *argv[])
test blitz binary I/O operators
Definition: blitztest.cc:57
struct to define options ,This struct is used to define a list of options. An example is: ...
Definition: commandline.h:136
option has no argument
Definition: commandline.h:100
Base class for exceptions.
Definition: error.h:78
std::string string_arg(const int &iopt) const
return argument of option # iopt as string value
Definition: commandline.h:216
#define TFXX_abort(M)
Abort and give a message.
Definition: error.h:183