/[escript]/trunk/escript/src/WrappedArray.cpp
ViewVC logotype

Contents of /trunk/escript/src/WrappedArray.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2677 - (show annotations)
Tue Sep 22 00:48:00 2009 UTC (10 years, 5 months ago) by jfenwick
File size: 3336 byte(s)
Removed stuff in user guide about needing to make table larger to allow
boundary points.
The table interpolation is now OMP parallel.


1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2009 by University of Queensland
5 * Earth Systems Science Computational Center (ESSCC)
6 * http://www.uq.edu.au/esscc
7 *
8 * Primary Business: Queensland, Australia
9 * Licensed under the Open Software License version 3.0
10 * http://www.opensource.org/licenses/osl-3.0.php
11 *
12 *******************************************************/
13
14 #include <boost/python/tuple.hpp>
15 #include "WrappedArray.h"
16 #include "DataException.h"
17
18 #include <iostream>
19
20 using namespace escript;
21 using namespace boost::python;
22
23 namespace
24 {
25
26 void checkFeatures(const boost::python::object& obj)
27 {
28 using namespace std;
29 int len=0;
30 boost::python::object o2;
31 try
32 {
33 len=extract<int>(obj.attr("__len__")());
34 }
35 catch (...)
36 {
37 PyErr_Clear();
38 throw DataException("Object passed to WrappedArray must support __len__");
39 }
40 try
41 {
42 o2=obj.attr("__getitem__");
43 }
44 catch (...)
45 {
46 PyErr_Clear();
47 throw DataException("Object passed to WrappedArray must support __getitem__");
48 }
49 }
50
51 void getObjShape(const boost::python::object& obj, DataTypes::ShapeType& s)
52 {
53 int len=0;
54 try
55 {
56 len=extract<int>(obj.attr("__len__")());
57 }
58 catch(...)
59 {
60 PyErr_Clear(); // tell python the error isn't there anymore
61 return;
62 }
63 if (len<1)
64 {
65 throw DataException("Array filter - no empty components in arrays please.");
66 }
67 s.push_back(len);
68
69 if (s.size()>ESCRIPT_MAX_DATA_RANK)
70 {
71 throw DataException("Array filter - Maximum rank exceeded in array");
72 }
73 getObjShape(obj[0],s);
74 }
75
76 }
77
78 WrappedArray::WrappedArray(const boost::python::object& obj_in)
79 :obj(obj_in)
80 {
81 dat=0;
82 // First we check for scalars
83 try
84 {
85 double v=extract<double>(obj_in);
86 m_scalar=v;
87 rank=0;
88 return;
89 }
90 catch (...)
91 { // so we clear the failure
92 PyErr_Clear();
93 }
94 try
95 {
96 double v=extract<double>(obj_in[make_tuple()]);
97 m_scalar=v;
98 rank=0;
99 return;
100 }
101 catch (...)
102 { // so we clear the failure
103 PyErr_Clear();
104 }
105
106
107 m_scalar=0;
108 checkFeatures(obj_in);
109 getObjShape(obj,shape);
110 rank=shape.size();
111 }
112
113 void WrappedArray::convertArray() const
114 {
115 if ((dat!=0) || (rank<=0) || (rank>4)) // checking illegal rank here to avoid memory issues later
116 { // yes the failure is silent here but not doing the copy
117 return; // will just cause an error to be raised later
118 }
119 int size=DataTypes::noValues(shape);
120 double* tdat=new double[size];
121 switch (rank)
122 {
123 case 1: for (int i=0;i<shape[0];i++)
124 {
125 tdat[i]=getElt(i);
126 }
127 break;
128 case 2: for (int i=0;i<shape[0];i++)
129 {
130 for (int j=0;j<shape[1];j++)
131 {
132 tdat[DataTypes::getRelIndex(shape,i,j)]=getElt(i,j);
133 }
134 }
135 break;
136 case 3: for (int i=0;i<shape[0];i++)
137 {
138 for (int j=0;j<shape[1];j++)
139 {
140 for (int k=0;k<shape[2];k++)
141 {
142 tdat[DataTypes::getRelIndex(shape,i,j,k)]=getElt(i,j,k);
143 }
144 }
145 }
146 break;
147 case 4: for (int i=0;i<shape[0];i++)
148 {
149 for (int j=0;j<shape[1];j++)
150 {
151 for (int k=0;k<shape[2];k++)
152 {
153 for (int m=0;m<shape[3];m++)
154 {
155 tdat[DataTypes::getRelIndex(shape,i,j,k,m)]=getElt(i,j,k,m);
156 }
157 }
158 }
159 }
160 break;
161 default:
162 ; // do nothing
163 // can't happen. We've already checked the bounds above
164 }
165 dat=tdat;
166 }
167
168 WrappedArray::~WrappedArray()
169 {
170 if (dat!=0)
171 {
172 delete dat;
173 }
174 }
175
176

  ViewVC Help
Powered by ViewVC 1.1.26