File: pysilo.cpp

package info (click to toggle)
silo-llnl 4.9.1-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 26,260 kB
  • ctags: 7,688
  • sloc: ansic: 81,649; sh: 18,657; cpp: 3,314; makefile: 1,060; fortran: 863; pascal: 476; xml: 109; python: 102; csh: 74; perl: 54
file content (325 lines) | stat: -rw-r--r-- 10,219 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright (c) 1994 - 2010, Lawrence Livermore National Security, LLC.
// LLNL-CODE-425250.
// All rights reserved.
// 
// This file is part of Silo. For details, see silo.llnl.gov.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 
//    * Redistributions of source code must retain the above copyright
//      notice, this list of conditions and the disclaimer below.
//    * Redistributions in binary form must reproduce the above copyright
//      notice, this list of conditions and the disclaimer (as noted
//      below) in the documentation and/or other materials provided with
//      the distribution.
//    * Neither the name of the LLNS/LLNL nor the names of its
//      contributors may be used to endorse or promote products derived
//      from this software without specific prior written permission.
// 
// THIS SOFTWARE  IS PROVIDED BY  THE COPYRIGHT HOLDERS  AND CONTRIBUTORS
// "AS  IS" AND  ANY EXPRESS  OR IMPLIED  WARRANTIES, INCLUDING,  BUT NOT
// LIMITED TO, THE IMPLIED  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A  PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN  NO  EVENT SHALL  LAWRENCE
// LIVERMORE  NATIONAL SECURITY, LLC,  THE U.S.  DEPARTMENT OF  ENERGY OR
// CONTRIBUTORS BE LIABLE FOR  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR  CONSEQUENTIAL DAMAGES  (INCLUDING, BUT NOT  LIMITED TO,
// PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS  OF USE,  DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER  IN CONTRACT, STRICT LIABILITY,  OR TORT (INCLUDING
// NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT  OF THE USE  OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// 
// This work was produced at Lawrence Livermore National Laboratory under
// Contract  No.   DE-AC52-07NA27344 with  the  DOE.  Neither the  United
// States Government  nor Lawrence  Livermore National Security,  LLC nor
// any of  their employees,  makes any warranty,  express or  implied, or
// assumes   any   liability   or   responsibility  for   the   accuracy,
// completeness, or usefulness of any information, apparatus, product, or
// process  disclosed, or  represents  that its  use  would not  infringe
// privately-owned   rights.  Any  reference   herein  to   any  specific
// commercial products,  process, or  services by trade  name, trademark,
// manufacturer or otherwise does not necessarily constitute or imply its
// endorsement,  recommendation,   or  favoring  by   the  United  States
// Government or Lawrence Livermore National Security, LLC. The views and
// opinions  of authors  expressed  herein do  not  necessarily state  or
// reflect those  of the United  States Government or  Lawrence Livermore
// National  Security, LLC,  and shall  not  be used  for advertising  or
// product endorsement purposes.

#include <Python.h>
#include <silo.h>
#include "pydbfile.h"

#include <vector>
#include <string>
#include <iostream>
using namespace std;

static PyObject             *SiloError;
static PyObject             *siloModule = 0;

//
// Method table
//

std::vector<PyMethodDef> SiloMethods;


// ****************************************************************************
// Function: SiloErrorFunc
//
// Purpose: 
//   This function sets the Python error string if we're allowing Python
//   exceptions to be thrown from the Silo module.
//
// Arguments:
//   errString : The error string that is "thrown".
//
// Programmer: Brad Whitlock
// Creation:   Mon Sep 17 11:44:43 PDT 2001
//
// Modifications:
//   
// ****************************************************************************

void
SiloErrorFunc(const char *errString)
{
    PyErr_SetString(SiloError, errString);
}

// ****************************************************************************
// Function: AddMethod
//
// Purpose:
//   This function adds a method to the Silo module's Python method table.
//
// Arguments:
//   methodName : The name of the method.
//   cb         : The Python callback function.
//   doc        : The documentation string for the method.
//
// Programmer: Brad Whitlock
// Creation:   Tue Sep 4 15:36:47 PST 2001
//
// Modifications:
//   
// ****************************************************************************
static void
AddMethod(const char *methodName, PyObject *(cb)(PyObject *, PyObject *),
          const char *doc = NULL)
{
    PyMethodDef newMethod;
    newMethod.ml_name = (char *)methodName;
    newMethod.ml_meth = cb;
    newMethod.ml_flags = METH_VARARGS;
    newMethod.ml_doc = (char *)doc;
    SiloMethods.push_back(newMethod);
}


// ****************************************************************************
//  Method:  silo_Open
//
//  Purpose:
//    Encapsulate DBOpen
//
//  Python Arguments:
//    form 1: filename, mode
//    form 2: filename
//
//  Programmer:  Jeremy Meredith
//  Creation:    July 12, 2005
//
// ****************************************************************************
PyObject *silo_Open(PyObject *self, PyObject *args)
{
    char *filename;
    int mode;
    if (! PyArg_ParseTuple(args, "si", &filename, &mode))
    {
        mode = DB_READ;
        if (! PyArg_ParseTuple(args, "s", &filename))
        {
            PyErr_SetString(PyExc_TypeError,
                            "Open takes either 1 or 2 arguments");
            return NULL;
        }
    }

    PyErr_Clear();
    DBfile *db = DBOpen(filename, DB_UNKNOWN, mode);
    if (!db)
    {
        PyErr_SetString(PyExc_TypeError,
                        "File could not be opened");
        return NULL;
    }
    return DBfile_NEW(db);
}

// ****************************************************************************
//  Method:  silo_Create
//
//  Purpose:
//    Encapsulate DBCreate
//
//  Python Arguments:
//    form 1: filename, fileinfo, driver, mode
//    form 2: filename, fileinfo, driver
//    form 3: filename, fileinfo
//
//  Programmer:  Jeremy Meredith
//  Creation:    July 12, 2005
//
// ****************************************************************************
PyObject *silo_Create(PyObject *self, PyObject *args)
{
    char *filename;
    char *info;
    int driver;
    int mode;
    if (!PyArg_ParseTuple(args, "ssii", &filename, &info, &driver, &mode))
    {
        mode = DB_CLOBBER;
        if (!PyArg_ParseTuple(args, "ssi", &filename, &info, &driver))
        {
            driver = DB_PDB;
            if (!PyArg_ParseTuple(args, "ss", &filename, &info))
            {
                PyErr_SetString(PyExc_TypeError,
                                "Create takes 2, 3, or 4 arguments");
                return NULL;
            }
        }
    }

    PyErr_Clear();
    DBfile *db = DBCreate(filename, mode, DB_LOCAL, info, driver);
    if (!db)
    {
        PyErr_SetString(PyExc_TypeError,
                        "File creation failed");
        return NULL;
    }
    DBSetAllowOverwrites(1);
    return DBfile_NEW(db);
}

// ****************************************************************************
//  Method:  initSilo
//

//    Called by python to initialize the Silo module.
//
//  Arguments:
//    none
//
//  Programmer:  Jeremy Meredith
//  Creation:    July 12, 2005
//
//  Modifications:
//    Mark C. Miller, Tue Nov 13 10:56:57 PST 2012
//    Added a slew of constants so calllers can properly examine dict
//    contents returned by GetVarInfo method.
//
// ****************************************************************************
#define ADD_CONSTANT(C)  PyDict_SetItemString(d, #C, PyInt_FromLong(C))
extern "C" void initSilo()
{
    AddMethod("Open", silo_Open,
              "Usage: Open(filename [, DB_READ|DB_APPEND]])");
    AddMethod("Create", silo_Create,
              "Usage: Create(filename , info [, DB_PDB|DB_HDF5 [, DB_CLOBBER|DB_NOCLOBBER]])");
    AddMethod(NULL, NULL);


    siloModule = Py_InitModule("Silo", &SiloMethods[0]);

    PyObject *d;
    d = PyModule_GetDict(siloModule);
    SiloError = PyErr_NewException("Silo.SiloException", NULL, NULL);
    PyDict_SetItemString(d, "SiloException", SiloError);

    // File Drivers
    ADD_CONSTANT(DB_PDB);
    ADD_CONSTANT(DB_HDF5);
    ADD_CONSTANT(DB_HDF5_SEC2);
    ADD_CONSTANT(DB_HDF5_STDIO);
    ADD_CONSTANT(DB_HDF5_CORE);
    ADD_CONSTANT(DB_HDF5_LOG);
    ADD_CONSTANT(DB_HDF5_SPLIT);
    ADD_CONSTANT(DB_HDF5_DIRECT);
    ADD_CONSTANT(DB_HDF5_FAMILY);
    ADD_CONSTANT(DB_HDF5_MPIO);
    ADD_CONSTANT(DB_HDF5_MPIOP);
    ADD_CONSTANT(DB_HDF5_MPIP);
    ADD_CONSTANT(DB_HDF5_SILO);

    // File flags
    ADD_CONSTANT(DB_CLOBBER);
    ADD_CONSTANT(DB_NOCLOBBER);
    ADD_CONSTANT(DB_READ);
    ADD_CONSTANT(DB_APPEND);

    // Coordinate type flags
    ADD_CONSTANT(DB_COLLINEAR);
    ADD_CONSTANT(DB_NONCOLLINEAR);
    ADD_CONSTANT(DB_QUAD_RECT);
    ADD_CONSTANT(DB_QUAD_CURV);

    // Centering
    ADD_CONSTANT(DB_NOTCENT);
    ADD_CONSTANT(DB_NODECENT);
    ADD_CONSTANT(DB_ZONECENT);
    ADD_CONSTANT(DB_FACECENT);
    ADD_CONSTANT(DB_BNDCENT);
    ADD_CONSTANT(DB_EDGECENT);
    ADD_CONSTANT(DB_BLOCKCENT);

    // Major order
    ADD_CONSTANT(DB_ROWMAJOR);
    ADD_CONSTANT(DB_COLMAJOR);

    // Coordinate system
    ADD_CONSTANT(DB_CARTESIAN);
    ADD_CONSTANT(DB_CYLINDRICAL);
    ADD_CONSTANT(DB_SPHERICAL);
    ADD_CONSTANT(DB_NUMERICAL);
    ADD_CONSTANT(DB_OTHER);

    // Planar
    ADD_CONSTANT(DB_AREA);
    ADD_CONSTANT(DB_VOLUME);

    // Facetype
    ADD_CONSTANT(DB_RECTILINEAR);
    ADD_CONSTANT(DB_CURVILINEAR);

    // Datatype
    ADD_CONSTANT(DB_INT);
    ADD_CONSTANT(DB_SHORT);
    ADD_CONSTANT(DB_LONG);
    ADD_CONSTANT(DB_LONG_LONG);
    ADD_CONSTANT(DB_FLOAT);
    ADD_CONSTANT(DB_DOUBLE);
    ADD_CONSTANT(DB_CHAR);
    ADD_CONSTANT(DB_NOTYPE);

    ADD_CONSTANT(DB_ON);
    ADD_CONSTANT(DB_OFF);

    ADD_CONSTANT(DB_ABUTTING);
    ADD_CONSTANT(DB_FLOATING);

    ADD_CONSTANT(DB_VARTYPE_SCALAR);
    ADD_CONSTANT(DB_VARTYPE_VECTOR);
    ADD_CONSTANT(DB_VARTYPE_TENSOR);
    ADD_CONSTANT(DB_VARTYPE_SYMTENSOR);
    ADD_CONSTANT(DB_VARTYPE_ARRAY);
    ADD_CONSTANT(DB_VARTYPE_MATERIAL);
    ADD_CONSTANT(DB_VARTYPE_SPECIES);
    ADD_CONSTANT(DB_VARTYPE_LABEL);
}