/[escript]/trunk/finley/src/CPPAdapter/MeshAdapter.cpp
ViewVC logotype

Diff of /trunk/finley/src/CPPAdapter/MeshAdapter.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2989 by gross, Thu Mar 18 01:45:55 2010 UTC revision 4437 by caltinay, Tue Jun 4 05:37:18 2013 UTC
# Line 1  Line 1 
1    
2  /*******************************************************  /*****************************************************************************
3  *  *
4  * Copyright (c) 2003-2010 by University of Queensland  * Copyright (c) 2003-2013 by University of Queensland
5  * Earth Systems Science Computational Center (ESSCC)  * http://www.uq.edu.au
 * http://www.uq.edu.au/esscc  
6  *  *
7  * Primary Business: Queensland, Australia  * Primary Business: Queensland, Australia
8  * Licensed under the Open Software License version 3.0  * Licensed under the Open Software License version 3.0
9  * http://www.opensource.org/licenses/osl-3.0.php  * http://www.opensource.org/licenses/osl-3.0.php
10  *  *
11  *******************************************************/  * Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12    * Development since 2012 by School of Earth Sciences
13    *
14    *****************************************************************************/
15    
16    #include <pasowrap/PasoException.h>
17    #include <pasowrap/TransportProblemAdapter.h>
18  #include "MeshAdapter.h"  #include "MeshAdapter.h"
19  #include "escript/Data.h"  #include "escript/Data.h"
20  #include "escript/DataFactory.h"  #include "escript/DataFactory.h"
21    #include "esysUtils/blocktimer.h"
22    
23    #include <boost/python/import.hpp>
24  #ifdef USE_NETCDF  #ifdef USE_NETCDF
25  #include <netcdfcpp.h>  #include <netcdfcpp.h>
26  #endif  #endif
 #ifdef PASO_MPI  
 #include <mpi.h>  
 #include "paso/Paso_MPI.h"  
 #endif  
 extern "C" {  
 #include "esysUtils/blocktimer.h"  
 }  
27    
28  using namespace std;  using namespace std;
29  using namespace escript;  using namespace paso;
30    
31  namespace finley {  namespace finley {
32    
 //  
33  // define the static constants  // define the static constants
34  MeshAdapter::FunctionSpaceNamesMapType MeshAdapter::m_functionSpaceTypeNames;  MeshAdapter::FunctionSpaceNamesMapType MeshAdapter::m_functionSpaceTypeNames;
35  const int MeshAdapter::DegreesOfFreedom=FINLEY_DEGREES_OF_FREEDOM;  const int MeshAdapter::DegreesOfFreedom=FINLEY_DEGREES_OF_FREEDOM;
# Line 50  const int MeshAdapter::ReducedContactEle Line 48  const int MeshAdapter::ReducedContactEle
48    
49  MeshAdapter::MeshAdapter(Finley_Mesh* finleyMesh)  MeshAdapter::MeshAdapter(Finley_Mesh* finleyMesh)
50  {  {
51     setFunctionSpaceTypeNames();      setFunctionSpaceTypeNames();
52     //      // need to use a null_deleter as Finley_Mesh_free deletes the pointer
53     // need to use a null_deleter as Finley_Mesh_free deletes the pointer      // for us.
54     // for us.      m_finleyMesh.reset(finleyMesh, null_deleter());
    m_finleyMesh.reset(finleyMesh,null_deleter());  
55  }  }
56    
57  //  //
58  // The copy constructor should just increment the use count  // The copy constructor should just increment the use count
59  MeshAdapter::MeshAdapter(const MeshAdapter& in):  MeshAdapter::MeshAdapter(const MeshAdapter& in) :
60  m_finleyMesh(in.m_finleyMesh)      m_finleyMesh(in.m_finleyMesh)
61  {  {
62     setFunctionSpaceTypeNames();      setFunctionSpaceTypeNames();
63  }  }
64    
65  MeshAdapter::~MeshAdapter()  MeshAdapter::~MeshAdapter()
66  {  {
67     //      // I hope the case for the pointer being zero has been taken care of
68     // I hope the case for the pointer being zero has been taken care of.      if (m_finleyMesh.unique()) {
69     //  cout << "In MeshAdapter destructor." << endl;          Finley_Mesh_free(m_finleyMesh.get());
70     if (m_finleyMesh.unique()) {      }
       Finley_Mesh_free(m_finleyMesh.get());  
    }  
71  }  }
72    
73  int MeshAdapter::getMPISize() const  int MeshAdapter::getMPISize() const
74  {  {
75     return m_finleyMesh.get()->MPIInfo->size;      return m_finleyMesh.get()->MPIInfo->size;
76  }  }
77    
78  int MeshAdapter::getMPIRank() const  int MeshAdapter::getMPIRank() const
79  {  {
80     return m_finleyMesh.get()->MPIInfo->rank;      return m_finleyMesh.get()->MPIInfo->rank;
81  }  }
82    
83  void MeshAdapter::MPIBarrier() const  void MeshAdapter::MPIBarrier() const
84  {  {
85  #ifdef PASO_MPI  #ifdef ESYS_MPI
86     MPI_Barrier(m_finleyMesh.get()->MPIInfo->comm);      MPI_Barrier(m_finleyMesh.get()->MPIInfo->comm);
87  #endif  #endif
    return;  
88  }  }
89    
90  bool MeshAdapter::onMasterProcessor() const  bool MeshAdapter::onMasterProcessor() const
91  {  {
92     return m_finleyMesh.get()->MPIInfo->rank == 0;      return m_finleyMesh.get()->MPIInfo->rank == 0;
93  }  }
94    
95    #ifdef ESYS_MPI
96  #ifdef PASO_MPI  MPI_Comm
   MPI_Comm  
97  #else  #else
98    unsigned int  unsigned int
99  #endif  #endif
100  MeshAdapter::getMPIComm() const  MeshAdapter::getMPIComm() const
101  {  {
102  #ifdef PASO_MPI  #ifdef ESYS_MPI
103      return m_finleyMesh->MPIInfo->comm;      return m_finleyMesh->MPIInfo->comm;
104  #else  #else
105      return 0;      return 0;
106  #endif  #endif
107  }  }
108    
109    Finley_Mesh* MeshAdapter::getFinley_Mesh() const
110  Finley_Mesh* MeshAdapter::getFinley_Mesh() const {  {
111     return m_finleyMesh.get();      return m_finleyMesh.get();
112  }  }
113    
114  void MeshAdapter::write(const string& fileName) const  void MeshAdapter::write(const string& fileName) const
115  {  {
116     char *fName = (fileName.size()+1>0) ? TMPMEMALLOC(fileName.size()+1,char) : (char*)NULL;      Finley_Mesh_write(m_finleyMesh.get(), fileName.c_str());
117     strcpy(fName,fileName.c_str());      checkFinleyError();
    Finley_Mesh_write(m_finleyMesh.get(),fName);  
    checkFinleyError();  
    TMPMEMFREE(fName);  
118  }  }
119    
120  void MeshAdapter::Print_Mesh_Info(const bool full) const  void MeshAdapter::Print_Mesh_Info(const bool full) const
121  {  {
122     Finley_PrintMesh_Info(m_finleyMesh.get(), full);      Finley_PrintMesh_Info(m_finleyMesh.get(), full);
123  }  }
124    
125  void MeshAdapter::dump(const string& fileName) const  void MeshAdapter::dump(const string& fileName) const
126  {  {
127  #ifdef USE_NETCDF  #ifdef USE_NETCDF
128     const NcDim* ncdims[12] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};      const NcDim* ncdims[12] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
129     NcVar *ids;      NcVar *ids;
130     int *int_ptr;      int *int_ptr;
131     Finley_Mesh *mesh = m_finleyMesh.get();      Finley_Mesh *mesh = m_finleyMesh.get();
132     Finley_TagMap* tag_map;      int num_Tags = 0;
133     int num_Tags = 0;      int mpi_size                         = mesh->MPIInfo->size;
134     int mpi_size             = mesh->MPIInfo->size;      int mpi_rank                         = mesh->MPIInfo->rank;
135     int mpi_rank             = mesh->MPIInfo->rank;      int numDim                           = mesh->Nodes->numDim;
136     int numDim               = mesh->Nodes->numDim;      int numNodes                         = mesh->Nodes->numNodes;
137     int numNodes             = mesh->Nodes->numNodes;      int num_Elements                     = mesh->Elements->numElements;
138     int num_Elements         = mesh->Elements->numElements;      int num_FaceElements                 = mesh->FaceElements->numElements;
139     int num_FaceElements         = mesh->FaceElements->numElements;      int num_ContactElements              = mesh->ContactElements->numElements;
140     int num_ContactElements      = mesh->ContactElements->numElements;      int num_Points                       = mesh->Points->numElements;
141     int num_Points           = mesh->Points->numElements;      int num_Elements_numNodes            = mesh->Elements->numNodes;
142     int num_Elements_numNodes        = mesh->Elements->numNodes;      int num_FaceElements_numNodes        = mesh->FaceElements->numNodes;
143     int num_FaceElements_numNodes    = mesh->FaceElements->numNodes;      int num_ContactElements_numNodes     = mesh->ContactElements->numNodes;
144     int num_ContactElements_numNodes = mesh->ContactElements->numNodes;  #ifdef ESYS_MPI
145  #ifdef PASO_MPI      MPI_Status status;
    MPI_Status status;  
146  #endif  #endif
147    
148  /* Incoming token indicates it's my turn to write */  /* Incoming token indicates it's my turn to write */
149  #ifdef PASO_MPI  #ifdef ESYS_MPI
150     if (mpi_rank>0) MPI_Recv(&num_Tags, 0, MPI_INT, mpi_rank-1, 81800, mesh->MPIInfo->comm, &status);      if (mpi_rank>0) {
151            MPI_Recv(&num_Tags, 0, MPI_INT, mpi_rank-1, 81800, mesh->MPIInfo->comm, &status);
152        }
153  #endif  #endif
154    
155     char *newFileName = Paso_MPI_appendRankToFileName(fileName.c_str(),      char *newFileName = Esys_MPI_appendRankToFileName(fileName.c_str(),
156                                                       mpi_size, mpi_rank);                                                       mpi_size, mpi_rank);
157    
158     /* Figure out how much storage is required for tags */      // Figure out how much storage is required for tags
159     tag_map = mesh->TagMap;      num_Tags = mesh->tagMap.size();
160     num_Tags = 0;  
161     while (tag_map) {      // NetCDF error handler
162        num_Tags++;      NcError err(NcError::verbose_nonfatal);
163        tag_map=tag_map->next;      // Create the file.
164     }      NcFile dataFile(newFileName, NcFile::Replace);
165        string msgPrefix("Error in MeshAdapter::dump: NetCDF operation failed - ");
166     // NetCDF error handler      // check if writing was successful
167     NcError err(NcError::verbose_nonfatal);      if (!dataFile.is_valid())
168     // Create the file.          throw FinleyAdapterException(msgPrefix+"Open file for output");
169     NcFile dataFile(newFileName, NcFile::Replace);  
170     string msgPrefix("Error in MeshAdapter::dump: NetCDF operation failed - ");      // Define dimensions (num_Elements and dim_Elements are identical,
171     // check if writing was successful      // dim_Elements only appears if > 0)
172     if (!dataFile.is_valid())      if (! (ncdims[0] = dataFile.add_dim("numNodes", numNodes)) )
173        throw DataException(msgPrefix+"Open file for output");          throw FinleyAdapterException(msgPrefix+"add_dim(numNodes)");
174        if (! (ncdims[1] = dataFile.add_dim("numDim", numDim)) )
175     // Define dimensions (num_Elements and dim_Elements are identical,          throw FinleyAdapterException(msgPrefix+"add_dim(numDim)");
176     // dim_Elements only appears if > 0)      if (! (ncdims[2] = dataFile.add_dim("mpi_size_plus_1", mpi_size+1)) )
177     if (! (ncdims[0] = dataFile.add_dim("numNodes", numNodes)) )          throw FinleyAdapterException(msgPrefix+"add_dim(mpi_size)");
178        throw DataException(msgPrefix+"add_dim(numNodes)");      if (num_Elements>0)
179     if (! (ncdims[1] = dataFile.add_dim("numDim", numDim)) )          if (! (ncdims[3] = dataFile.add_dim("dim_Elements", num_Elements)) )
180        throw DataException(msgPrefix+"add_dim(numDim)");              throw FinleyAdapterException(msgPrefix+"add_dim(dim_Elements)");
181     if (! (ncdims[2] = dataFile.add_dim("mpi_size_plus_1", mpi_size+1)) )      if (num_FaceElements>0)
182        throw DataException(msgPrefix+"add_dim(mpi_size)");          if (! (ncdims[4] = dataFile.add_dim("dim_FaceElements", num_FaceElements)) )
183     if (num_Elements>0)              throw FinleyAdapterException(msgPrefix+"add_dim(dim_FaceElements)");
184        if (! (ncdims[3] = dataFile.add_dim("dim_Elements", num_Elements)) )      if (num_ContactElements>0)
185           throw DataException(msgPrefix+"add_dim(dim_Elements)");          if (! (ncdims[5] = dataFile.add_dim("dim_ContactElements", num_ContactElements)) )
186     if (num_FaceElements>0)              throw FinleyAdapterException(msgPrefix+"add_dim(dim_ContactElements)");
187        if (! (ncdims[4] = dataFile.add_dim("dim_FaceElements", num_FaceElements)) )      if (num_Points>0)
188           throw DataException(msgPrefix+"add_dim(dim_FaceElements)");          if (! (ncdims[6] = dataFile.add_dim("dim_Points", num_Points)) )
189     if (num_ContactElements>0)              throw FinleyAdapterException(msgPrefix+"add_dim(dim_Points)");
190        if (! (ncdims[5] = dataFile.add_dim("dim_ContactElements", num_ContactElements)) )      if (num_Elements>0)
191           throw DataException(msgPrefix+"add_dim(dim_ContactElements)");          if (! (ncdims[7] = dataFile.add_dim("dim_Elements_Nodes", num_Elements_numNodes)) )
192     if (num_Points>0)              throw FinleyAdapterException(msgPrefix+"add_dim(dim_Elements_Nodes)");
193        if (! (ncdims[6] = dataFile.add_dim("dim_Points", num_Points)) )      if (num_FaceElements>0)
194           throw DataException(msgPrefix+"add_dim(dim_Points)");          if (! (ncdims[8] = dataFile.add_dim("dim_FaceElements_numNodes", num_FaceElements_numNodes)) )
195     if (num_Elements>0)              throw FinleyAdapterException(msgPrefix+"add_dim(dim_FaceElements_numNodes)");
196        if (! (ncdims[7] = dataFile.add_dim("dim_Elements_Nodes", num_Elements_numNodes)) )      if (num_ContactElements>0)
197           throw DataException(msgPrefix+"add_dim(dim_Elements_Nodes)");          if (! (ncdims[9] = dataFile.add_dim("dim_ContactElements_numNodes", num_ContactElements_numNodes)) )
198     if (num_FaceElements>0)              throw FinleyAdapterException(msgPrefix+"add_dim(dim_ContactElements_numNodes)");
199        if (! (ncdims[8] = dataFile.add_dim("dim_FaceElements_numNodes", num_FaceElements_numNodes)) )      if (num_Tags>0)
200           throw DataException(msgPrefix+"add_dim(dim_FaceElements_numNodes)");          if (! (ncdims[10] = dataFile.add_dim("dim_Tags", num_Tags)) )
201     if (num_ContactElements>0)              throw FinleyAdapterException(msgPrefix+"add_dim(dim_Tags)");
202        if (! (ncdims[9] = dataFile.add_dim("dim_ContactElements_numNodes", num_ContactElements_numNodes)) )  
203           throw DataException(msgPrefix+"add_dim(dim_ContactElements_numNodes)");      // Attributes: MPI size, MPI rank, Name, order, reduced_order
204     if (num_Tags>0)      if (!dataFile.add_att("mpi_size", mpi_size) )
205        if (! (ncdims[10] = dataFile.add_dim("dim_Tags", num_Tags)) )          throw FinleyAdapterException(msgPrefix+"add_att(mpi_size)");
206           throw DataException(msgPrefix+"add_dim(dim_Tags)");      if (!dataFile.add_att("mpi_rank", mpi_rank) )
207            throw FinleyAdapterException(msgPrefix+"add_att(mpi_rank)");
208     // Attributes: MPI size, MPI rank, Name, order, reduced_order      if (!dataFile.add_att("Name",mesh->Name) )
209     if (!dataFile.add_att("mpi_size", mpi_size) )          throw FinleyAdapterException(msgPrefix+"add_att(Name)");
210        throw DataException(msgPrefix+"add_att(mpi_size)");      if (!dataFile.add_att("numDim",numDim) )
211     if (!dataFile.add_att("mpi_rank", mpi_rank) )          throw FinleyAdapterException(msgPrefix+"add_att(order)");
212        throw DataException(msgPrefix+"add_att(mpi_rank)");      if (!dataFile.add_att("order",mesh->integrationOrder) )
213     if (!dataFile.add_att("Name",mesh->Name) )          throw FinleyAdapterException(msgPrefix+"add_att(order)");
214        throw DataException(msgPrefix+"add_att(Name)");      if (!dataFile.add_att("reduced_order",mesh->reducedIntegrationOrder) )
215     if (!dataFile.add_att("numDim",numDim) )          throw FinleyAdapterException(msgPrefix+"add_att(reduced_order)");
216        throw DataException(msgPrefix+"add_att(order)");      if (!dataFile.add_att("numNodes",numNodes) )
217     if (!dataFile.add_att("order",mesh->integrationOrder) )          throw FinleyAdapterException(msgPrefix+"add_att(numNodes)");
218        throw DataException(msgPrefix+"add_att(order)");      if (!dataFile.add_att("num_Elements",num_Elements) )
219     if (!dataFile.add_att("reduced_order",mesh->reducedIntegrationOrder) )          throw FinleyAdapterException(msgPrefix+"add_att(num_Elements)");
220        throw DataException(msgPrefix+"add_att(reduced_order)");      if (!dataFile.add_att("num_FaceElements",num_FaceElements) )
221     if (!dataFile.add_att("numNodes",numNodes) )          throw FinleyAdapterException(msgPrefix+"add_att(num_FaceElements)");
222        throw DataException(msgPrefix+"add_att(numNodes)");      if (!dataFile.add_att("num_ContactElements",num_ContactElements) )
223     if (!dataFile.add_att("num_Elements",num_Elements) )          throw FinleyAdapterException(msgPrefix+"add_att(num_ContactElements)");
224        throw DataException(msgPrefix+"add_att(num_Elements)");      if (!dataFile.add_att("num_Points",num_Points) )
225     if (!dataFile.add_att("num_FaceElements",num_FaceElements) )          throw FinleyAdapterException(msgPrefix+"add_att(num_Points)");
226        throw DataException(msgPrefix+"add_att(num_FaceElements)");      if (!dataFile.add_att("num_Elements_numNodes",num_Elements_numNodes) )
227     if (!dataFile.add_att("num_ContactElements",num_ContactElements) )          throw FinleyAdapterException(msgPrefix+"add_att(num_Elements_numNodes)");
228        throw DataException(msgPrefix+"add_att(num_ContactElements)");      if (!dataFile.add_att("num_FaceElements_numNodes",num_FaceElements_numNodes) )
229     if (!dataFile.add_att("num_Points",num_Points) )          throw FinleyAdapterException(msgPrefix+"add_att(num_FaceElements_numNodes)");
230        throw DataException(msgPrefix+"add_att(num_Points)");      if (!dataFile.add_att("num_ContactElements_numNodes",num_ContactElements_numNodes) )
231     if (!dataFile.add_att("num_Elements_numNodes",num_Elements_numNodes) )          throw FinleyAdapterException(msgPrefix+"add_att(num_ContactElements_numNodes)");
232        throw DataException(msgPrefix+"add_att(num_Elements_numNodes)");      if (!dataFile.add_att("Elements_TypeId", mesh->Elements->referenceElementSet->referenceElement->Type->TypeId) )
233     if (!dataFile.add_att("num_FaceElements_numNodes",num_FaceElements_numNodes) )          throw FinleyAdapterException(msgPrefix+"add_att(Elements_TypeId)");
234        throw DataException(msgPrefix+"add_att(num_FaceElements_numNodes)");      if (!dataFile.add_att("FaceElements_TypeId", mesh->FaceElements->referenceElementSet->referenceElement->Type->TypeId) )
235     if (!dataFile.add_att("num_ContactElements_numNodes",num_ContactElements_numNodes) )          throw FinleyAdapterException(msgPrefix+"add_att(FaceElements_TypeId)");
236        throw DataException(msgPrefix+"add_att(num_ContactElements_numNodes)");      if (!dataFile.add_att("ContactElements_TypeId", mesh->ContactElements->referenceElementSet->referenceElement->Type->TypeId) )
237     if (!dataFile.add_att("Elements_TypeId", mesh->Elements->referenceElementSet->referenceElement->Type->TypeId) )          throw FinleyAdapterException(msgPrefix+"add_att(ContactElements_TypeId)");
238        throw DataException(msgPrefix+"add_att(Elements_TypeId)");      if (!dataFile.add_att("Points_TypeId", mesh->Points->referenceElementSet->referenceElement->Type->TypeId) )
239     if (!dataFile.add_att("FaceElements_TypeId", mesh->FaceElements->referenceElementSet->referenceElement->Type->TypeId) )          throw FinleyAdapterException(msgPrefix+"add_att(Points_TypeId)");
240        throw DataException(msgPrefix+"add_att(FaceElements_TypeId)");      if (!dataFile.add_att("num_Tags", num_Tags) )
241     if (!dataFile.add_att("ContactElements_TypeId", mesh->ContactElements->referenceElementSet->referenceElement->Type->TypeId) )          throw FinleyAdapterException(msgPrefix+"add_att(num_Tags)");
242        throw DataException(msgPrefix+"add_att(ContactElements_TypeId)");  
243     if (!dataFile.add_att("Points_TypeId", mesh->Points->referenceElementSet->referenceElement->Type->TypeId) )      // // // // // Nodes // // // // //
244        throw DataException(msgPrefix+"add_att(Points_TypeId)");  
245     if (!dataFile.add_att("num_Tags", num_Tags) )      // Nodes nodeDistribution
246        throw DataException(msgPrefix+"add_att(num_Tags)");      if (! (ids = dataFile.add_var("Nodes_NodeDistribution", ncInt, ncdims[2])) )
247            throw FinleyAdapterException(msgPrefix+"add_var(Nodes_NodeDistribution)");
248     // // // // // Nodes // // // // //      int_ptr = &mesh->Nodes->nodesDistribution->first_component[0];
249        if (! (ids->put(int_ptr, mpi_size+1)) )
250     // Nodes nodeDistribution          throw FinleyAdapterException(msgPrefix+"put(Nodes_NodeDistribution)");
251     if (! ( ids = dataFile.add_var("Nodes_NodeDistribution", ncInt, ncdims[2])) )  
252        throw DataException(msgPrefix+"add_var(Nodes_NodeDistribution)");      // Nodes degreesOfFreedomDistribution
253     int_ptr = &mesh->Nodes->nodesDistribution->first_component[0];      if (! ( ids = dataFile.add_var("Nodes_DofDistribution", ncInt, ncdims[2])) )
254     if (! (ids->put(int_ptr, mpi_size+1)) )          throw FinleyAdapterException(msgPrefix+"add_var(Nodes_DofDistribution)");
255        throw DataException(msgPrefix+"put(Nodes_NodeDistribution)");      int_ptr = &mesh->Nodes->degreesOfFreedomDistribution->first_component[0];
256        if (! (ids->put(int_ptr, mpi_size+1)) )
257     // Nodes degreesOfFreedomDistribution          throw FinleyAdapterException(msgPrefix+"put(Nodes_DofDistribution)");
258     if (! ( ids = dataFile.add_var("Nodes_DofDistribution", ncInt, ncdims[2])) )  
259        throw DataException(msgPrefix+"add_var(Nodes_DofDistribution)");      // Only write nodes if non-empty because NetCDF doesn't like empty arrays
260     int_ptr = &mesh->Nodes->degreesOfFreedomDistribution->first_component[0];      // (it treats them as NC_UNLIMITED)
261     if (! (ids->put(int_ptr, mpi_size+1)) )      if (numNodes>0) {
262        throw DataException(msgPrefix+"put(Nodes_DofDistribution)");          // Nodes Id
263            if (! ( ids = dataFile.add_var("Nodes_Id", ncInt, ncdims[0])) )
264     // Only write nodes if non-empty because NetCDF doesn't like empty arrays              throw FinleyAdapterException(msgPrefix+"add_var(Nodes_Id)");
265     // (it treats them as NC_UNLIMITED)          int_ptr = &mesh->Nodes->Id[0];
266     if (numNodes>0) {          if (! (ids->put(int_ptr, numNodes)) )
267                throw FinleyAdapterException(msgPrefix+"put(Nodes_Id)");
268        // Nodes Id  
269        if (! ( ids = dataFile.add_var("Nodes_Id", ncInt, ncdims[0])) )          // Nodes Tag
270           throw DataException(msgPrefix+"add_var(Nodes_Id)");          if (! ( ids = dataFile.add_var("Nodes_Tag", ncInt, ncdims[0])) )
271        int_ptr = &mesh->Nodes->Id[0];              throw FinleyAdapterException(msgPrefix+"add_var(Nodes_Tag)");
272        if (! (ids->put(int_ptr, numNodes)) )          int_ptr = &mesh->Nodes->Tag[0];
273           throw DataException(msgPrefix+"put(Nodes_Id)");          if (! (ids->put(int_ptr, numNodes)) )
274                throw FinleyAdapterException(msgPrefix+"put(Nodes_Tag)");
275        // Nodes Tag  
276        if (! ( ids = dataFile.add_var("Nodes_Tag", ncInt, ncdims[0])) )          // Nodes gDOF
277           throw DataException(msgPrefix+"add_var(Nodes_Tag)");          if (! ( ids = dataFile.add_var("Nodes_gDOF", ncInt, ncdims[0])) )
278        int_ptr = &mesh->Nodes->Tag[0];              throw FinleyAdapterException(msgPrefix+"add_var(Nodes_gDOF)");
279        if (! (ids->put(int_ptr, numNodes)) )          int_ptr = &mesh->Nodes->globalDegreesOfFreedom[0];
280           throw DataException(msgPrefix+"put(Nodes_Tag)");          if (! (ids->put(int_ptr, numNodes)) )
281                throw FinleyAdapterException(msgPrefix+"put(Nodes_gDOF)");
282        // Nodes gDOF  
283        if (! ( ids = dataFile.add_var("Nodes_gDOF", ncInt, ncdims[0])) )          // Nodes global node index
284           throw DataException(msgPrefix+"add_var(Nodes_gDOF)");          if (! ( ids = dataFile.add_var("Nodes_gNI", ncInt, ncdims[0])) )
285        int_ptr = &mesh->Nodes->globalDegreesOfFreedom[0];              throw FinleyAdapterException(msgPrefix+"add_var(Nodes_gNI)");
286        if (! (ids->put(int_ptr, numNodes)) )          int_ptr = &mesh->Nodes->globalNodesIndex[0];
287           throw DataException(msgPrefix+"put(Nodes_gDOF)");          if (! (ids->put(int_ptr, numNodes)) )
288                throw FinleyAdapterException(msgPrefix+"put(Nodes_gNI)");
289        // Nodes global node index  
290        if (! ( ids = dataFile.add_var("Nodes_gNI", ncInt, ncdims[0])) )          // Nodes grDof
291           throw DataException(msgPrefix+"add_var(Nodes_gNI)");          if (! ( ids = dataFile.add_var("Nodes_grDfI", ncInt, ncdims[0])) )
292        int_ptr = &mesh->Nodes->globalNodesIndex[0];              throw FinleyAdapterException(msgPrefix+"add_var(Nodes_grDfI)");
293        if (! (ids->put(int_ptr, numNodes)) )          int_ptr = &mesh->Nodes->globalReducedDOFIndex[0];
294           throw DataException(msgPrefix+"put(Nodes_gNI)");          if (! (ids->put(int_ptr, numNodes)) )
295                throw FinleyAdapterException(msgPrefix+"put(Nodes_grDfI)");
296        // Nodes grDof  
297        if (! ( ids = dataFile.add_var("Nodes_grDfI", ncInt, ncdims[0])) )          // Nodes grNI
298           throw DataException(msgPrefix+"add_var(Nodes_grDfI)");          if (! ( ids = dataFile.add_var("Nodes_grNI", ncInt, ncdims[0])) )
299        int_ptr = &mesh->Nodes->globalReducedDOFIndex[0];              throw FinleyAdapterException(msgPrefix+"add_var(Nodes_grNI)");
300        if (! (ids->put(int_ptr, numNodes)) )          int_ptr = &mesh->Nodes->globalReducedNodesIndex[0];
301           throw DataException(msgPrefix+"put(Nodes_grDfI)");          if (! (ids->put(int_ptr, numNodes)) )
302                throw FinleyAdapterException(msgPrefix+"put(Nodes_grNI)");
303        // Nodes grNI  
304        if (! ( ids = dataFile.add_var("Nodes_grNI", ncInt, ncdims[0])) )          // Nodes Coordinates
305           throw DataException(msgPrefix+"add_var(Nodes_grNI)");          if (! ( ids = dataFile.add_var("Nodes_Coordinates", ncDouble, ncdims[0], ncdims[1]) ) )
306        int_ptr = &mesh->Nodes->globalReducedNodesIndex[0];              throw FinleyAdapterException(msgPrefix+"add_var(Nodes_Coordinates)");
307        if (! (ids->put(int_ptr, numNodes)) )          if (! (ids->put(&(mesh->Nodes->Coordinates[INDEX2(0,0,numDim)]), numNodes, numDim)) )
308           throw DataException(msgPrefix+"put(Nodes_grNI)");              throw FinleyAdapterException(msgPrefix+"put(Nodes_Coordinates)");
309        }
310        // Nodes Coordinates  
311        if (! ( ids = dataFile.add_var("Nodes_Coordinates", ncDouble, ncdims[0], ncdims[1]) ) )      // // // // // Elements // // // // //
312           throw DataException(msgPrefix+"add_var(Nodes_Coordinates)");      if (num_Elements>0) {
313        if (! (ids->put(&(mesh->Nodes->Coordinates[INDEX2(0,0,numDim)]), numNodes, numDim)) )          // Elements_Id
314           throw DataException(msgPrefix+"put(Nodes_Coordinates)");          if (! ( ids = dataFile.add_var("Elements_Id", ncInt, ncdims[3])) )
315                throw FinleyAdapterException(msgPrefix+"add_var(Elements_Id)");
316     }          int_ptr = &mesh->Elements->Id[0];
317            if (! (ids->put(int_ptr, num_Elements)) )
318     // // // // // Elements // // // // //              throw FinleyAdapterException(msgPrefix+"put(Elements_Id)");
319    
320     if (num_Elements>0) {          // Elements_Tag
321            if (! ( ids = dataFile.add_var("Elements_Tag", ncInt, ncdims[3])) )
322        // Elements_Id              throw FinleyAdapterException(msgPrefix+"add_var(Elements_Tag)");
323        if (! ( ids = dataFile.add_var("Elements_Id", ncInt, ncdims[3])) )          int_ptr = &mesh->Elements->Tag[0];
324           throw DataException(msgPrefix+"add_var(Elements_Id)");          if (! (ids->put(int_ptr, num_Elements)) )
325        int_ptr = &mesh->Elements->Id[0];              throw FinleyAdapterException(msgPrefix+"put(Elements_Tag)");
326        if (! (ids->put(int_ptr, num_Elements)) )  
327           throw DataException(msgPrefix+"put(Elements_Id)");          // Elements_Owner
328            if (! ( ids = dataFile.add_var("Elements_Owner", ncInt, ncdims[3])) )
329        // Elements_Tag              throw FinleyAdapterException(msgPrefix+"add_var(Elements_Owner)");
330        if (! ( ids = dataFile.add_var("Elements_Tag", ncInt, ncdims[3])) )          int_ptr = &mesh->Elements->Owner[0];
331           throw DataException(msgPrefix+"add_var(Elements_Tag)");          if (! (ids->put(int_ptr, num_Elements)) )
332        int_ptr = &mesh->Elements->Tag[0];              throw FinleyAdapterException(msgPrefix+"put(Elements_Owner)");
333        if (! (ids->put(int_ptr, num_Elements)) )  
334           throw DataException(msgPrefix+"put(Elements_Tag)");          // Elements_Color
335            if (! ( ids = dataFile.add_var("Elements_Color", ncInt, ncdims[3])) )
336        // Elements_Owner              throw FinleyAdapterException(msgPrefix+"add_var(Elements_Color)");
337        if (! ( ids = dataFile.add_var("Elements_Owner", ncInt, ncdims[3])) )          int_ptr = &mesh->Elements->Color[0];
338           throw DataException(msgPrefix+"add_var(Elements_Owner)");          if (! (ids->put(int_ptr, num_Elements)) )
339        int_ptr = &mesh->Elements->Owner[0];              throw FinleyAdapterException(msgPrefix+"put(Elements_Color)");
340        if (! (ids->put(int_ptr, num_Elements)) )  
341           throw DataException(msgPrefix+"put(Elements_Owner)");          // Elements_Nodes
342            if (! ( ids = dataFile.add_var("Elements_Nodes", ncInt, ncdims[3], ncdims[7]) ) )
343        // Elements_Color              throw FinleyAdapterException(msgPrefix+"add_var(Elements_Nodes)");
344        if (! ( ids = dataFile.add_var("Elements_Color", ncInt, ncdims[3])) )          if (! (ids->put(&(mesh->Elements->Nodes[0]), num_Elements, num_Elements_numNodes)) )
345           throw DataException(msgPrefix+"add_var(Elements_Color)");              throw FinleyAdapterException(msgPrefix+"put(Elements_Nodes)");
346        int_ptr = &mesh->Elements->Color[0];      }
347        if (! (ids->put(int_ptr, num_Elements)) )  
348           throw DataException(msgPrefix+"put(Elements_Color)");      // // // // // Face_Elements // // // // //
349        if (num_FaceElements>0) {
350        // Elements_Nodes          // FaceElements_Id
351        if (! ( ids = dataFile.add_var("Elements_Nodes", ncInt, ncdims[3], ncdims[7]) ) )          if (! ( ids = dataFile.add_var("FaceElements_Id", ncInt, ncdims[4])) )
352           throw DataException(msgPrefix+"add_var(Elements_Nodes)");              throw FinleyAdapterException(msgPrefix+"add_var(FaceElements_Id)");
353        if (! (ids->put(&(mesh->Elements->Nodes[0]), num_Elements, num_Elements_numNodes)) )          int_ptr = &mesh->FaceElements->Id[0];
354           throw DataException(msgPrefix+"put(Elements_Nodes)");          if (! (ids->put(int_ptr, num_FaceElements)) )
355                throw FinleyAdapterException(msgPrefix+"put(FaceElements_Id)");
356     }  
357            // FaceElements_Tag
358     // // // // // Face_Elements // // // // //          if (! ( ids = dataFile.add_var("FaceElements_Tag", ncInt, ncdims[4])) )
359                throw FinleyAdapterException(msgPrefix+"add_var(FaceElements_Tag)");
360     if (num_FaceElements>0) {          int_ptr = &mesh->FaceElements->Tag[0];
361            if (! (ids->put(int_ptr, num_FaceElements)) )
362        // FaceElements_Id              throw FinleyAdapterException(msgPrefix+"put(FaceElements_Tag)");
363        if (! ( ids = dataFile.add_var("FaceElements_Id", ncInt, ncdims[4])) )  
364           throw DataException(msgPrefix+"add_var(FaceElements_Id)");          // FaceElements_Owner
365        int_ptr = &mesh->FaceElements->Id[0];          if (! ( ids = dataFile.add_var("FaceElements_Owner", ncInt, ncdims[4])) )
366        if (! (ids->put(int_ptr, num_FaceElements)) )              throw FinleyAdapterException(msgPrefix+"add_var(FaceElements_Owner)");
367           throw DataException(msgPrefix+"put(FaceElements_Id)");          int_ptr = &mesh->FaceElements->Owner[0];
368            if (! (ids->put(int_ptr, num_FaceElements)) )
369        // FaceElements_Tag              throw FinleyAdapterException(msgPrefix+"put(FaceElements_Owner)");
370        if (! ( ids = dataFile.add_var("FaceElements_Tag", ncInt, ncdims[4])) )  
371           throw DataException(msgPrefix+"add_var(FaceElements_Tag)");          // FaceElements_Color
372        int_ptr = &mesh->FaceElements->Tag[0];          if (! ( ids = dataFile.add_var("FaceElements_Color", ncInt, ncdims[4])) )
373        if (! (ids->put(int_ptr, num_FaceElements)) )              throw FinleyAdapterException(msgPrefix+"add_var(FaceElements_Color)");
374           throw DataException(msgPrefix+"put(FaceElements_Tag)");          int_ptr = &mesh->FaceElements->Color[0];
375            if (! (ids->put(int_ptr, num_FaceElements)) )
376        // FaceElements_Owner              throw FinleyAdapterException(msgPrefix+"put(FaceElements_Color)");
377        if (! ( ids = dataFile.add_var("FaceElements_Owner", ncInt, ncdims[4])) )  
378           throw DataException(msgPrefix+"add_var(FaceElements_Owner)");          // FaceElements_Nodes
379        int_ptr = &mesh->FaceElements->Owner[0];          if (! ( ids = dataFile.add_var("FaceElements_Nodes", ncInt, ncdims[4], ncdims[8]) ) )
380        if (! (ids->put(int_ptr, num_FaceElements)) )              throw FinleyAdapterException(msgPrefix+"add_var(FaceElements_Nodes)");
381           throw DataException(msgPrefix+"put(FaceElements_Owner)");          if (! (ids->put(&(mesh->FaceElements->Nodes[0]), num_FaceElements, num_FaceElements_numNodes)) )
382                throw FinleyAdapterException(msgPrefix+"put(FaceElements_Nodes)");
383        // FaceElements_Color      }
384        if (! ( ids = dataFile.add_var("FaceElements_Color", ncInt, ncdims[4])) )  
385           throw DataException(msgPrefix+"add_var(FaceElements_Color)");      // // // // // Contact_Elements // // // // //
386        int_ptr = &mesh->FaceElements->Color[0];      if (num_ContactElements>0) {
387        if (! (ids->put(int_ptr, num_FaceElements)) )  
388           throw DataException(msgPrefix+"put(FaceElements_Color)");          // ContactElements_Id
389            if (! ( ids = dataFile.add_var("ContactElements_Id", ncInt, ncdims[5])) )
390        // FaceElements_Nodes              throw FinleyAdapterException(msgPrefix+"add_var(ContactElements_Id)");
391        if (! ( ids = dataFile.add_var("FaceElements_Nodes", ncInt, ncdims[4], ncdims[8]) ) )          int_ptr = &mesh->ContactElements->Id[0];
392           throw DataException(msgPrefix+"add_var(FaceElements_Nodes)");          if (! (ids->put(int_ptr, num_ContactElements)) )
393        if (! (ids->put(&(mesh->FaceElements->Nodes[0]), num_FaceElements, num_FaceElements_numNodes)) )              throw FinleyAdapterException(msgPrefix+"put(ContactElements_Id)");
394           throw DataException(msgPrefix+"put(FaceElements_Nodes)");  
395            // ContactElements_Tag
396     }          if (! ( ids = dataFile.add_var("ContactElements_Tag", ncInt, ncdims[5])) )
397                throw FinleyAdapterException(msgPrefix+"add_var(ContactElements_Tag)");
398     // // // // // Contact_Elements // // // // //          int_ptr = &mesh->ContactElements->Tag[0];
399            if (! (ids->put(int_ptr, num_ContactElements)) )
400     if (num_ContactElements>0) {              throw FinleyAdapterException(msgPrefix+"put(ContactElements_Tag)");
401    
402        // ContactElements_Id          // ContactElements_Owner
403        if (! ( ids = dataFile.add_var("ContactElements_Id", ncInt, ncdims[5])) )          if (! ( ids = dataFile.add_var("ContactElements_Owner", ncInt, ncdims[5])) )
404           throw DataException(msgPrefix+"add_var(ContactElements_Id)");              throw FinleyAdapterException(msgPrefix+"add_var(ContactElements_Owner)");
405        int_ptr = &mesh->ContactElements->Id[0];          int_ptr = &mesh->ContactElements->Owner[0];
406        if (! (ids->put(int_ptr, num_ContactElements)) )          if (! (ids->put(int_ptr, num_ContactElements)) )
407           throw DataException(msgPrefix+"put(ContactElements_Id)");              throw FinleyAdapterException(msgPrefix+"put(ContactElements_Owner)");
408    
409        // ContactElements_Tag          // ContactElements_Color
410        if (! ( ids = dataFile.add_var("ContactElements_Tag", ncInt, ncdims[5])) )          if (! ( ids = dataFile.add_var("ContactElements_Color", ncInt, ncdims[5])) )
411           throw DataException(msgPrefix+"add_var(ContactElements_Tag)");              throw FinleyAdapterException(msgPrefix+"add_var(ContactElements_Color)");
412        int_ptr = &mesh->ContactElements->Tag[0];          int_ptr = &mesh->ContactElements->Color[0];
413        if (! (ids->put(int_ptr, num_ContactElements)) )          if (! (ids->put(int_ptr, num_ContactElements)) )
414           throw DataException(msgPrefix+"put(ContactElements_Tag)");              throw FinleyAdapterException(msgPrefix+"put(ContactElements_Color)");
415    
416        // ContactElements_Owner          // ContactElements_Nodes
417        if (! ( ids = dataFile.add_var("ContactElements_Owner", ncInt, ncdims[5])) )          if (! ( ids = dataFile.add_var("ContactElements_Nodes", ncInt, ncdims[5], ncdims[9]) ) )
418           throw DataException(msgPrefix+"add_var(ContactElements_Owner)");              throw FinleyAdapterException(msgPrefix+"add_var(ContactElements_Nodes)");
419        int_ptr = &mesh->ContactElements->Owner[0];          if (! (ids->put(&(mesh->ContactElements->Nodes[0]), num_ContactElements, num_ContactElements_numNodes)) )
420        if (! (ids->put(int_ptr, num_ContactElements)) )              throw FinleyAdapterException(msgPrefix+"put(ContactElements_Nodes)");
421           throw DataException(msgPrefix+"put(ContactElements_Owner)");      }
422    
423        // ContactElements_Color      // // // // // Points // // // // //
424        if (! ( ids = dataFile.add_var("ContactElements_Color", ncInt, ncdims[5])) )      if (num_Points>0) {
425           throw DataException(msgPrefix+"add_var(ContactElements_Color)");          // Points_Id
426        int_ptr = &mesh->ContactElements->Color[0];          if (! ( ids = dataFile.add_var("Points_Id", ncInt, ncdims[6])) )
427        if (! (ids->put(int_ptr, num_ContactElements)) )              throw FinleyAdapterException(msgPrefix+"add_var(Points_Id)");
428           throw DataException(msgPrefix+"put(ContactElements_Color)");          int_ptr = &mesh->Points->Id[0];
429            if (! (ids->put(int_ptr, num_Points)) )
430        // ContactElements_Nodes              throw FinleyAdapterException(msgPrefix+"put(Points_Id)");
431        if (! ( ids = dataFile.add_var("ContactElements_Nodes", ncInt, ncdims[5], ncdims[9]) ) )  
432           throw DataException(msgPrefix+"add_var(ContactElements_Nodes)");          // Points_Tag
433        if (! (ids->put(&(mesh->ContactElements->Nodes[0]), num_ContactElements, num_ContactElements_numNodes)) )          if (! ( ids = dataFile.add_var("Points_Tag", ncInt, ncdims[6])) )
434           throw DataException(msgPrefix+"put(ContactElements_Nodes)");              throw FinleyAdapterException(msgPrefix+"add_var(Points_Tag)");
435            int_ptr = &mesh->Points->Tag[0];
436     }          if (! (ids->put(int_ptr, num_Points)) )
437                throw FinleyAdapterException(msgPrefix+"put(Points_Tag)");
438     // // // // // Points // // // // //  
439            // Points_Owner
440     if (num_Points>0) {          if (! ( ids = dataFile.add_var("Points_Owner", ncInt, ncdims[6])) )
441                throw FinleyAdapterException(msgPrefix+"add_var(Points_Owner)");
442        fprintf(stderr, "\n\n\nWARNING: MeshAdapter::dump has not been tested with Point elements\n\n\n");          int_ptr = &mesh->Points->Owner[0];
443            if (! (ids->put(int_ptr, num_Points)) )
444        // Points_Id              throw FinleyAdapterException(msgPrefix+"put(Points_Owner)");
445        if (! ( ids = dataFile.add_var("Points_Id", ncInt, ncdims[6])) )  
446           throw DataException(msgPrefix+"add_var(Points_Id)");          // Points_Color
447        int_ptr = &mesh->Points->Id[0];          if (! ( ids = dataFile.add_var("Points_Color", ncInt, ncdims[6])) )
448        if (! (ids->put(int_ptr, num_Points)) )              throw FinleyAdapterException(msgPrefix+"add_var(Points_Color)");
449           throw DataException(msgPrefix+"put(Points_Id)");          int_ptr = &mesh->Points->Color[0];
450            if (! (ids->put(int_ptr, num_Points)) )
451        // Points_Tag              throw FinleyAdapterException(msgPrefix+"put(Points_Color)");
452        if (! ( ids = dataFile.add_var("Points_Tag", ncInt, ncdims[6])) )  
453           throw DataException(msgPrefix+"add_var(Points_Tag)");          // Points_Nodes
454        int_ptr = &mesh->Points->Tag[0];          // mesh->Nodes->Id[mesh->Points->Nodes[INDEX2(0,i,1)]]
455        if (! (ids->put(int_ptr, num_Points)) )          if (! ( ids = dataFile.add_var("Points_Nodes", ncInt, ncdims[6]) ) )
456           throw DataException(msgPrefix+"put(Points_Tag)");              throw FinleyAdapterException(msgPrefix+"add_var(Points_Nodes)");
457            if (! (ids->put(&(mesh->Points->Nodes[0]), num_Points)) )
458        // Points_Owner              throw FinleyAdapterException(msgPrefix+"put(Points_Nodes)");
459        if (! ( ids = dataFile.add_var("Points_Owner", ncInt, ncdims[6])) )      }
460           throw DataException(msgPrefix+"add_var(Points_Owner)");  
461        int_ptr = &mesh->Points->Owner[0];      // // // // // TagMap // // // // //
462        if (! (ids->put(int_ptr, num_Points)) )      if (num_Tags>0) {
463           throw DataException(msgPrefix+"put(Points_Owner)");          // Temp storage to gather node IDs
464            vector<int> Tags_keys;
465        // Points_Color  
466        if (! ( ids = dataFile.add_var("Points_Color", ncInt, ncdims[6])) )          // Copy tag data into temp arrays
467           throw DataException(msgPrefix+"add_var(Points_Color)");          TagMap::const_iterator it;
468        int_ptr = &mesh->Points->Color[0];          for (it=mesh->tagMap.begin(); it!=mesh->tagMap.end(); it++) {
469        if (! (ids->put(int_ptr, num_Points)) )              Tags_keys.push_back(it->second);
470           throw DataException(msgPrefix+"put(Points_Color)");          }
471    
472        // Points_Nodes          // Tags_keys
473        // mesh->Nodes->Id[mesh->Points->Nodes[INDEX2(0,i,1)]]          if (! (ids = dataFile.add_var("Tags_keys", ncInt, ncdims[10])) )
474        if (! ( ids = dataFile.add_var("Points_Nodes", ncInt, ncdims[6]) ) )              throw FinleyAdapterException(msgPrefix+"add_var(Tags_keys)");
475           throw DataException(msgPrefix+"add_var(Points_Nodes)");          int_ptr = &Tags_keys[0];
476        if (! (ids->put(&(mesh->Points->Nodes[0]), num_Points)) )          if (! (ids->put(int_ptr, num_Tags)) )
477           throw DataException(msgPrefix+"put(Points_Nodes)");              throw FinleyAdapterException(msgPrefix+"put(Tags_keys)");
478    
479     }          // Tags_names_*
480            // This is an array of strings, it should be stored as an array but
481     // // // // // TagMap // // // // //          // instead I have hacked in one attribute per string because the NetCDF
482            // manual doesn't tell how to do an array of strings
483     if (num_Tags>0) {          int i = 0;
484            for (it=mesh->tagMap.begin(); it!=mesh->tagMap.end(); it++, i++) {
485        // Temp storage to gather node IDs              stringstream tagnamestream;
486        int *Tags_keys = TMPMEMALLOC(num_Tags, int);              tagnamestream << "Tags_name_" << i;
487        char name_temp[4096];              const string tagname = tagnamestream.str();
488                if (!dataFile.add_att(tagname.c_str(), it->first.c_str()))
489        /* Copy tag data into temp arrays */                  throw FinleyAdapterException(msgPrefix+"add_att(Tags_names_X)");
490        tag_map = mesh->TagMap;          }
491        if (tag_map) {      }
492           int i = 0;  
493           while (tag_map) {      // Send token to next MPI process so he can take his turn
494              Tags_keys[i++] = tag_map->tag_key;  #ifdef ESYS_MPI
495              tag_map=tag_map->next;      if (mpi_rank<mpi_size-1) {
496           }          MPI_Send(&num_Tags, 0, MPI_INT, mpi_rank+1, 81800, mesh->MPIInfo->comm);
497        }      }
   
       // Tags_keys  
       if (! ( ids = dataFile.add_var("Tags_keys", ncInt, ncdims[10])) )  
          throw DataException(msgPrefix+"add_var(Tags_keys)");  
       int_ptr = &Tags_keys[0];  
       if (! (ids->put(int_ptr, num_Tags)) )  
          throw DataException(msgPrefix+"put(Tags_keys)");  
   
       // Tags_names_*  
       // This is an array of strings, it should be stored as an array but  
       // instead I have hacked in one attribute per string because the NetCDF  
       // manual doesn't tell how to do an array of strings  
       tag_map = mesh->TagMap;  
       if (tag_map) {  
          int i = 0;  
          while (tag_map) {  
             sprintf(name_temp, "Tags_name_%d", i);  
             if (!dataFile.add_att(name_temp, tag_map->name) )  
                throw DataException(msgPrefix+"add_att(Tags_names_XX)");  
             tag_map=tag_map->next;  
             i++;  
          }  
       }  
   
       TMPMEMFREE(Tags_keys);  
    }  
   
 /* Send token to next MPI process so he can take his turn */  
 #ifdef PASO_MPI  
    if (mpi_rank<mpi_size-1) MPI_Send(&num_Tags, 0, MPI_INT, mpi_rank+1, 81800, mesh->MPIInfo->comm);  
498  #endif  #endif
499    
500     // NetCDF file is closed by destructor of NcFile object     // NetCDF file is closed by destructor of NcFile object
501    
502  #else  #else // USE_NETCDF
503     Finley_setError(IO_ERROR, "MeshAdapter::dump: not configured with NetCDF. Please contact your installation manager.");      Finley_setError(IO_ERROR, "MeshAdapter::dump: not configured with netCDF. Please contact your installation manager.");
504  #endif  /* USE_NETCDF */  #endif // USE_NETCDF
505     checkFinleyError();      checkFinleyError();
506  }  }
507    
508  string MeshAdapter::getDescription() const  string MeshAdapter::getDescription() const
509  {  {
510     return "FinleyMesh";      return "FinleyMesh";
511  }  }
512    
513  string MeshAdapter::functionSpaceTypeAsString(int functionSpaceType) const  string MeshAdapter::functionSpaceTypeAsString(int functionSpaceType) const
514  {  {
515     FunctionSpaceNamesMapType::iterator loc;      FunctionSpaceNamesMapType::iterator loc;
516     loc=m_functionSpaceTypeNames.find(functionSpaceType);      loc=m_functionSpaceTypeNames.find(functionSpaceType);
517     if (loc==m_functionSpaceTypeNames.end()) {      if (loc==m_functionSpaceTypeNames.end()) {
518        return "Invalid function space type code.";          return "Invalid function space type code.";
519     } else {      } else {
520        return loc->second;          return loc->second;
521     }      }
522  }  }
523    
524  bool MeshAdapter::isValidFunctionSpaceType(int functionSpaceType) const  bool MeshAdapter::isValidFunctionSpaceType(int functionSpaceType) const
525  {  {
526     FunctionSpaceNamesMapType::iterator loc;      FunctionSpaceNamesMapType::iterator loc;
527     loc=m_functionSpaceTypeNames.find(functionSpaceType);      loc=m_functionSpaceTypeNames.find(functionSpaceType);
528     return (loc!=m_functionSpaceTypeNames.end());      return (loc!=m_functionSpaceTypeNames.end());
529  }  }
530    
531  void MeshAdapter::setFunctionSpaceTypeNames()  void MeshAdapter::setFunctionSpaceTypeNames()
532  {  {
533     m_functionSpaceTypeNames.insert      m_functionSpaceTypeNames.insert(FunctionSpaceNamesMapType::value_type(
534     (FunctionSpaceNamesMapType::value_type(DegreesOfFreedom,"Finley_DegreesOfFreedom"));                  DegreesOfFreedom,"Finley_DegreesOfFreedom"));
535     m_functionSpaceTypeNames.insert      m_functionSpaceTypeNames.insert(FunctionSpaceNamesMapType::value_type(
536     (FunctionSpaceNamesMapType::value_type(ReducedDegreesOfFreedom,"Finley_ReducedDegreesOfFreedom"));                  ReducedDegreesOfFreedom,"Finley_ReducedDegreesOfFreedom"));
537     m_functionSpaceTypeNames.insert      m_functionSpaceTypeNames.insert(FunctionSpaceNamesMapType::value_type(
538     (FunctionSpaceNamesMapType::value_type(Nodes,"Finley_Nodes"));                  Nodes,"Finley_Nodes"));
539     m_functionSpaceTypeNames.insert      m_functionSpaceTypeNames.insert(FunctionSpaceNamesMapType::value_type(
540     (FunctionSpaceNamesMapType::value_type(ReducedNodes,"Finley_Reduced_Nodes"));                  ReducedNodes,"Finley_Reduced_Nodes"));
541     m_functionSpaceTypeNames.insert      m_functionSpaceTypeNames.insert(FunctionSpaceNamesMapType::value_type(
542     (FunctionSpaceNamesMapType::value_type(Elements,"Finley_Elements"));                  Elements,"Finley_Elements"));
543     m_functionSpaceTypeNames.insert      m_functionSpaceTypeNames.insert(FunctionSpaceNamesMapType::value_type(
544     (FunctionSpaceNamesMapType::value_type(ReducedElements,"Finley_Reduced_Elements"));                  ReducedElements,"Finley_Reduced_Elements"));
545     m_functionSpaceTypeNames.insert      m_functionSpaceTypeNames.insert(FunctionSpaceNamesMapType::value_type(
546     (FunctionSpaceNamesMapType::value_type(FaceElements,"Finley_Face_Elements"));                  FaceElements,"Finley_Face_Elements"));
547     m_functionSpaceTypeNames.insert      m_functionSpaceTypeNames.insert(FunctionSpaceNamesMapType::value_type(
548     (FunctionSpaceNamesMapType::value_type(ReducedFaceElements,"Finley_Reduced_Face_Elements"));                  ReducedFaceElements,"Finley_Reduced_Face_Elements"));
549     m_functionSpaceTypeNames.insert      m_functionSpaceTypeNames.insert(FunctionSpaceNamesMapType::value_type(
550     (FunctionSpaceNamesMapType::value_type(Points,"Finley_Points"));                  Points,"Finley_Points"));
551     m_functionSpaceTypeNames.insert      m_functionSpaceTypeNames.insert(FunctionSpaceNamesMapType::value_type(
552     (FunctionSpaceNamesMapType::value_type(ContactElementsZero,"Finley_Contact_Elements_0"));                  ContactElementsZero,"Finley_Contact_Elements_0"));
553     m_functionSpaceTypeNames.insert      m_functionSpaceTypeNames.insert(FunctionSpaceNamesMapType::value_type(
554     (FunctionSpaceNamesMapType::value_type(ReducedContactElementsZero,"Finley_Reduced_Contact_Elements_0"));                  ReducedContactElementsZero,"Finley_Reduced_Contact_Elements_0"));
555     m_functionSpaceTypeNames.insert      m_functionSpaceTypeNames.insert(FunctionSpaceNamesMapType::value_type(
556     (FunctionSpaceNamesMapType::value_type(ContactElementsOne,"Finley_Contact_Elements_1"));                  ContactElementsOne,"Finley_Contact_Elements_1"));
557     m_functionSpaceTypeNames.insert      m_functionSpaceTypeNames.insert(FunctionSpaceNamesMapType::value_type(
558     (FunctionSpaceNamesMapType::value_type(ReducedContactElementsOne,"Finley_Reduced_Contact_Elements_1"));                  ReducedContactElementsOne,"Finley_Reduced_Contact_Elements_1"));
559  }  }
560    
561  int MeshAdapter::getContinuousFunctionCode() const  int MeshAdapter::getContinuousFunctionCode() const
562  {  {
563     return Nodes;      return Nodes;
564  }  }
565    
566  int MeshAdapter::getReducedContinuousFunctionCode() const  int MeshAdapter::getReducedContinuousFunctionCode() const
567  {  {
568     return ReducedNodes;      return ReducedNodes;
569  }  }
570    
571  int MeshAdapter::getFunctionCode() const  int MeshAdapter::getFunctionCode() const
572  {  {
573     return Elements;      return Elements;
574  }  }
575    
576  int MeshAdapter::getReducedFunctionCode() const  int MeshAdapter::getReducedFunctionCode() const
577  {  {
578     return ReducedElements;      return ReducedElements;
579  }  }
580    
581  int MeshAdapter::getFunctionOnBoundaryCode() const  int MeshAdapter::getFunctionOnBoundaryCode() const
582  {  {
583     return FaceElements;      return FaceElements;
584  }  }
585    
586  int MeshAdapter::getReducedFunctionOnBoundaryCode() const  int MeshAdapter::getReducedFunctionOnBoundaryCode() const
587  {  {
588     return ReducedFaceElements;      return ReducedFaceElements;
589  }  }
590    
591  int MeshAdapter::getFunctionOnContactZeroCode() const  int MeshAdapter::getFunctionOnContactZeroCode() const
592  {  {
593     return ContactElementsZero;      return ContactElementsZero;
594  }  }
595    
596  int MeshAdapter::getReducedFunctionOnContactZeroCode() const  int MeshAdapter::getReducedFunctionOnContactZeroCode() const
597  {  {
598     return ReducedContactElementsZero;      return ReducedContactElementsZero;
599  }  }
600    
601  int MeshAdapter::getFunctionOnContactOneCode() const  int MeshAdapter::getFunctionOnContactOneCode() const
602  {  {
603     return ContactElementsOne;      return ContactElementsOne;
604  }  }
605    
606  int MeshAdapter::getReducedFunctionOnContactOneCode() const  int MeshAdapter::getReducedFunctionOnContactOneCode() const
607  {  {
608     return ReducedContactElementsOne;      return ReducedContactElementsOne;
609  }  }
610    
611  int MeshAdapter::getSolutionCode() const  int MeshAdapter::getSolutionCode() const
612  {  {
613     return DegreesOfFreedom;      return DegreesOfFreedom;
614  }  }
615    
616  int MeshAdapter::getReducedSolutionCode() const  int MeshAdapter::getReducedSolutionCode() const
617  {  {
618     return ReducedDegreesOfFreedom;      return ReducedDegreesOfFreedom;
619  }  }
620    
621  int MeshAdapter::getDiracDeltaFunctionCode() const  int MeshAdapter::getDiracDeltaFunctionsCode() const
622  {  {
623     return Points;      return Points;
624  }  }
625    
626  //  //
# Line 659  int MeshAdapter::getDiracDeltaFunctionCo Line 628  int MeshAdapter::getDiracDeltaFunctionCo
628  //  //
629  int MeshAdapter::getDim() const  int MeshAdapter::getDim() const
630  {  {
631     Finley_Mesh* mesh=m_finleyMesh.get();      Finley_Mesh* mesh=m_finleyMesh.get();
632     int numDim=Finley_Mesh_getDim(mesh);      int numDim=Finley_Mesh_getDim(mesh);
633     checkFinleyError();      checkFinleyError();
634     return numDim;      return numDim;
635  }  }
636    
637  //  //
# Line 670  int MeshAdapter::getDim() const Line 639  int MeshAdapter::getDim() const
639  //  //
640  int MeshAdapter::getNumDataPointsGlobal() const  int MeshAdapter::getNumDataPointsGlobal() const
641  {  {
642     return Finley_NodeFile_getGlobalNumNodes(m_finleyMesh.get()->Nodes);      return m_finleyMesh.get()->Nodes->getGlobalNumNodes();
643  }  }
644    
645  //  //
# Line 679  int MeshAdapter::getNumDataPointsGlobal( Line 648  int MeshAdapter::getNumDataPointsGlobal(
648  //  //
649  pair<int,int> MeshAdapter::getDataShape(int functionSpaceCode) const  pair<int,int> MeshAdapter::getDataShape(int functionSpaceCode) const
650  {  {
651     int numDataPointsPerSample=0;      int numDataPointsPerSample=0;
652     int numSamples=0;      int numSamples=0;
653     Finley_Mesh* mesh=m_finleyMesh.get();      Finley_Mesh* mesh=m_finleyMesh.get();
654     switch (functionSpaceCode) {      switch (functionSpaceCode) {
655     case(Nodes):          case Nodes:
656     numDataPointsPerSample=1;              numDataPointsPerSample=1;
657     numSamples=Finley_NodeFile_getNumNodes(mesh->Nodes);              numSamples=mesh->Nodes->getNumNodes();
658     break;              break;
659     case(ReducedNodes):          case ReducedNodes:
660     numDataPointsPerSample=1;              numDataPointsPerSample=1;
661     numSamples=Finley_NodeFile_getNumReducedNodes(mesh->Nodes);              numSamples=mesh->Nodes->getNumReducedNodes();
662     break;              break;
663     case(Elements):          case Elements:
664     if (mesh->Elements!=NULL) {              if (mesh->Elements!=NULL) {
665        numSamples=mesh->Elements->numElements;                  numSamples=mesh->Elements->numElements;
666        numDataPointsPerSample=mesh->Elements->referenceElementSet->referenceElement->Parametrization->numQuadNodes;                  numDataPointsPerSample=mesh->Elements->referenceElementSet->referenceElement->Parametrization->numQuadNodes;
667     }              }
668     break;              break;
669     case(ReducedElements):          case ReducedElements:
670     if (mesh->Elements!=NULL) {              if (mesh->Elements!=NULL) {
671        numSamples=mesh->Elements->numElements;                  numSamples=mesh->Elements->numElements;
672        numDataPointsPerSample=mesh->Elements->referenceElementSet->referenceElementReducedQuadrature->Parametrization->numQuadNodes;                  numDataPointsPerSample=mesh->Elements->referenceElementSet->referenceElementReducedQuadrature->Parametrization->numQuadNodes;
673     }              }
674     break;              break;
675     case(FaceElements):          case FaceElements:
676     if (mesh->FaceElements!=NULL) {              if (mesh->FaceElements!=NULL) {
677        numDataPointsPerSample=mesh->FaceElements->referenceElementSet->referenceElement->Parametrization->numQuadNodes;                  numDataPointsPerSample=mesh->FaceElements->referenceElementSet->referenceElement->Parametrization->numQuadNodes;
678        numSamples=mesh->FaceElements->numElements;                  numSamples=mesh->FaceElements->numElements;
679     }              }
680     break;              break;
681     case(ReducedFaceElements):          case ReducedFaceElements:
682     if (mesh->FaceElements!=NULL) {              if (mesh->FaceElements!=NULL) {
683        numDataPointsPerSample=mesh->FaceElements->referenceElementSet->referenceElementReducedQuadrature->Parametrization->numQuadNodes;                  numDataPointsPerSample=mesh->FaceElements->referenceElementSet->referenceElementReducedQuadrature->Parametrization->numQuadNodes;
684        numSamples=mesh->FaceElements->numElements;                  numSamples=mesh->FaceElements->numElements;
685     }              }
686     break;              break;
687     case(Points):          case Points:
688     if (mesh->Points!=NULL) {              if (mesh->Points!=NULL) {
689        numDataPointsPerSample=1;                  numDataPointsPerSample=1;
690        numSamples=mesh->Points->numElements;                  numSamples=mesh->Points->numElements;
691     }              }
692     break;              break;
693     case(ContactElementsZero):          case ContactElementsZero:
694     if (mesh->ContactElements!=NULL) {              if (mesh->ContactElements!=NULL) {
695        numDataPointsPerSample=mesh->ContactElements->referenceElementSet->referenceElement->Parametrization->numQuadNodes;                  numDataPointsPerSample=mesh->ContactElements->referenceElementSet->referenceElement->Parametrization->numQuadNodes;
696        numSamples=mesh->ContactElements->numElements;                  numSamples=mesh->ContactElements->numElements;
697     }              }
698     break;              break;
699     case(ReducedContactElementsZero):          case ReducedContactElementsZero:
700     if (mesh->ContactElements!=NULL) {              if (mesh->ContactElements!=NULL) {
701        numDataPointsPerSample=mesh->ContactElements->referenceElementSet->referenceElementReducedQuadrature->Parametrization->numQuadNodes;                  numDataPointsPerSample=mesh->ContactElements->referenceElementSet->referenceElementReducedQuadrature->Parametrization->numQuadNodes;
702        numSamples=mesh->ContactElements->numElements;                  numSamples=mesh->ContactElements->numElements;
703     }              }
704     break;              break;
705     case(ContactElementsOne):          case ContactElementsOne:
706     if (mesh->ContactElements!=NULL) {              if (mesh->ContactElements!=NULL) {
707        numDataPointsPerSample=mesh->ContactElements->referenceElementSet->referenceElement->Parametrization->numQuadNodes;                  numDataPointsPerSample=mesh->ContactElements->referenceElementSet->referenceElement->Parametrization->numQuadNodes;
708        numSamples=mesh->ContactElements->numElements;                  numSamples=mesh->ContactElements->numElements;
709     }              }
710     break;              break;
711     case(ReducedContactElementsOne):          case ReducedContactElementsOne:
712     if (mesh->ContactElements!=NULL) {              if (mesh->ContactElements!=NULL) {
713        numDataPointsPerSample=mesh->ContactElements->referenceElementSet->referenceElementReducedQuadrature->Parametrization->numQuadNodes;                  numDataPointsPerSample=mesh->ContactElements->referenceElementSet->referenceElementReducedQuadrature->Parametrization->numQuadNodes;
714        numSamples=mesh->ContactElements->numElements;                  numSamples=mesh->ContactElements->numElements;
715     }              }
716     break;              break;
717     case(DegreesOfFreedom):          case DegreesOfFreedom:
718     if (mesh->Nodes!=NULL) {              if (mesh->Nodes!=NULL) {
719        numDataPointsPerSample=1;                  numDataPointsPerSample=1;
720        numSamples=Finley_NodeFile_getNumDegreesOfFreedom(mesh->Nodes);                  numSamples=mesh->Nodes->getNumDegreesOfFreedom();
721     }              }
722     break;              break;
723     case(ReducedDegreesOfFreedom):          case ReducedDegreesOfFreedom:
724     if (mesh->Nodes!=NULL) {              if (mesh->Nodes!=NULL) {
725        numDataPointsPerSample=1;                  numDataPointsPerSample=1;
726        numSamples=Finley_NodeFile_getNumReducedDegreesOfFreedom(mesh->Nodes);                  numSamples=mesh->Nodes->getNumReducedDegreesOfFreedom();
727     }              }
728     break;              break;
729     default:          default:
730        stringstream temp;              stringstream temp;
731        temp << "Error - Invalid function space type: " << functionSpaceCode << " for domain: " << getDescription();              temp << "Error - Invalid function space type: " << functionSpaceCode << " for domain: " << getDescription();
732        throw FinleyAdapterException(temp.str());              throw FinleyAdapterException(temp.str());
733        break;              break;
734     }      }
735     return pair<int,int>(numDataPointsPerSample,numSamples);      return pair<int,int>(numDataPointsPerSample,numSamples);
736  }  }
737    
738  //  //
739  // adds linear PDE of second order into a given stiffness matrix and right hand side:  // adds linear PDE of second order into a given stiffness matrix and right
740    // hand side:
741  //  //
742  void MeshAdapter::addPDEToSystem(  void MeshAdapter::addPDEToSystem(
743                                   SystemMatrixAdapter& mat, escript::Data& rhs,          escript::AbstractSystemMatrix& mat, escript::Data& rhs,
744                                   const escript::Data& A, const escript::Data& B, const escript::Data& C,const  escript::Data& D,const  escript::Data& X,const  escript::Data& Y,          const escript::Data& A, const escript::Data& B, const escript::Data& C,
745                                   const escript::Data& d, const escript::Data& y,          const escript::Data& D, const escript::Data& X, const escript::Data& Y,
746                                   const escript::Data& d_contact,const escript::Data& y_contact) const          const escript::Data& d, const escript::Data& y,
747  {          const escript::Data& d_contact, const escript::Data& y_contact,
748     escriptDataC _rhs=rhs.getDataC();          const escript::Data& d_dirac, const escript::Data& y_dirac) const
749     escriptDataC _A  =A.getDataC();  {
750     escriptDataC _B=B.getDataC();      SystemMatrixAdapter* smat=dynamic_cast<SystemMatrixAdapter*>(&mat);
751     escriptDataC _C=C.getDataC();      if (!smat)
752     escriptDataC _D=D.getDataC();          throw FinleyAdapterException("finley only supports Paso system matrices.");
753     escriptDataC _X=X.getDataC();  
754     escriptDataC _Y=Y.getDataC();      escriptDataC _rhs=rhs.getDataC();
755     escriptDataC _d=d.getDataC();      escriptDataC _A  =A.getDataC();
756     escriptDataC _y=y.getDataC();      escriptDataC _B=B.getDataC();
757     escriptDataC _d_contact=d_contact.getDataC();      escriptDataC _C=C.getDataC();
758     escriptDataC _y_contact=y_contact.getDataC();      escriptDataC _D=D.getDataC();
759        escriptDataC _X=X.getDataC();
760     Finley_Mesh* mesh=m_finleyMesh.get();      escriptDataC _Y=Y.getDataC();
761        escriptDataC _d=d.getDataC();
762     Finley_Assemble_PDE(mesh->Nodes,mesh->Elements,mat.getPaso_SystemMatrix(), &_rhs, &_A, &_B, &_C, &_D, &_X, &_Y );      escriptDataC _y=y.getDataC();
763     checkFinleyError();      escriptDataC _d_contact=d_contact.getDataC();
764        escriptDataC _y_contact=y_contact.getDataC();
765     Finley_Assemble_PDE(mesh->Nodes,mesh->FaceElements, mat.getPaso_SystemMatrix(), &_rhs, 0, 0, 0, &_d, 0, &_y );      escriptDataC _d_dirac=d_dirac.getDataC();
766     checkFinleyError();      escriptDataC _y_dirac=y_dirac.getDataC();
767    
768     Finley_Assemble_PDE(mesh->Nodes,mesh->ContactElements, mat.getPaso_SystemMatrix(), &_rhs , 0, 0, 0, &_d_contact, 0, &_y_contact );      Finley_Mesh* mesh=m_finleyMesh.get();
769     checkFinleyError();      Paso_SystemMatrix* S = smat->getPaso_SystemMatrix();
770  }  
771        Finley_Assemble_PDE(mesh->Nodes, mesh->Elements, S, &_rhs, &_A, &_B, &_C,
772  void  MeshAdapter::addPDEToLumpedSystem(                          &_D, &_X, &_Y);
773                                          escript::Data& mat,      checkFinleyError();
774                                          const escript::Data& D,  
775                                          const escript::Data& d) const      Finley_Assemble_PDE(mesh->Nodes, mesh->FaceElements, S, &_rhs, 0, 0, 0,
776  {                          &_d, 0, &_y);
777     escriptDataC _mat=mat.getDataC();      checkFinleyError();
778     escriptDataC _D=D.getDataC();  
779     escriptDataC _d=d.getDataC();      Finley_Assemble_PDE(mesh->Nodes, mesh->ContactElements, S, &_rhs, 0, 0, 0,
780                &_d_contact, 0, &_y_contact);
781     Finley_Mesh* mesh=m_finleyMesh.get();      checkFinleyError();
782    
783     Finley_Assemble_LumpedSystem(mesh->Nodes,mesh->Elements,&_mat, &_D);      Finley_Assemble_PDE(mesh->Nodes, mesh->Points, S, &_rhs, 0, 0, 0,
784     checkFinleyError();                          &_d_dirac, 0, &_y_dirac );
785          checkFinleyError();
786     Finley_Assemble_LumpedSystem(mesh->Nodes,mesh->FaceElements,&_mat, &_d);  }
787     checkFinleyError();  
788    void MeshAdapter::addPDEToLumpedSystem(escript::Data& mat,
789            const escript::Data& D, const escript::Data& d,
790            const escript::Data& d_dirac, const bool useHRZ) const
791    {
792        escriptDataC _mat=mat.getDataC();
793        escriptDataC _D=D.getDataC();
794        escriptDataC _d=d.getDataC();
795        escriptDataC _d_dirac=d_dirac.getDataC();
796    
797        Finley_Mesh* mesh=m_finleyMesh.get();
798    
799  }      Finley_Assemble_LumpedSystem(mesh->Nodes, mesh->Elements, &_mat, &_D, useHRZ);
800        checkFinleyError();
801    
802        Finley_Assemble_LumpedSystem(mesh->Nodes, mesh->FaceElements, &_mat, &_d, useHRZ);
803        checkFinleyError();
804    
805        Finley_Assemble_LumpedSystem(mesh->Nodes, mesh->Points, &_mat, &_d_dirac, useHRZ);
806        checkFinleyError();
807    }
808    
809  //  //
810  // adds linear PDE of second order into the right hand side only  // adds linear PDE of second order into the right hand side only
811  //  //
812  void MeshAdapter::addPDEToRHS( escript::Data& rhs, const  escript::Data& X,const  escript::Data& Y, const escript::Data& y, const escript::Data& y_contact) const  void MeshAdapter::addPDEToRHS(escript::Data& rhs, const escript::Data& X,
813  {          const escript::Data& Y, const escript::Data& y,
814     Finley_Mesh* mesh=m_finleyMesh.get();          const escript::Data& y_contact, const escript::Data& y_dirac) const
815    {
816        Finley_Mesh* mesh=m_finleyMesh.get();
817    
818        escriptDataC _rhs=rhs.getDataC();
819        escriptDataC _X=X.getDataC();
820        escriptDataC _Y=Y.getDataC();
821        escriptDataC _y=y.getDataC();
822        escriptDataC _y_contact=y_contact.getDataC();
823        escriptDataC _y_dirac=y_dirac.getDataC();
824    
825        Finley_Assemble_PDE(mesh->Nodes,mesh->Elements, 0, &_rhs, 0, 0, 0, 0, &_X, &_Y);
826        checkFinleyError();
827    
828     escriptDataC _rhs=rhs.getDataC();      Finley_Assemble_PDE(mesh->Nodes,mesh->FaceElements, 0, &_rhs, 0, 0, 0, 0, 0, &_y);
829     escriptDataC _X=X.getDataC();      checkFinleyError();
    escriptDataC _Y=Y.getDataC();  
    escriptDataC _y=y.getDataC();  
    escriptDataC _y_contact=y_contact.getDataC();  
830    
831     Finley_Assemble_PDE(mesh->Nodes,mesh->Elements, 0, &_rhs, 0, 0, 0, 0, &_X, &_Y );      Finley_Assemble_PDE(mesh->Nodes,mesh->ContactElements, 0, &_rhs, 0, 0, 0, 0, 0, &_y_contact);
832     checkFinleyError();      checkFinleyError();
833    
834     Finley_Assemble_PDE(mesh->Nodes,mesh->FaceElements, 0, &_rhs, 0, 0, 0, 0, 0, &_y );      Finley_Assemble_PDE(mesh->Nodes,mesh->Points, 0, &_rhs, 0, 0, 0, 0, 0, &_y_dirac);
835     checkFinleyError();      checkFinleyError();
   
    Finley_Assemble_PDE(mesh->Nodes,mesh->ContactElements, 0, &_rhs , 0, 0, 0, 0, 0, &_y_contact );  
    checkFinleyError();  
836  }  }
837    
838  //  //
839  // adds PDE of second order into a transport problem  // adds PDE of second order into a transport problem
840  //  //
841  void MeshAdapter::addPDEToTransportProblem(  void MeshAdapter::addPDEToTransportProblem(
842                                             TransportProblemAdapter& tp, escript::Data& source, const escript::Data& M,          escript::AbstractTransportProblem& tp, escript::Data& source,
843                                             const escript::Data& A, const escript::Data& B, const escript::Data& C,          const escript::Data& M, const escript::Data& A, const escript::Data& B,
844                                             const  escript::Data& D,const  escript::Data& X,const  escript::Data& Y,          const escript::Data& C, const escript::Data& D, const escript::Data& X,
845                                             const escript::Data& d, const escript::Data& y,          const escript::Data& Y, const escript::Data& d, const escript::Data& y,
846                                             const escript::Data& d_contact,const escript::Data& y_contact) const          const escript::Data& d_contact, const escript::Data& y_contact,
847  {          const escript::Data& d_dirac, const escript::Data& y_dirac) const
848     DataTypes::ShapeType shape;  {
849     source.expand();      TransportProblemAdapter* tpa=dynamic_cast<TransportProblemAdapter*>(&tp);
850     escriptDataC _source=source.getDataC();      if (!tpa)
851     escriptDataC _M=M.getDataC();          throw FinleyAdapterException("finley only supports Paso transport problems.");
852     escriptDataC _A=A.getDataC();  
853     escriptDataC _B=B.getDataC();      source.expand();
854     escriptDataC _C=C.getDataC();      escriptDataC _source=source.getDataC();
855     escriptDataC _D=D.getDataC();      escriptDataC _M=M.getDataC();
856     escriptDataC _X=X.getDataC();      escriptDataC _A=A.getDataC();
857     escriptDataC _Y=Y.getDataC();      escriptDataC _B=B.getDataC();
858     escriptDataC _d=d.getDataC();      escriptDataC _C=C.getDataC();
859     escriptDataC _y=y.getDataC();      escriptDataC _D=D.getDataC();
860     escriptDataC _d_contact=d_contact.getDataC();      escriptDataC _X=X.getDataC();
861     escriptDataC _y_contact=y_contact.getDataC();      escriptDataC _Y=Y.getDataC();
862        escriptDataC _d=d.getDataC();
863     Finley_Mesh* mesh=m_finleyMesh.get();      escriptDataC _y=y.getDataC();
864     Paso_TransportProblem* _tp = tp.getPaso_TransportProblem();      escriptDataC _d_contact=d_contact.getDataC();
865        escriptDataC _y_contact=y_contact.getDataC();
866     Finley_Assemble_PDE(mesh->Nodes,mesh->Elements,_tp->mass_matrix, &_source, 0, 0, 0, &_M, 0, 0 );      escriptDataC _d_dirac=d_dirac.getDataC();
867     checkFinleyError();      escriptDataC _y_dirac=y_dirac.getDataC();
868    
869     Finley_Assemble_PDE(mesh->Nodes,mesh->Elements,_tp->transport_matrix, &_source, &_A, &_B, &_C, &_D, &_X, &_Y );      Finley_Mesh* mesh=m_finleyMesh.get();
870     checkFinleyError();      Paso_TransportProblem* _tp = tpa->getPaso_TransportProblem();
871    
872     Finley_Assemble_PDE(mesh->Nodes,mesh->FaceElements, _tp->transport_matrix, &_source, 0, 0, 0, &_d, 0, &_y );      Finley_Assemble_PDE(mesh->Nodes, mesh->Elements, _tp->mass_matrix,
873     checkFinleyError();                          &_source, 0, 0, 0, &_M, 0, 0);
874        checkFinleyError();
875     Finley_Assemble_PDE(mesh->Nodes,mesh->ContactElements, _tp->transport_matrix, &_source , 0, 0, 0, &_d_contact, 0, &_y_contact );  
876     checkFinleyError();      Finley_Assemble_PDE(mesh->Nodes, mesh->Elements, _tp->transport_matrix,
877  }                          &_source, &_A, &_B, &_C, &_D, &_X, &_Y);
878        checkFinleyError();
879  //  
880  // interpolates data between different function spaces:      Finley_Assemble_PDE(mesh->Nodes, mesh->FaceElements, _tp->transport_matrix,
881  //                          &_source, 0, 0, 0, &_d, 0, &_y);
882  void MeshAdapter::interpolateOnDomain(escript::Data& target,const escript::Data& in) const      checkFinleyError();
883  {  
884     const MeshAdapter& inDomain=dynamic_cast<const MeshAdapter&>(*(in.getFunctionSpace().getDomain()));      Finley_Assemble_PDE(mesh->Nodes, mesh->ContactElements,
885     const MeshAdapter& targetDomain=dynamic_cast<const MeshAdapter&>(*(target.getFunctionSpace().getDomain()));                          _tp->transport_matrix, &_source, 0, 0, 0, &_d_contact, 0, &_y_contact);
886     if (inDomain!=*this)        checkFinleyError();
887        throw FinleyAdapterException("Error - Illegal domain of interpolant.");  
888     if (targetDomain!=*this)      Finley_Assemble_PDE(mesh->Nodes, mesh->Points, _tp->transport_matrix,
889        throw FinleyAdapterException("Error - Illegal domain of interpolation target.");                          &_source, 0, 0, 0, &_d_dirac, 0, &_y_dirac);
890        checkFinleyError();
891     Finley_Mesh* mesh=m_finleyMesh.get();  }
892     escriptDataC _target=target.getDataC();  
893     escriptDataC _in=in.getDataC();  //
894     switch(in.getFunctionSpace().getTypeCode()) {  // interpolates data between different function spaces
895     case(Nodes):  //
896        switch(target.getFunctionSpace().getTypeCode()) {  void MeshAdapter::interpolateOnDomain(escript::Data& target, const escript::Data& in) const
897        case(Nodes):  {
898        case(ReducedNodes):      const MeshAdapter& inDomain=dynamic_cast<const MeshAdapter&>(*(in.getFunctionSpace().getDomain()));
899        case(DegreesOfFreedom):      const MeshAdapter& targetDomain=dynamic_cast<const MeshAdapter&>(*(target.getFunctionSpace().getDomain()));
900        case(ReducedDegreesOfFreedom):      if (inDomain!=*this)
901        Finley_Assemble_CopyNodalData(mesh->Nodes,&_target,&_in);          throw FinleyAdapterException("Error - Illegal domain of interpolant.");
902        break;      if (targetDomain!=*this)
903        case(Elements):          throw FinleyAdapterException("Error - Illegal domain of interpolation target.");
904        case(ReducedElements):  
905        Finley_Assemble_interpolate(mesh->Nodes,mesh->Elements,&_in,&_target);      Finley_Mesh* mesh=m_finleyMesh.get();
906        break;      escriptDataC _target=target.getDataC();
907        case(FaceElements):      escriptDataC _in=in.getDataC();
908        case(ReducedFaceElements):      switch(in.getFunctionSpace().getTypeCode()) {
909        Finley_Assemble_interpolate(mesh->Nodes,mesh->FaceElements,&_in,&_target);          case Nodes:
910        break;              switch(target.getFunctionSpace().getTypeCode()) {
911        case(Points):                  case Nodes:
912        Finley_Assemble_interpolate(mesh->Nodes,mesh->Points,&_in,&_target);                  case ReducedNodes:
913        break;                  case DegreesOfFreedom:
914        case(ContactElementsZero):                  case ReducedDegreesOfFreedom:
915        case(ReducedContactElementsZero):                      Finley_Assemble_CopyNodalData(mesh->Nodes,&_target,&_in);
916        Finley_Assemble_interpolate(mesh->Nodes,mesh->ContactElements,&_in,&_target);                      break;
917        break;                  case Elements:
918        case(ContactElementsOne):                  case ReducedElements:
919        case(ReducedContactElementsOne):                      Finley_Assemble_interpolate(mesh->Nodes,mesh->Elements,&_in,&_target);
920        Finley_Assemble_interpolate(mesh->Nodes,mesh->ContactElements,&_in,&_target);                      break;
921        break;                  case FaceElements:
922        default:                  case ReducedFaceElements:
923           stringstream temp;                      Finley_Assemble_interpolate(mesh->Nodes,mesh->FaceElements,&_in,&_target);
924           temp << "Error - Interpolation on Domain: Finley does not know anything about function space type " << target.getFunctionSpace().getTypeCode();                      break;
925           throw FinleyAdapterException(temp.str());                  case Points:
926           break;                      Finley_Assemble_interpolate(mesh->Nodes,mesh->Points,&_in,&_target);
927        }                      break;
928        break;                  case ContactElementsZero:
929     case(ReducedNodes):                  case ReducedContactElementsZero:
930        switch(target.getFunctionSpace().getTypeCode()) {                  case ContactElementsOne:
931        case(Nodes):                  case ReducedContactElementsOne:
932        case(ReducedNodes):                      Finley_Assemble_interpolate(mesh->Nodes,mesh->ContactElements,&_in,&_target);
933        case(DegreesOfFreedom):                      break;
934        case(ReducedDegreesOfFreedom):                  default:
935        Finley_Assemble_CopyNodalData(mesh->Nodes,&_target,&_in);                      stringstream temp;
936        break;                      temp << "Error - Interpolation on Domain: Finley does not know anything about function space type " << target.getFunctionSpace().getTypeCode();
937        case(Elements):                      throw FinleyAdapterException(temp.str());
938        case(ReducedElements):                      break;
939        Finley_Assemble_interpolate(mesh->Nodes,mesh->Elements,&_in,&_target);              }
940        break;              break;
941        case(FaceElements):          case ReducedNodes:
942        case(ReducedFaceElements):              switch(target.getFunctionSpace().getTypeCode()) {
943        Finley_Assemble_interpolate(mesh->Nodes,mesh->FaceElements,&_in,&_target);                  case Nodes:
944        break;                  case ReducedNodes:
945        case(Points):                  case DegreesOfFreedom:
946        Finley_Assemble_interpolate(mesh->Nodes,mesh->Points,&_in,&_target);                  case ReducedDegreesOfFreedom:
947        break;                      Finley_Assemble_CopyNodalData(mesh->Nodes,&_target,&_in);
948        case(ContactElementsZero):                      break;
949        case(ReducedContactElementsZero):                  case Elements:
950        Finley_Assemble_interpolate(mesh->Nodes,mesh->ContactElements,&_in,&_target);                  case ReducedElements:
951        break;                      Finley_Assemble_interpolate(mesh->Nodes,mesh->Elements,&_in,&_target);
952        case(ContactElementsOne):                      break;
953        case(ReducedContactElementsOne):                  case FaceElements:
954        Finley_Assemble_interpolate(mesh->Nodes,mesh->ContactElements,&_in,&_target);                  case ReducedFaceElements:
955        break;                      Finley_Assemble_interpolate(mesh->Nodes,mesh->FaceElements,&_in,&_target);
956        default:                      break;
957           stringstream temp;                  case Points:
958           temp << "Error - Interpolation on Domain: Finley does not know anything about function space type " << target.getFunctionSpace().getTypeCode();                      Finley_Assemble_interpolate(mesh->Nodes,mesh->Points,&_in,&_target);
959           throw FinleyAdapterException(temp.str());                      break;
960           break;                  case ContactElementsZero:
961        }                  case ReducedContactElementsZero:
962        break;                  case ContactElementsOne:
963     case(Elements):                  case ReducedContactElementsOne:
964        if (target.getFunctionSpace().getTypeCode()==Elements) {                      Finley_Assemble_interpolate(mesh->Nodes,mesh->ContactElements,&_in,&_target);
965           Finley_Assemble_CopyElementData(mesh->Elements,&_target,&_in);                      break;
966        } else if (target.getFunctionSpace().getTypeCode()==ReducedElements) {                  default:
967           Finley_Assemble_AverageElementData(mesh->Elements,&_target,&_in);                      stringstream temp;
968        } else {                      temp << "Error - Interpolation on Domain: Finley does not know anything about function space type " << target.getFunctionSpace().getTypeCode();
969           throw FinleyAdapterException("Error - No interpolation with data on elements possible.");                      throw FinleyAdapterException(temp.str());
970        }                      break;
971        break;              }
972     case(ReducedElements):              break;
973        if (target.getFunctionSpace().getTypeCode()==ReducedElements) {          case Elements:
974           Finley_Assemble_CopyElementData(mesh->Elements,&_target,&_in);              if (target.getFunctionSpace().getTypeCode()==Elements) {
975        } else {                  Finley_Assemble_CopyElementData(mesh->Elements,&_target,&_in);
976           throw FinleyAdapterException("Error - No interpolation with data on elements with reduced integration order possible.");              } else if (target.getFunctionSpace().getTypeCode()==ReducedElements) {
977        }                  Finley_Assemble_AverageElementData(mesh->Elements,&_target,&_in);
978        break;              } else {
979     case(FaceElements):                  throw FinleyAdapterException("Error - No interpolation with data on elements possible.");
980        if (target.getFunctionSpace().getTypeCode()==FaceElements) {              }
981           Finley_Assemble_CopyElementData(mesh->FaceElements,&_target,&_in);              break;
982        } else if (target.getFunctionSpace().getTypeCode()==ReducedFaceElements) {          case ReducedElements:
983           Finley_Assemble_AverageElementData(mesh->FaceElements,&_target,&_in);              if (target.getFunctionSpace().getTypeCode()==ReducedElements) {
984        } else {                  Finley_Assemble_CopyElementData(mesh->Elements,&_target,&_in);
985           throw FinleyAdapterException("Error - No interpolation with data on face elements possible.");              } else {
986        }                  throw FinleyAdapterException("Error - No interpolation with data on elements with reduced integration order possible.");
987        break;              }
988     case(ReducedFaceElements):              break;
989        if (target.getFunctionSpace().getTypeCode()==ReducedFaceElements) {          case FaceElements:
990           Finley_Assemble_CopyElementData(mesh->FaceElements,&_target,&_in);              if (target.getFunctionSpace().getTypeCode()==FaceElements) {
991        } else {                  Finley_Assemble_CopyElementData(mesh->FaceElements,&_target,&_in);
992           throw FinleyAdapterException("Error - No interpolation with data on face elements with reduced integration order possible.");              } else if (target.getFunctionSpace().getTypeCode()==ReducedFaceElements) {
993        }                  Finley_Assemble_AverageElementData(mesh->FaceElements,&_target,&_in);
994        break;              } else {
995     case(Points):                  throw FinleyAdapterException("Error - No interpolation with data on face elements possible.");
996        if (target.getFunctionSpace().getTypeCode()==Points) {              }
997           Finley_Assemble_CopyElementData(mesh->Points,&_target,&_in);              break;
998        } else {          case ReducedFaceElements:
999           throw FinleyAdapterException("Error - No interpolation with data on points possible.");              if (target.getFunctionSpace().getTypeCode()==ReducedFaceElements) {
1000        }                  Finley_Assemble_CopyElementData(mesh->FaceElements,&_target,&_in);
1001        break;              } else {
1002     case(ContactElementsZero):                  throw FinleyAdapterException("Error - No interpolation with data on face elements with reduced integration order possible.");
1003     case(ContactElementsOne):              }
1004        if (target.getFunctionSpace().getTypeCode()==ContactElementsZero || target.getFunctionSpace().getTypeCode()==ContactElementsOne) {              break;
1005           Finley_Assemble_CopyElementData(mesh->ContactElements,&_target,&_in);          case Points:
1006        } else if (target.getFunctionSpace().getTypeCode()==ReducedContactElementsZero || target.getFunctionSpace().getTypeCode()==ReducedContactElementsOne) {              if (target.getFunctionSpace().getTypeCode()==Points) {
1007           Finley_Assemble_AverageElementData(mesh->ContactElements,&_target,&_in);                  Finley_Assemble_CopyElementData(mesh->Points,&_target,&_in);
1008        } else {              } else {
1009           throw FinleyAdapterException("Error - No interpolation with data on contact elements possible.");                  throw FinleyAdapterException("Error - No interpolation with data on points possible.");
1010        }              }
1011        break;              break;
1012     case(ReducedContactElementsZero):          case ContactElementsZero:
1013     case(ReducedContactElementsOne):          case ContactElementsOne:
1014        if (target.getFunctionSpace().getTypeCode()==ReducedContactElementsZero || target.getFunctionSpace().getTypeCode()==ReducedContactElementsOne) {              if (target.getFunctionSpace().getTypeCode()==ContactElementsZero || target.getFunctionSpace().getTypeCode()==ContactElementsOne) {
1015           Finley_Assemble_CopyElementData(mesh->ContactElements,&_target,&_in);                  Finley_Assemble_CopyElementData(mesh->ContactElements,&_target,&_in);
1016        } else {              } else if (target.getFunctionSpace().getTypeCode()==ReducedContactElementsZero || target.getFunctionSpace().getTypeCode()==ReducedContactElementsOne) {
1017           throw FinleyAdapterException("Error - No interpolation with data on contact elements with reduced integration order possible.");                  Finley_Assemble_AverageElementData(mesh->ContactElements,&_target,&_in);
1018        }              } else {
1019        break;                  throw FinleyAdapterException("Error - No interpolation with data on contact elements possible.");
1020     case(DegreesOfFreedom):                    }
1021        switch(target.getFunctionSpace().getTypeCode()) {              break;
1022        case(ReducedDegreesOfFreedom):          case ReducedContactElementsZero:
1023        case(DegreesOfFreedom):          case ReducedContactElementsOne:
1024        Finley_Assemble_CopyNodalData(mesh->Nodes,&_target,&_in);              if (target.getFunctionSpace().getTypeCode()==ReducedContactElementsZero || target.getFunctionSpace().getTypeCode()==ReducedContactElementsOne) {
1025        break;                  Finley_Assemble_CopyElementData(mesh->ContactElements,&_target,&_in);
1026                  } else {
1027        case(Nodes):                  throw FinleyAdapterException("Error - No interpolation with data on contact elements with reduced integration order possible.");
1028        case(ReducedNodes):              }
1029        if (getMPISize()>1) {              break;
1030           escript::Data temp=escript::Data(in);          case DegreesOfFreedom:
1031           temp.expand();              switch(target.getFunctionSpace().getTypeCode()) {
1032           escriptDataC _in2 = temp.getDataC();                  case ReducedDegreesOfFreedom:
1033           Finley_Assemble_CopyNodalData(mesh->Nodes,&_target,&_in2);                  case DegreesOfFreedom:
1034        } else {                      Finley_Assemble_CopyNodalData(mesh->Nodes,&_target,&_in);
1035           Finley_Assemble_CopyNodalData(mesh->Nodes,&_target,&_in);                      break;
1036        }  
1037        break;                  case Nodes:
1038        case(Elements):                  case ReducedNodes:
1039        case(ReducedElements):                      if (getMPISize()>1) {
1040        if (getMPISize()>1) {                          escript::Data temp=escript::Data(in);
1041           escript::Data temp=escript::Data( in,  continuousFunction(asAbstractContinuousDomain()) );                          temp.expand();
1042           escriptDataC _in2 = temp.getDataC();                          escriptDataC _in2 = temp.getDataC();
1043           Finley_Assemble_interpolate(mesh->Nodes,mesh->Elements,&_in2,&_target);                          Finley_Assemble_CopyNodalData(mesh->Nodes,&_target,&_in2);
1044        } else {                      } else {
1045           Finley_Assemble_interpolate(mesh->Nodes,mesh->Elements,&_in,&_target);                          Finley_Assemble_CopyNodalData(mesh->Nodes,&_target,&_in);
1046        }                      }
1047        break;                      break;
1048        case(FaceElements):                  case Elements:
1049        case(ReducedFaceElements):                  case ReducedElements:
1050        if (getMPISize()>1) {                      if (getMPISize()>1) {
1051           escript::Data temp=escript::Data( in,  continuousFunction(asAbstractContinuousDomain()) );                          escript::Data temp=escript::Data(in, continuousFunction(*this));
1052           escriptDataC _in2 = temp.getDataC();                          escriptDataC _in2 = temp.getDataC();
1053           Finley_Assemble_interpolate(mesh->Nodes,mesh->FaceElements,&_in2,&_target);                          Finley_Assemble_interpolate(mesh->Nodes,mesh->Elements,&_in2,&_target);
1054                          } else {
1055        } else {                          Finley_Assemble_interpolate(mesh->Nodes,mesh->Elements,&_in,&_target);
1056           Finley_Assemble_interpolate(mesh->Nodes,mesh->FaceElements,&_in,&_target);                      }
1057        }                      break;
1058        break;                  case FaceElements:
1059        case(Points):                  case ReducedFaceElements:
1060        if (getMPISize()>1) {                      if (getMPISize()>1) {
1061           escript::Data temp=escript::Data( in,  continuousFunction(asAbstractContinuousDomain()) );                          escript::Data temp=escript::Data(in, continuousFunction(*this));
1062           escriptDataC _in2 = temp.getDataC();                          escriptDataC _in2 = temp.getDataC();
1063        } else {                          Finley_Assemble_interpolate(mesh->Nodes,mesh->FaceElements,&_in2,&_target);
1064           Finley_Assemble_interpolate(mesh->Nodes,mesh->Points,&_in,&_target);                      } else {
1065        }                          Finley_Assemble_interpolate(mesh->Nodes,mesh->FaceElements,&_in,&_target);
1066        break;                      }
1067        case(ContactElementsZero):                      break;
1068        case(ContactElementsOne):                  case Points:
1069        case(ReducedContactElementsZero):                      if (getMPISize()>1) {
1070        case(ReducedContactElementsOne):                          //escript::Data temp=escript::Data(in, continuousFunction(*this) );
1071        if (getMPISize()>1) {                          //escriptDataC _in2 = temp.getDataC();
1072           escript::Data temp=escript::Data( in,  continuousFunction(asAbstractContinuousDomain()) );                      } else {
1073           escriptDataC _in2 = temp.getDataC();                          Finley_Assemble_interpolate(mesh->Nodes,mesh->Points,&_in,&_target);
1074           Finley_Assemble_interpolate(mesh->Nodes,mesh->ContactElements,&_in2,&_target);                      }
1075        } else {                      break;
1076           Finley_Assemble_interpolate(mesh->Nodes,mesh->ContactElements,&_in,&_target);                  case ContactElementsZero:
1077        }                  case ContactElementsOne:
1078        break;                  case ReducedContactElementsZero:
1079        default:                  case ReducedContactElementsOne:
1080           stringstream temp;                      if (getMPISize()>1) {
1081           temp << "Error - Interpolation On Domain: Finley does not know anything about function space type " << target.getFunctionSpace().getTypeCode();                          escript::Data temp=escript::Data(in, continuousFunction(*this));
1082           throw FinleyAdapterException(temp.str());                          escriptDataC _in2 = temp.getDataC();
1083           break;                          Finley_Assemble_interpolate(mesh->Nodes,mesh->ContactElements,&_in2,&_target);
1084        }                      } else {
1085        break;                          Finley_Assemble_interpolate(mesh->Nodes,mesh->ContactElements,&_in,&_target);
1086     case(ReducedDegreesOfFreedom):                      }
1087        switch(target.getFunctionSpace().getTypeCode()) {                      break;
1088        case(Nodes):                  default:
1089        throw FinleyAdapterException("Error - Finley does not support interpolation from reduced degrees of freedom to mesh nodes.");                      stringstream temp;
1090        break;                      temp << "Error - Interpolation On Domain: Finley does not know anything about function space type " << target.getFunctionSpace().getTypeCode();
1091        case(ReducedNodes):                      throw FinleyAdapterException(temp.str());
1092        if (getMPISize()>1) {                      break;
1093           escript::Data temp=escript::Data(in);              }
1094           temp.expand();              break;
1095           escriptDataC _in2 = temp.getDataC();          case ReducedDegreesOfFreedom:
1096           Finley_Assemble_CopyNodalData(mesh->Nodes,&_target,&_in2);              switch(target.getFunctionSpace().getTypeCode()) {
1097        } else {                  case Nodes:
1098           Finley_Assemble_CopyNodalData(mesh->Nodes,&_target,&_in);                      throw FinleyAdapterException("Error - Finley does not support interpolation from reduced degrees of freedom to mesh nodes.");
1099        }                      break;
1100        break;                  case ReducedNodes:
1101        case(DegreesOfFreedom):                      if (getMPISize()>1) {
1102        throw FinleyAdapterException("Error - Finley does not support interpolation from reduced degrees of freedom to degrees of freedom");                          escript::Data temp=escript::Data(in);
1103        break;                          temp.expand();
1104        case(ReducedDegreesOfFreedom):                          escriptDataC _in2 = temp.getDataC();
1105        Finley_Assemble_CopyNodalData(mesh->Nodes,&_target,&_in);                          Finley_Assemble_CopyNodalData(mesh->Nodes,&_target,&_in2);
1106        break;                      } else {
1107        case(Elements):                          Finley_Assemble_CopyNodalData(mesh->Nodes,&_target,&_in);
1108        case(ReducedElements):                      }
1109        if (getMPISize()>1) {                      break;
1110           escript::Data temp=escript::Data( in,  reducedContinuousFunction(asAbstractContinuousDomain()) );                  case DegreesOfFreedom:
1111           escriptDataC _in2 = temp.getDataC();                      throw FinleyAdapterException("Error - Finley does not support interpolation from reduced degrees of freedom to degrees of freedom");
1112           Finley_Assemble_interpolate(mesh->Nodes,mesh->Elements,&_in2,&_target);                      break;
1113        } else {                  case ReducedDegreesOfFreedom:
1114           Finley_Assemble_interpolate(mesh->Nodes,mesh->Elements,&_in,&_target);                      Finley_Assemble_CopyNodalData(mesh->Nodes,&_target,&_in);
1115        }                      break;
1116        break;                  case Elements:
1117        case(FaceElements):                  case ReducedElements:
1118        case(ReducedFaceElements):                      if (getMPISize()>1) {
1119        if (getMPISize()>1) {                          escript::Data temp=escript::Data(in, reducedContinuousFunction(*this) );
1120           escript::Data temp=escript::Data( in,  reducedContinuousFunction(asAbstractContinuousDomain()) );                          escriptDataC _in2 = temp.getDataC();
1121           escriptDataC _in2 = temp.getDataC();                          Finley_Assemble_interpolate(mesh->Nodes,mesh->Elements,&_in2,&_target);
1122           Finley_Assemble_interpolate(mesh->Nodes,mesh->FaceElements,&_in2,&_target);                      } else {
1123        } else {                          Finley_Assemble_interpolate(mesh->Nodes,mesh->Elements,&_in,&_target);
1124           Finley_Assemble_interpolate(mesh->Nodes,mesh->FaceElements,&_in,&_target);                      }
1125        }                      break;
1126        break;                  case FaceElements:
1127        case(Points):                  case ReducedFaceElements:
1128        if (getMPISize()>1) {                      if (getMPISize()>1) {
1129           escript::Data temp=escript::Data( in,  reducedContinuousFunction(asAbstractContinuousDomain()) );                          escript::Data temp=escript::Data(in, reducedContinuousFunction(*this) );
1130           escriptDataC _in2 = temp.getDataC();                          escriptDataC _in2 = temp.getDataC();
1131           Finley_Assemble_interpolate(mesh->Nodes,mesh->Points,&_in2,&_target);                          Finley_Assemble_interpolate(mesh->Nodes,mesh->FaceElements,&_in2,&_target);
1132        } else {                      } else {
1133           Finley_Assemble_interpolate(mesh->Nodes,mesh->Points,&_in,&_target);                          Finley_Assemble_interpolate(mesh->Nodes,mesh->FaceElements,&_in,&_target);
1134        }                      }
1135        break;                      break;
1136        case(ContactElementsZero):                  case Points:
1137        case(ContactElementsOne):                      if (getMPISize()>1) {
1138        case(ReducedContactElementsZero):                          escript::Data temp=escript::Data(in, reducedContinuousFunction(*this));
1139        case(ReducedContactElementsOne):                          escriptDataC _in2 = temp.getDataC();
1140        if (getMPISize()>1) {                          Finley_Assemble_interpolate(mesh->Nodes,mesh->Points,&_in2,&_target);
1141           escript::Data temp=escript::Data( in,  reducedContinuousFunction(asAbstractContinuousDomain()) );                      } else {
1142           escriptDataC _in2 = temp.getDataC();                          Finley_Assemble_interpolate(mesh->Nodes,mesh->Points,&_in,&_target);
1143           Finley_Assemble_interpolate(mesh->Nodes,mesh->ContactElements,&_in2,&_target);                      }
1144        } else {                      break;
1145           Finley_Assemble_interpolate(mesh->Nodes,mesh->ContactElements,&_in,&_target);                  case ContactElementsZero:
1146        }                  case ContactElementsOne:
1147        break;                  case ReducedContactElementsZero:
1148        default:                  case ReducedContactElementsOne:
1149           stringstream temp;                      if (getMPISize()>1) {
1150           temp << "Error - Interpolation On Domain: Finley does not know anything about function space type " << target.getFunctionSpace().getTypeCode();                          escript::Data temp=escript::Data(in, reducedContinuousFunction(*this));
1151           throw FinleyAdapterException(temp.str());                          escriptDataC _in2 = temp.getDataC();
1152           break;                          Finley_Assemble_interpolate(mesh->Nodes,mesh->ContactElements,&_in2,&_target);
1153        }                      } else {
1154        break;                          Finley_Assemble_interpolate(mesh->Nodes,mesh->ContactElements,&_in,&_target);
1155     default:                      }
1156        stringstream temp;                      break;
1157        temp << "Error - Interpolation On Domain: Finley does not know anything about function space type %d" << in.getFunctionSpace().getTypeCode();                  default:
1158        throw FinleyAdapterException(temp.str());                      stringstream temp;
1159        break;                      temp << "Error - Interpolation On Domain: Finley does not know anything about function space type " << target.getFunctionSpace().getTypeCode();
1160     }                      throw FinleyAdapterException(temp.str());
1161     checkFinleyError();                      break;
1162                }
1163                break;
1164            default:
1165                stringstream temp;
1166                temp << "Error - Interpolation On Domain: Finley does not know anything about function space type %d" << in.getFunctionSpace().getTypeCode();
1167                throw FinleyAdapterException(temp.str());
1168                break;
1169        }
1170        checkFinleyError();
1171  }  }
1172    
1173  //  //
1174  // copies the locations of sample points into x:  // copies the locations of sample points into x
1175  //  //
1176  void MeshAdapter::setToX(escript::Data& arg) const  void MeshAdapter::setToX(escript::Data& arg) const
1177  {  {
1178     const MeshAdapter& argDomain=dynamic_cast<const MeshAdapter&>(*(arg.getFunctionSpace().getDomain()));      const MeshAdapter& argDomain=dynamic_cast<const MeshAdapter&>(*(arg.getFunctionSpace().getDomain()));
1179     if (argDomain!=*this)      if (argDomain!=*this)
1180        throw FinleyAdapterException("Error - Illegal domain of data point locations");          throw FinleyAdapterException("Error - Illegal domain of data point locations");
1181     Finley_Mesh* mesh=m_finleyMesh.get();      Finley_Mesh* mesh=m_finleyMesh.get();
1182     // in case of values node coordinates we can do the job directly:      // in case of appropriate function space we can do the job directly:
1183     if (arg.getFunctionSpace().getTypeCode()==Nodes) {      if (arg.getFunctionSpace().getTypeCode()==Nodes) {
1184        escriptDataC _arg=arg.getDataC();          escriptDataC _arg=arg.getDataC();
1185        Finley_Assemble_NodeCoordinates(mesh->Nodes,&_arg);          Finley_Assemble_NodeCoordinates(mesh->Nodes, &_arg);
1186     } else {      } else {
1187        escript::Data tmp_data=Vector(0.0,continuousFunction(asAbstractContinuousDomain()),true);          escript::Data tmp_data=Vector(0., continuousFunction(*this), true);
1188        escriptDataC _tmp_data=tmp_data.getDataC();          escriptDataC _tmp_data=tmp_data.getDataC();
1189        Finley_Assemble_NodeCoordinates(mesh->Nodes,&_tmp_data);          Finley_Assemble_NodeCoordinates(mesh->Nodes, &_tmp_data);
1190        // this is then interpolated onto arg:          // this is then interpolated onto arg:
1191        interpolateOnDomain(arg,tmp_data);          interpolateOnDomain(arg, tmp_data);
1192     }      }
1193     checkFinleyError();      checkFinleyError();
1194  }  }
1195    
1196  //  //
1197  // return the normal vectors at the location of data points as a Data object:  // return the normal vectors at the location of data points as a Data object
1198  //  //
1199  void MeshAdapter::setToNormal(escript::Data& normal) const  void MeshAdapter::setToNormal(escript::Data& normal) const
1200  {  {
1201  /*   const MeshAdapter& normalDomain=dynamic_cast<const MeshAdapter&>(normal.getFunctionSpace().getDomain());*/      const MeshAdapter& normalDomain=dynamic_cast<const MeshAdapter&>(*(normal.getFunctionSpace().getDomain()));
1202     const MeshAdapter& normalDomain=dynamic_cast<const MeshAdapter&>(*(normal.getFunctionSpace().getDomain()));      if (normalDomain!=*this)
1203     if (normalDomain!=*this)          throw FinleyAdapterException("Error - Illegal domain of normal locations");
1204        throw FinleyAdapterException("Error - Illegal domain of normal locations");      Finley_Mesh* mesh=m_finleyMesh.get();
1205     Finley_Mesh* mesh=m_finleyMesh.get();      escriptDataC _normal=normal.getDataC();
1206     escriptDataC _normal=normal.getDataC();      switch(normal.getFunctionSpace().getTypeCode()) {
1207     switch(normal.getFunctionSpace().getTypeCode()) {          case Nodes:
1208     case(Nodes):              throw FinleyAdapterException("Error - Finley does not support surface normal vectors for nodes");
1209     throw FinleyAdapterException("Error - Finley does not support surface normal vectors for nodes");              break;
1210     break;          case ReducedNodes:
1211     case(ReducedNodes):              throw FinleyAdapterException("Error - Finley does not support surface normal vectors for reduced nodes");
1212     throw FinleyAdapterException("Error - Finley does not support surface normal vectors for reduced nodes");              break;
1213     break;          case Elements:
1214     case(Elements):              throw FinleyAdapterException("Error - Finley does not support surface normal vectors for elements");
1215     throw FinleyAdapterException("Error - Finley does not support surface normal vectors for elements");              break;
1216     break;          case ReducedElements:
1217     case(ReducedElements):              throw FinleyAdapterException("Error - Finley does not support surface normal vectors for elements with reduced integration order");
1218     throw FinleyAdapterException("Error - Finley does not support surface normal vectors for elements with reduced integration order");              break;
1219     break;          case FaceElements:
1220     case (FaceElements):          case ReducedFaceElements:
1221     Finley_Assemble_setNormal(mesh->Nodes,mesh->FaceElements,&_normal);              Finley_Assemble_setNormal(mesh->Nodes,mesh->FaceElements,&_normal);
1222     break;              break;
1223     case (ReducedFaceElements):          case Points:
1224     Finley_Assemble_setNormal(mesh->Nodes,mesh->FaceElements,&_normal);              throw FinleyAdapterException("Error - Finley does not support surface normal vectors for point elements");
1225     break;              break;
1226     case(Points):          case ContactElementsOne:
1227     throw FinleyAdapterException("Error - Finley does not support surface normal vectors for point elements");          case ContactElementsZero:
1228     break;          case ReducedContactElementsOne:
1229     case (ContactElementsOne):          case ReducedContactElementsZero:
1230     case (ContactElementsZero):              Finley_Assemble_setNormal(mesh->Nodes,mesh->ContactElements,&_normal);
1231     Finley_Assemble_setNormal(mesh->Nodes,mesh->ContactElements,&_normal);              break;
1232     break;          case DegreesOfFreedom:
1233     case (ReducedContactElementsOne):              throw FinleyAdapterException("Error - Finley does not support surface normal vectors for degrees of freedom.");
1234     case (ReducedContactElementsZero):              break;
1235     Finley_Assemble_setNormal(mesh->Nodes,mesh->ContactElements,&_normal);          case ReducedDegreesOfFreedom:
1236     break;              throw FinleyAdapterException("Error - Finley does not support surface normal vectors for reduced degrees of freedom.");
1237     case(DegreesOfFreedom):              break;
1238     throw FinleyAdapterException("Error - Finley does not support surface normal vectors for degrees of freedom.");          default:
1239     break;              stringstream temp;
1240     case(ReducedDegreesOfFreedom):              temp << "Error - Normal Vectors: Finley does not know anything about function space type " << normal.getFunctionSpace().getTypeCode();
1241     throw FinleyAdapterException("Error - Finley does not support surface normal vectors for reduced degrees of freedom.");              throw FinleyAdapterException(temp.str());
1242     break;              break;
1243     default:      }
1244        stringstream temp;      checkFinleyError();
       temp << "Error - Normal Vectors: Finley does not know anything about function space type " << normal.getFunctionSpace().getTypeCode();  
       throw FinleyAdapterException(temp.str());  
       break;  
    }  
    checkFinleyError();  
1245  }  }
1246    
1247  //  //
1248  // interpolates data to other domain:  // interpolates data to other domain
1249  //  //
1250  void MeshAdapter::interpolateACross(escript::Data& target,const escript::Data& source) const  void MeshAdapter::interpolateACross(escript::Data& target,const escript::Data& source) const
1251  {  {
1252     const_Domain_ptr targetDomain_p=target.getFunctionSpace().getDomain();      escript::const_Domain_ptr targetDomain_p=target.getFunctionSpace().getDomain();
1253     const MeshAdapter* targetDomain=dynamic_cast<const MeshAdapter*>(targetDomain_p.get());      const MeshAdapter* targetDomain=dynamic_cast<const MeshAdapter*>(targetDomain_p.get());
1254     if (targetDomain!=this)      if (targetDomain!=this)
1255        throw FinleyAdapterException("Error - Illegal domain of interpolation target");          throw FinleyAdapterException("Error - Illegal domain of interpolation target");
1256    
1257     throw FinleyAdapterException("Error - Finley does not allow interpolation across domains yet.");      throw FinleyAdapterException("Error - Finley does not allow interpolation across domains yet.");
1258  }  }
1259    
1260  //  //
1261  // calculates the integral of a function defined of arg:  // calculates the integral of a function defined on arg
1262  //  //
1263  void MeshAdapter::setToIntegrals(vector<double>& integrals,const escript::Data& arg) const  void MeshAdapter::setToIntegrals(vector<double>& integrals, const escript::Data& arg) const
1264  {  {
1265     const MeshAdapter& argDomain=dynamic_cast<const MeshAdapter&>(*(arg.getFunctionSpace().getDomain()));      const MeshAdapter& argDomain=dynamic_cast<const MeshAdapter&>(*(arg.getFunctionSpace().getDomain()));
1266     if (argDomain!=*this)      if (argDomain!=*this)
1267        throw FinleyAdapterException("Error - Illegal domain of integration kernel");          throw FinleyAdapterException("Error - Illegal domain of integration kernel");
1268    
1269     double blocktimer_start = blocktimer_time();      double blocktimer_start = blocktimer_time();
1270     Finley_Mesh* mesh=m_finleyMesh.get();      Finley_Mesh* mesh=m_finleyMesh.get();
1271     escriptDataC _temp;      escriptDataC _temp;
1272     escript::Data temp;      escript::Data temp;
1273     escriptDataC _arg=arg.getDataC();      escriptDataC _arg=arg.getDataC();
1274     switch(arg.getFunctionSpace().getTypeCode()) {      switch(arg.getFunctionSpace().getTypeCode()) {
1275     case(Nodes):          case Nodes:
1276     temp=escript::Data( arg, escript::function(asAbstractContinuousDomain()) );              temp=escript::Data(arg, escript::function(*this));
1277     _temp=temp.getDataC();              _temp=temp.getDataC();
1278     Finley_Assemble_integrate(mesh->Nodes,mesh->Elements,&_temp,&integrals[0]);              Finley_Assemble_integrate(mesh->Nodes, mesh->Elements, &_temp, &integrals[0]);
1279     break;              break;
1280     case(ReducedNodes):          case ReducedNodes:
1281     temp=escript::Data( arg, escript::function(asAbstractContinuousDomain()) );              temp=escript::Data( arg, escript::function(*this) );
1282     _temp=temp.getDataC();              _temp=temp.getDataC();
1283     Finley_Assemble_integrate(mesh->Nodes,mesh->Elements,&_temp,&integrals[0]);              Finley_Assemble_integrate(mesh->Nodes,mesh->Elements,&_temp,&integrals[0]);
1284     break;              break;
1285     case(Elements):          case Elements:
1286     Finley_Assemble_integrate(mesh->Nodes,mesh->Elements,&_arg,&integrals[0]);          case ReducedElements:
1287     break;              Finley_Assemble_integrate(mesh->Nodes,mesh->Elements,&_arg,&integrals[0]);
1288     case(ReducedElements):              break;
1289     Finley_Assemble_integrate(mesh->Nodes,mesh->Elements,&_arg,&integrals[0]);          case FaceElements:
1290     break;          case ReducedFaceElements:
1291     case(FaceElements):              Finley_Assemble_integrate(mesh->Nodes,mesh->FaceElements,&_arg,&integrals[0]);
1292     Finley_Assemble_integrate(mesh->Nodes,mesh->FaceElements,&_arg,&integrals[0]);              break;
1293     break;          case Points:
1294     case(ReducedFaceElements):              throw FinleyAdapterException("Error - Integral of data on points is not supported.");
1295     Finley_Assemble_integrate(mesh->Nodes,mesh->FaceElements,&_arg,&integrals[0]);              break;
1296     break;          case ContactElementsZero:
1297     case(Points):          case ReducedContactElementsZero:
1298     throw FinleyAdapterException("Error - Integral of data on points is not supported.");          case ContactElementsOne:
1299     break;          case ReducedContactElementsOne:
1300     case(ContactElementsZero):              Finley_Assemble_integrate(mesh->Nodes,mesh->ContactElements,&_arg,&integrals[0]);
1301     Finley_Assemble_integrate(mesh->Nodes,mesh->ContactElements,&_arg,&integrals[0]);              break;
1302     break;          case DegreesOfFreedom:
1303     case(ReducedContactElementsZero):          case ReducedDegreesOfFreedom:
1304     Finley_Assemble_integrate(mesh->Nodes,mesh->ContactElements,&_arg,&integrals[0]);              temp=escript::Data(arg, escript::function(*this));
1305     break;              _temp=temp.getDataC();
1306     case(ContactElementsOne):              Finley_Assemble_integrate(mesh->Nodes,mesh->Elements,&_temp,&integrals[0]);
1307     Finley_Assemble_integrate(mesh->Nodes,mesh->ContactElements,&_arg,&integrals[0]);              break;
1308     break;          default:
1309     case(ReducedContactElementsOne):              stringstream temp;
1310     Finley_Assemble_integrate(mesh->Nodes,mesh->ContactElements,&_arg,&integrals[0]);              temp << "Error - Integrals: Finley does not know anything about function space type " << arg.getFunctionSpace().getTypeCode();
1311     break;              throw FinleyAdapterException(temp.str());
1312     case(DegreesOfFreedom):              break;
1313     temp=escript::Data( arg, escript::function(asAbstractContinuousDomain()) );      }
1314     _temp=temp.getDataC();      checkFinleyError();
1315     Finley_Assemble_integrate(mesh->Nodes,mesh->Elements,&_temp,&integrals[0]);      blocktimer_increment("integrate()", blocktimer_start);
1316     break;  }
1317     case(ReducedDegreesOfFreedom):  
1318     temp=escript::Data( arg, escript::function(asAbstractContinuousDomain()) );  //
1319     _temp=temp.getDataC();  // calculates the gradient of arg
1320     Finley_Assemble_integrate(mesh->Nodes,mesh->Elements,&_temp,&integrals[0]);  //
1321     break;  void MeshAdapter::setToGradient(escript::Data& grad, const escript::Data& arg) const
1322     default:  {
1323        stringstream temp;      const MeshAdapter& argDomain=dynamic_cast<const MeshAdapter&>(*(arg.getFunctionSpace().getDomain()));
1324        temp << "Error - Integrals: Finley does not know anything about function space type " << arg.getFunctionSpace().getTypeCode();      if (argDomain!=*this)
1325        throw FinleyAdapterException(temp.str());          throw FinleyAdapterException("Error - Illegal domain of gradient argument");
1326        break;      const MeshAdapter& gradDomain=dynamic_cast<const MeshAdapter&>(*(grad.getFunctionSpace().getDomain()));
1327     }      if (gradDomain!=*this)
1328     checkFinleyError();          throw FinleyAdapterException("Error - Illegal domain of gradient");
1329     blocktimer_increment("integrate()", blocktimer_start);  
1330  }      Finley_Mesh* mesh=m_finleyMesh.get();
1331        escriptDataC _grad=grad.getDataC();
1332  //      escriptDataC nodeDataC;
1333  // calculates the gradient of arg:      escript::Data temp;
1334  //      if (getMPISize()>1) {
1335  void MeshAdapter::setToGradient(escript::Data& grad,const escript::Data& arg) const          if (arg.getFunctionSpace().getTypeCode() == DegreesOfFreedom) {
1336  {              temp=escript::Data(arg, continuousFunction(*this));
1337     const MeshAdapter& argDomain=dynamic_cast<const MeshAdapter&>(*(arg.getFunctionSpace().getDomain()));              nodeDataC = temp.getDataC();
1338     if (argDomain!=*this)          } else if(arg.getFunctionSpace().getTypeCode() == ReducedDegreesOfFreedom) {
1339        throw FinleyAdapterException("Error - Illegal domain of gradient argument");              temp=escript::Data(arg, reducedContinuousFunction(*this));
1340     const MeshAdapter& gradDomain=dynamic_cast<const MeshAdapter&>(*(grad.getFunctionSpace().getDomain()));              nodeDataC = temp.getDataC();
1341     if (gradDomain!=*this)          } else {
1342        throw FinleyAdapterException("Error - Illegal domain of gradient");              nodeDataC = arg.getDataC();
1343            }
1344     Finley_Mesh* mesh=m_finleyMesh.get();      } else {
1345     escriptDataC _grad=grad.getDataC();          nodeDataC = arg.getDataC();
1346     escriptDataC nodeDataC;      }
1347     escript::Data temp;      switch(grad.getFunctionSpace().getTypeCode()) {
1348     if (getMPISize()>1) {          case Nodes:
1349        if( arg.getFunctionSpace().getTypeCode() == DegreesOfFreedom ) {              throw FinleyAdapterException("Error - Gradient at nodes is not supported.");
1350           temp=escript::Data( arg,  continuousFunction(asAbstractContinuousDomain()) );              break;
1351           nodeDataC = temp.getDataC();          case ReducedNodes:
1352        } else if( arg.getFunctionSpace().getTypeCode() == ReducedDegreesOfFreedom ) {              throw FinleyAdapterException("Error - Gradient at reduced nodes is not supported.");
1353           temp=escript::Data( arg,  reducedContinuousFunction(asAbstractContinuousDomain()) );              break;
1354           nodeDataC = temp.getDataC();          case Elements:
1355        } else {          case ReducedElements:
1356           nodeDataC = arg.getDataC();              Finley_Assemble_gradient(mesh->Nodes,mesh->Elements,&_grad,&nodeDataC);
1357        }              break;
1358     } else {          case FaceElements:
1359        nodeDataC = arg.getDataC();          case ReducedFaceElements:
1360     }              Finley_Assemble_gradient(mesh->Nodes,mesh->FaceElements,&_grad,&nodeDataC);
1361     switch(grad.getFunctionSpace().getTypeCode()) {              break;
1362     case(Nodes):          case Points:
1363     throw FinleyAdapterException("Error - Gradient at nodes is not supported.");              throw FinleyAdapterException("Error - Gradient at points is not supported.");
1364     break;              break;
1365     case(ReducedNodes):          case ContactElementsZero:
1366     throw FinleyAdapterException("Error - Gradient at reduced nodes is not supported.");          case ReducedContactElementsZero:
1367     break;          case ContactElementsOne:
1368     case(Elements):          case ReducedContactElementsOne:
1369     Finley_Assemble_gradient(mesh->Nodes,mesh->Elements,&_grad,&nodeDataC);              Finley_Assemble_gradient(mesh->Nodes,mesh->ContactElements,&_grad,&nodeDataC);
1370     break;              break;
1371     case(ReducedElements):          case DegreesOfFreedom:
1372     Finley_Assemble_gradient(mesh->Nodes,mesh->Elements,&_grad,&nodeDataC);              throw FinleyAdapterException("Error - Gradient at degrees of freedom is not supported.");
1373     break;              break;
1374     case(FaceElements):          case ReducedDegreesOfFreedom:
1375     Finley_Assemble_gradient(mesh->Nodes,mesh->FaceElements,&_grad,&nodeDataC);              throw FinleyAdapterException("Error - Gradient at reduced degrees of freedom is not supported.");
1376     break;              break;
1377     case(ReducedFaceElements):          default:
1378     Finley_Assemble_gradient(mesh->Nodes,mesh->FaceElements,&_grad,&nodeDataC);              stringstream temp;
1379     break;              temp << "Error - Gradient: Finley does not know anything about function space type " << arg.getFunctionSpace().getTypeCode();
1380     case(Points):              throw FinleyAdapterException(temp.str());
1381     throw FinleyAdapterException("Error - Gradient at points is not supported.");              break;
1382     break;      }
1383     case(ContactElementsZero):      checkFinleyError();
    Finley_Assemble_gradient(mesh->Nodes,mesh->ContactElements,&_grad,&nodeDataC);  
    break;  
    case(ReducedContactElementsZero):  
    Finley_Assemble_gradient(mesh->Nodes,mesh->ContactElements,&_grad,&nodeDataC);  
    break;  
    case(ContactElementsOne):  
    Finley_Assemble_gradient(mesh->Nodes,mesh->ContactElements,&_grad,&nodeDataC);  
    break;  
    case(ReducedContactElementsOne):  
    Finley_Assemble_gradient(mesh->Nodes,mesh->ContactElements,&_grad,&nodeDataC);  
    break;  
    case(DegreesOfFreedom):  
    throw FinleyAdapterException("Error - Gradient at degrees of freedom is not supported.");  
    break;  
    case(ReducedDegreesOfFreedom):  
    throw FinleyAdapterException("Error - Gradient at reduced degrees of freedom is not supported.");  
    break;  
    default:  
       stringstream temp;  
       temp << "Error - Gradient: Finley does not know anything about function space type " << arg.getFunctionSpace().getTypeCode();  
       throw FinleyAdapterException(temp.str());  
       break;  
    }  
    checkFinleyError();  
1384  }  }
1385    
1386  //  //
1387  // returns the size of elements:  // returns the size of elements
1388  //  //
1389  void MeshAdapter::setToSize(escript::Data& size) const  void MeshAdapter::setToSize(escript::Data& size) const
1390  {  {
1391     Finley_Mesh* mesh=m_finleyMesh.get();      Finley_Mesh* mesh=m_finleyMesh.get();
1392     escriptDataC tmp=size.getDataC();      escriptDataC tmp=size.getDataC();
1393     switch(size.getFunctionSpace().getTypeCode()) {      switch(size.getFunctionSpace().getTypeCode()) {
1394     case(Nodes):          case Nodes:
1395     throw FinleyAdapterException("Error - Size of nodes is not supported.");              throw FinleyAdapterException("Error - Size of nodes is not supported.");
1396     break;              break;
1397     case(ReducedNodes):          case ReducedNodes:
1398     throw FinleyAdapterException("Error - Size of reduced nodes is not supported.");              throw FinleyAdapterException("Error - Size of reduced nodes is not supported.");
1399     break;              break;
1400     case(Elements):          case Elements:
1401     Finley_Assemble_getSize(mesh->Nodes,mesh->Elements,&tmp);          case ReducedElements:
1402     break;              Finley_Assemble_getSize(mesh->Nodes,mesh->Elements,&tmp);
1403     case(ReducedElements):              break;
1404     Finley_Assemble_getSize(mesh->Nodes,mesh->Elements,&tmp);          case FaceElements:
1405     break;          case ReducedFaceElements:
1406     case(FaceElements):              Finley_Assemble_getSize(mesh->Nodes,mesh->FaceElements,&tmp);
1407     Finley_Assemble_getSize(mesh->Nodes,mesh->FaceElements,&tmp);              break;
1408     break;          case Points:
1409     case(ReducedFaceElements):              throw FinleyAdapterException("Error - Size of point elements is not supported.");
1410     Finley_Assemble_getSize(mesh->Nodes,mesh->FaceElements,&tmp);              break;
1411     break;          case ContactElementsZero:
1412     case(Points):          case ContactElementsOne:
1413     throw FinleyAdapterException("Error - Size of point elements is not supported.");          case ReducedContactElementsZero:
1414     break;          case ReducedContactElementsOne:
1415     case(ContactElementsZero):              Finley_Assemble_getSize(mesh->Nodes,mesh->ContactElements,&tmp);
1416     case(ContactElementsOne):              break;
1417     Finley_Assemble_getSize(mesh->Nodes,mesh->ContactElements,&tmp);          case DegreesOfFreedom:
1418     break;              throw FinleyAdapterException("Error - Size of degrees of freedom is not supported.");
1419     case(ReducedContactElementsZero):              break;
1420     case(ReducedContactElementsOne):          case ReducedDegreesOfFreedom:
1421     Finley_Assemble_getSize(mesh->Nodes,mesh->ContactElements,&tmp);              throw FinleyAdapterException("Error - Size of reduced degrees of freedom is not supported.");
1422     break;              break;
1423     case(DegreesOfFreedom):          default:
1424     throw FinleyAdapterException("Error - Size of degrees of freedom is not supported.");              stringstream temp;
1425     break;              temp << "Error - Element size: Finley does not know anything about function space type " << size.getFunctionSpace().getTypeCode();
1426     case(ReducedDegreesOfFreedom):              throw FinleyAdapterException(temp.str());
1427     throw FinleyAdapterException("Error - Size of reduced degrees of freedom is not supported.");              break;
1428     break;      }
1429     default:      checkFinleyError();
       stringstream temp;  
       temp << "Error - Element size: Finley does not know anything about function space type " << size.getFunctionSpace().getTypeCode();  
       throw FinleyAdapterException(temp.str());  
       break;  
    }  
    checkFinleyError();  
1430  }  }
1431    
1432  //  //
# Line 1466  void MeshAdapter::setToSize(escript::Dat Line 1434  void MeshAdapter::setToSize(escript::Dat
1434  //  //
1435  void MeshAdapter::setNewX(const escript::Data& new_x)  void MeshAdapter::setNewX(const escript::Data& new_x)
1436  {  {
1437     Finley_Mesh* mesh=m_finleyMesh.get();      Finley_Mesh* mesh=m_finleyMesh.get();
1438     escriptDataC tmp;      const MeshAdapter& newDomain=dynamic_cast<const MeshAdapter&>(*(new_x.getFunctionSpace().getDomain()));
1439     const MeshAdapter& newDomain=dynamic_cast<const MeshAdapter&>(*(new_x.getFunctionSpace().getDomain()));      if (newDomain!=*this)
1440     if (newDomain!=*this)          throw FinleyAdapterException("Error - Illegal domain of new point locations");
1441        throw FinleyAdapterException("Error - Illegal domain of new point locations");      if (new_x.getFunctionSpace() == continuousFunction(*this)) {
1442     if ( new_x.getFunctionSpace() == continuousFunction(asAbstractContinuousDomain()) ) {          Finley_Mesh_setCoordinates(mesh, new_x);
1443         tmp = new_x.getDataC();      } else {
1444         Finley_Mesh_setCoordinates(mesh,&tmp);          throw FinleyAdapterException("As of escript version 3.3 SetX() only accepts ContinuousFunction arguments. Please interpolate.");
1445     } else {          //escript::Data new_x_inter=escript::Data(new_x, continuousFunction(*this));
1446         escript::Data new_x_inter=escript::Data( new_x,  continuousFunction(asAbstractContinuousDomain()) );          //Finley_Mesh_setCoordinates(mesh, new_x_inter);
1447         tmp = new_x_inter.getDataC();      }
1448         Finley_Mesh_setCoordinates(mesh,&tmp);      checkFinleyError();
    }  
    checkFinleyError();  
 }  
   
 //  
 // Helper for the save* methods. Extracts optional data variable names and the  
 // corresponding pointers from python dictionary. Caller must free arrays.  
 //  
 void MeshAdapter::extractArgsFromDict(const boost::python::dict& arg, int& numData, char**& names, escriptDataC*& data, escriptDataC**& dataPtr) const  
 {  
    numData = boost::python::extract<int>(arg.attr("__len__")());  
    /* win32 refactor */  
    names = (numData>0) ? TMPMEMALLOC(numData, char*) : (char**)NULL;  
    data = (numData>0) ? TMPMEMALLOC(numData,escriptDataC) : (escriptDataC*)NULL;  
    dataPtr = (numData>0) ? TMPMEMALLOC(numData,escriptDataC*) : (escriptDataC**)NULL;  
   
    boost::python::list keys=arg.keys();  
    for (int i=0; i<numData; ++i) {  
       string n=boost::python::extract<string>(keys[i]);  
       escript::Data& d=boost::python::extract<escript::Data&>(arg[keys[i]]);  
       if (dynamic_cast<const MeshAdapter&>(*(d.getFunctionSpace().getDomain())) !=*this)  
          throw FinleyAdapterException("Error: Data must be defined on same Domain");  
       data[i] = d.getDataC();  
       dataPtr[i] = &(data[i]);  
       names[i] = TMPMEMALLOC(n.length()+1, char);  
       strcpy(names[i], n.c_str());  
    }  
 }  
   
 //  
 // saves mesh and optionally data arrays in openDX format  
 //  
 void MeshAdapter::saveDX(const string& filename,const boost::python::dict& arg) const  
 {  
    int num_data;  
    char **names;  
    escriptDataC *data;  
    escriptDataC **ptr_data;  
   
    extractArgsFromDict(arg, num_data, names, data, ptr_data);  
    Finley_Mesh_saveDX(filename.c_str(), m_finleyMesh.get(), num_data, names, ptr_data);  
    checkFinleyError();  
   
    /* win32 refactor */  
    TMPMEMFREE(data);  
    TMPMEMFREE(ptr_data);  
    for(int i=0; i<num_data; i++)  
    {  
       TMPMEMFREE(names[i]);  
    }  
    TMPMEMFREE(names);  
   
    return;  
 }  
   
 //  
 // saves mesh and optionally data arrays in VTK format  
 //  
 void MeshAdapter::saveVTK(const string& filename,const boost::python::dict& arg,  const string& metadata, const string& metadata_schema) const  
 {  
    int num_data;  
    char **names;  
    escriptDataC *data;  
    escriptDataC **ptr_data;  
   
    extractArgsFromDict(arg, num_data, names, data, ptr_data);  
    Finley_Mesh_saveVTK(filename.c_str(), m_finleyMesh.get(), num_data, names, ptr_data, metadata.c_str(), metadata_schema.c_str());  
    checkFinleyError();  
   
    /* win32 refactor */  
    TMPMEMFREE(data);  
    TMPMEMFREE(ptr_data);  
    for(int i=0; i<num_data; i++)  
    {  
       TMPMEMFREE(names[i]);  
    }  
    TMPMEMFREE(names);  
1449  }  }
1450    
1451  bool MeshAdapter::ownSample(int fs_code, index_t id) const  bool MeshAdapter::ownSample(int fs_code, index_t id) const
1452  {  {
1453  #ifdef PASO_MPI      if (getMPISize() > 1) {
1454      index_t myFirstNode=0, myLastNode=0, k=0;  #ifdef ESYS_MPI
1455      index_t* globalNodeIndex=0;          index_t myFirstNode=0, myLastNode=0, k=0;
1456      Finley_Mesh* mesh_p=m_finleyMesh.get();          index_t* globalNodeIndex=0;
1457      if (fs_code == FINLEY_REDUCED_NODES)          Finley_Mesh* mesh_p=m_finleyMesh.get();
1458      {          /*
1459      myFirstNode = Finley_NodeFile_getFirstReducedNode(mesh_p->Nodes);           * this method is only used by saveDataCSV which would use the returned
1460      myLastNode = Finley_NodeFile_getLastReducedNode(mesh_p->Nodes);           * values for reduced nodes wrongly so this case is disabled for now
1461      globalNodeIndex = Finley_NodeFile_borrowGlobalReducedNodesIndex(mesh_p->Nodes);          if (fs_code == FINLEY_REDUCED_NODES) {
1462      }              myFirstNode = Finley_NodeFile_getFirstReducedNode(mesh_p->Nodes);
1463      else              myLastNode = Finley_NodeFile_getLastReducedNode(mesh_p->Nodes);
1464      {              globalNodeIndex = Finley_NodeFile_borrowGlobalReducedNodesIndex(mesh_p->Nodes);
1465      myFirstNode = Finley_NodeFile_getFirstNode(mesh_p->Nodes);          } else
1466      myLastNode = Finley_NodeFile_getLastNode(mesh_p->Nodes);          */
1467      globalNodeIndex = Finley_NodeFile_borrowGlobalNodesIndex(mesh_p->Nodes);          if (fs_code == FINLEY_NODES) {
1468      }              myFirstNode = mesh_p->Nodes->getFirstNode();
1469      k=globalNodeIndex[id];              myLastNode = mesh_p->Nodes->getLastNode();
1470      return static_cast<bool>( (myFirstNode <= k) && (k < myLastNode) );              globalNodeIndex = mesh_p->Nodes->borrowGlobalNodesIndex();
1471            } else {
1472                throw FinleyAdapterException("Unsupported function space type for ownSample()");
1473            }
1474    
1475            k=globalNodeIndex[id];
1476            return static_cast<bool>( (myFirstNode <= k) && (k < myLastNode) );
1477  #endif  #endif
1478        }
1479      return true;      return true;
1480  }  }
1481    
1482    
   
1483  //  //
1484  // creates a SystemMatrixAdapter stiffness matrix an initializes it with zeros  // creates a SystemMatrixAdapter stiffness matrix an initializes it with zeros
1485  //  //
1486  SystemMatrixAdapter MeshAdapter::newSystemMatrix(  escript::ASM_ptr MeshAdapter::newSystemMatrix(const int row_blocksize,
1487                                                   const int row_blocksize,                              const escript::FunctionSpace& row_functionspace,
1488                                                   const escript::FunctionSpace& row_functionspace,                              const int column_blocksize,
1489                                                   const int column_blocksize,                              const escript::FunctionSpace& column_functionspace,
1490                                                   const escript::FunctionSpace& column_functionspace,                              const int type) const
1491                                                   const int type) const  {
1492  {      // is the domain right?
1493     int reduceRowOrder=0;      const MeshAdapter& row_domain=dynamic_cast<const MeshAdapter&>(*(row_functionspace.getDomain()));
1494     int reduceColOrder=0;      if (row_domain!=*this)
1495     // is the domain right?          throw FinleyAdapterException("Error - domain of row function space does not match the domain of matrix generator.");
1496     const MeshAdapter& row_domain=dynamic_cast<const MeshAdapter&>(*(row_functionspace.getDomain()));      const MeshAdapter& col_domain=dynamic_cast<const MeshAdapter&>(*(column_functionspace.getDomain()));
1497     if (row_domain!=*this)      if (col_domain!=*this)
1498        throw FinleyAdapterException("Error - domain of row function space does not match the domain of matrix generator.");          throw FinleyAdapterException("Error - domain of column function space does not match the domain of matrix generator.");
1499     const MeshAdapter& col_domain=dynamic_cast<const MeshAdapter&>(*(column_functionspace.getDomain()));  
1500     if (col_domain!=*this)      int reduceRowOrder=0;
1501        throw FinleyAdapterException("Error - domain of columnn function space does not match the domain of matrix generator.");      int reduceColOrder=0;
1502     // is the function space type right      // is the function space type right?
1503     if (row_functionspace.getTypeCode()==DegreesOfFreedom) {      if (row_functionspace.getTypeCode() == ReducedDegreesOfFreedom) {
1504        reduceRowOrder=0;          reduceRowOrder=1;
1505     } else if (row_functionspace.getTypeCode()==ReducedDegreesOfFreedom) {      } else if (row_functionspace.getTypeCode() != DegreesOfFreedom) {
1506        reduceRowOrder=1;          throw FinleyAdapterException("Error - illegal function space type for system matrix rows.");
1507     } else {      }
1508        throw FinleyAdapterException("Error - illegal function space type for system matrix rows.");      if (column_functionspace.getTypeCode() == ReducedDegreesOfFreedom) {
1509     }          reduceColOrder=1;
1510     if (column_functionspace.getTypeCode()==DegreesOfFreedom) {      } else if (column_functionspace.getTypeCode() != DegreesOfFreedom) {
1511        reduceColOrder=0;          throw FinleyAdapterException("Error - illegal function space type for system matrix columns.");
1512     } else if (column_functionspace.getTypeCode()==ReducedDegreesOfFreedom) {      }
1513        reduceColOrder=1;  
1514     } else {      // generate matrix:
1515        throw FinleyAdapterException("Error - illegal function space type for system matrix columns.");      Paso_SystemMatrixPattern* fsystemMatrixPattern=Finley_getPattern(
1516     }              getFinley_Mesh(), reduceRowOrder, reduceColOrder);
1517     // generate matrix:      checkFinleyError();
1518        Paso_SystemMatrix* fsystemMatrix;
1519     Paso_SystemMatrixPattern* fsystemMatrixPattern=Finley_getPattern(getFinley_Mesh(),reduceRowOrder,reduceColOrder);      const int trilinos = 0;
1520     checkFinleyError();      if (trilinos) {
    Paso_SystemMatrix* fsystemMatrix;  
    int trilinos = 0;  
    if (trilinos) {  
1521  #ifdef TRILINOS  #ifdef TRILINOS
1522        /* Allocation Epetra_VrbMatrix here */          // FIXME: Allocation Epetra_VrbMatrix here...
1523  #endif  #endif
1524     }      } else {
1525     else {          fsystemMatrix=Paso_SystemMatrix_alloc(type, fsystemMatrixPattern,
1526        fsystemMatrix=Paso_SystemMatrix_alloc(type,fsystemMatrixPattern,row_blocksize,column_blocksize,FALSE);                  row_blocksize, column_blocksize, FALSE);
1527     }      }
1528     checkPasoError();      checkPasoError();
1529     Paso_SystemMatrixPattern_free(fsystemMatrixPattern);      Paso_SystemMatrixPattern_free(fsystemMatrixPattern);
1530     return SystemMatrixAdapter(fsystemMatrix,row_blocksize,row_functionspace,column_blocksize,column_functionspace);      SystemMatrixAdapter* sma=new SystemMatrixAdapter(fsystemMatrix, row_blocksize, row_functionspace, column_blocksize, column_functionspace);
1531        return escript::ASM_ptr(sma);
1532  }  }
1533    
1534  //  //
1535  // creates a TransportProblemAdapter  // creates a TransportProblemAdapter
1536  //  //
1537  TransportProblemAdapter MeshAdapter::newTransportProblem(  escript::ATP_ptr MeshAdapter::newTransportProblem(const int blocksize,
1538                                                           const bool useBackwardEuler,          const escript::FunctionSpace& functionspace, const int type) const
1539                                                           const int blocksize,  {
1540                                                           const escript::FunctionSpace& functionspace,      // is the domain right?
1541                                                           const int type) const      const MeshAdapter& domain=dynamic_cast<const MeshAdapter&>(*(functionspace.getDomain()));
1542  {      if (domain!=*this)
1543     int reduceOrder=0;          throw FinleyAdapterException("Error - domain of function space does not match the domain of transport problem generator.");
1544     // is the domain right?  
1545     const MeshAdapter& domain=dynamic_cast<const MeshAdapter&>(*(functionspace.getDomain()));      // is the function space type right?
1546     if (domain!=*this)      int reduceOrder=0;
1547        throw FinleyAdapterException("Error - domain of function space does not match the domain of transport problem generator.");      if (functionspace.getTypeCode() == ReducedDegreesOfFreedom) {
1548     // is the function space type right          reduceOrder=1;
1549     if (functionspace.getTypeCode()==DegreesOfFreedom) {      } else if (functionspace.getTypeCode() != DegreesOfFreedom) {
1550        reduceOrder=0;          throw FinleyAdapterException("Error - illegal function space type for transport problem.");
1551     } else if (functionspace.getTypeCode()==ReducedDegreesOfFreedom) {      }
1552        reduceOrder=1;  
1553     } else {      // generate transport problem:
1554        throw FinleyAdapterException("Error - illegal function space type for system matrix rows.");      Paso_SystemMatrixPattern* fsystemMatrixPattern=Finley_getPattern(
1555     }              getFinley_Mesh(), reduceOrder, reduceOrder);
1556     // generate matrix:      checkFinleyError();
1557        Paso_TransportProblem* transportProblem;
1558     Paso_SystemMatrixPattern* fsystemMatrixPattern=Finley_getPattern(getFinley_Mesh(),reduceOrder,reduceOrder);      transportProblem=Paso_TransportProblem_alloc(fsystemMatrixPattern, blocksize);
1559     checkFinleyError();      checkPasoError();
1560     Paso_TransportProblem* transportProblem;      Paso_SystemMatrixPattern_free(fsystemMatrixPattern);
1561     transportProblem=Paso_TransportProblem_alloc(useBackwardEuler,fsystemMatrixPattern,blocksize);      TransportProblemAdapter* tpa=new TransportProblemAdapter(
1562     checkPasoError();              transportProblem, blocksize, functionspace);
1563     Paso_SystemMatrixPattern_free(fsystemMatrixPattern);      return escript::ATP_ptr(tpa);
    return TransportProblemAdapter(transportProblem,useBackwardEuler,blocksize,functionspace);  
1564  }  }
1565    
1566  //  //
1567  // vtkObject MeshAdapter::createVtkObject() const  // returns true if data on functionSpaceCode is considered as being cell centered
 // TODO:  
1568  //  //
   
 //  
 // returns true if data at the atom_type is considered as being cell centered:  
1569  bool MeshAdapter::isCellOriented(int functionSpaceCode) const  bool MeshAdapter::isCellOriented(int functionSpaceCode) const
1570  {  {
1571     switch(functionSpaceCode) {      switch(functionSpaceCode) {
1572     case(Nodes):          case Nodes:
1573     case(DegreesOfFreedom):          case DegreesOfFreedom:
1574     case(ReducedDegreesOfFreedom):          case ReducedDegreesOfFreedom:
1575     return false;              return false;
1576     break;          case Elements:
1577     case(Elements):          case FaceElements:
1578     case(FaceElements):          case Points:
1579     case(Points):          case ContactElementsZero:
1580     case(ContactElementsZero):          case ContactElementsOne:
1581     case(ContactElementsOne):          case ReducedElements:
1582     case(ReducedElements):          case ReducedFaceElements:
1583     case(ReducedFaceElements):          case ReducedContactElementsZero:
1584     case(ReducedContactElementsZero):          case ReducedContactElementsOne:
1585     case(ReducedContactElementsOne):              return true;
1586     return true;          default:
1587     break;              stringstream temp;
1588     default:              temp << "Error - Cell: Finley does not know anything about function space type " << functionSpaceCode;
1589        stringstream temp;              throw FinleyAdapterException(temp.str());
1590        temp << "Error - Cell: Finley does not know anything about function space type " << functionSpaceCode;              break;
1591        throw FinleyAdapterException(temp.str());      }
1592        break;      return false;
    }  
    checkFinleyError();  
    return false;  
1593  }  }
1594    
1595  bool  bool
1596  MeshAdapter::commonFunctionSpace(const vector<int>& fs, int& resultcode) const  MeshAdapter::commonFunctionSpace(const vector<int>& fs, int& resultcode) const
1597  {  {
1598     /* The idea is to use equivalence classes. [Types which can be interpolated back and forth]      /* The idea is to use equivalence classes. [Types which can be interpolated
1599      class 1: DOF <-> Nodes         back and forth]:
1600      class 2: ReducedDOF <-> ReducedNodes          class 1: DOF <-> Nodes
1601      class 3: Points          class 2: ReducedDOF <-> ReducedNodes
1602      class 4: Elements          class 3: Points
1603      class 5: ReducedElements          class 4: Elements
1604      class 6: FaceElements          class 5: ReducedElements
1605      class 7: ReducedFaceElements          class 6: FaceElements
1606      class 8: ContactElementZero <-> ContactElementOne          class 7: ReducedFaceElements
1607      class 9: ReducedContactElementZero <-> ReducedContactElementOne          class 8: ContactElementZero <-> ContactElementOne
1608            class 9: ReducedContactElementZero <-> ReducedContactElementOne
1609     There is also a set of lines. Interpolation is possible down a line but not between lines.  
1610     class 1 and 2 belong to all lines so aren't considered.      There is also a set of lines. Interpolation is possible down a line but
1611      line 0: class 3      not between lines.
1612      line 1: class 4,5      class 1 and 2 belong to all lines so aren't considered.
1613      line 2: class 6,7          line 0: class 3
1614      line 3: class 8,9          line 1: class 4,5
1615            line 2: class 6,7
1616     For classes with multiple members (eg class 2) we have vars to record if there is at least one instance.          line 3: class 8,9
1617     eg hasnodes is true if we have at least one instance of Nodes.  
1618     */      For classes with multiple members (eg class 2) we have vars to record if
1619        there is at least one instance.
1620        e.g. hasnodes is true if we have at least one instance of Nodes.
1621        */
1622      if (fs.empty())      if (fs.empty())
     {  
1623          return false;          return false;
1624      }  
1625      vector<int> hasclass(10);      vector<int> hasclass(10);
1626      vector<int> hasline(4);      vector<int> hasline(4);    
1627      bool hasnodes=false;      bool hasnodes=false;
1628      bool hasrednodes=false;      bool hasrednodes=false;
1629      bool hascez=false;      bool hascez=false;
1630      bool hasrcez=false;      bool hasrcez=false;
1631      for (int i=0;i<fs.size();++i)      for (int i=0;i<fs.size();++i) {
1632      {          switch(fs[i]) {
1633      switch(fs[i])              case Nodes:
1634      {                  hasnodes=true; // fall through
1635      case(Nodes):   hasnodes=true;   // no break is deliberate              case DegreesOfFreedom:
1636      case(DegreesOfFreedom):                  hasclass[1]=1;
1637          hasclass[1]=1;                  break;
1638          break;              case ReducedNodes:
1639      case(ReducedNodes):    hasrednodes=true;    // no break is deliberate                  hasrednodes=true; // fall through
1640      case(ReducedDegreesOfFreedom):              case ReducedDegreesOfFreedom:
1641          hasclass[2]=1;                  hasclass[2]=1;
1642          break;                  break;
1643      case(Points):              case Points:
1644          hasline[0]=1;                  hasline[0]=1;
1645          hasclass[3]=1;                  hasclass[3]=1;
1646          break;                  break;
1647      case(Elements):              case Elements:
1648          hasclass[4]=1;                  hasclass[4]=1;
1649          hasline[1]=1;                  hasline[1]=1;
1650          break;                  break;
1651      case(ReducedElements):              case ReducedElements:
1652          hasclass[5]=1;                  hasclass[5]=1;
1653          hasline[1]=1;                  hasline[1]=1;
1654          break;                  break;
1655      case(FaceElements):              case FaceElements:
1656          hasclass[6]=1;                  hasclass[6]=1;
1657          hasline[2]=1;                  hasline[2]=1;
1658          break;                  break;
1659      case(ReducedFaceElements):              case ReducedFaceElements:
1660          hasclass[7]=1;                  hasclass[7]=1;
1661          hasline[2]=1;                  hasline[2]=1;
1662          break;                  break;
1663      case(ContactElementsZero):  hascez=true;    // no break is deliberate              case ContactElementsZero:
1664      case(ContactElementsOne):                  hascez=true; // fall through
1665          hasclass[8]=1;              case ContactElementsOne:
1666          hasline[3]=1;                  hasclass[8]=1;
1667          break;                  hasline[3]=1;
1668      case(ReducedContactElementsZero):   hasrcez=true;   // no break is deliberate                  break;
1669      case(ReducedContactElementsOne):              case ReducedContactElementsZero:
1670          hasclass[9]=1;                  hasrcez=true; // fall through
1671          hasline[3]=1;              case ReducedContactElementsOne:
1672          break;                  hasclass[9]=1;
1673      default:                  hasline[3]=1;
1674          return false;                  break;
1675      }              default:
1676                    return false;
1677            }
1678      }      }
1679      int totlines=hasline[0]+hasline[1]+hasline[2]+hasline[3];      int totlines=hasline[0]+hasline[1]+hasline[2]+hasline[3];
     // fail if we have more than one leaf group  
1680    
1681        // fail if we have more than one leaf group
1682      if (totlines>1)      if (totlines>1)
1683      {          return false; // there are at least two branches we can't interpolate between
1684      return false;   // there are at least two branches we can't interpolate between      else if (totlines==1) {
1685      }          if (hasline[0]==1)              // we have points
1686      else if (totlines==1)              resultcode=Points;
1687      {          else if (hasline[1]==1) {
1688      if (hasline[0]==1)      // we have points              if (hasclass[5]==1)
1689      {                  resultcode=ReducedElements;
1690          resultcode=Points;              else
1691      }                  resultcode=Elements;
1692      else if (hasline[1]==1)          } else if (hasline[2]==1) {
1693      {              if (hasclass[7]==1)
1694          if (hasclass[5]==1)                  resultcode=ReducedFaceElements;
1695          {              else
1696          resultcode=ReducedElements;                  resultcode=FaceElements;
1697          }          } else {   // so we must be in line3
1698          else              if (hasclass[9]==1) {
1699          {                  // need something from class 9
1700          resultcode=Elements;                  resultcode=(hasrcez ? ReducedContactElementsZero : ReducedContactElementsOne);
1701          }              } else {
1702      }                  // something from class 8
1703      else if (hasline[2]==1)                  resultcode=(hascez?ContactElementsZero:ContactElementsOne);
1704      {              }
1705          if (hasclass[7]==1)          }
1706          {      } else { // totlines==0
1707          resultcode=ReducedFaceElements;          if (hasclass[2]==1) {
1708          }              // something from class 2
1709          else              resultcode=(hasrednodes ? ReducedNodes : ReducedDegreesOfFreedom);
1710          {          } else {
1711          resultcode=FaceElements;              // something from class 1
1712          }              resultcode=(hasnodes ? Nodes : DegreesOfFreedom);
1713      }          }
     else    // so we must be in line3  
     {  
         if (hasclass[9]==1)  
         {  
         // need something from class 9  
         resultcode=(hasrcez?ReducedContactElementsZero:ReducedContactElementsOne);  
         }  
         else  
         {  
         // something from class 8  
         resultcode=(hascez?ContactElementsZero:ContactElementsOne);  
         }  
     }  
     }  
     else    // totlines==0  
     {  
     if (hasclass[2]==1)  
     {  
         // something from class 2  
         resultcode=(hasrednodes?ReducedNodes:ReducedDegreesOfFreedom);  
     }  
     else  
     {   // something from class 1  
         resultcode=(hasnodes?Nodes:DegreesOfFreedom);  
     }  
1714      }      }
1715      return true;      return true;
1716  }  }
1717    
1718  bool MeshAdapter::probeInterpolationOnDomain(int functionSpaceType_source,int functionSpaceType_target) const  bool MeshAdapter::probeInterpolationOnDomain(int functionSpaceType_source,
1719                                                 int functionSpaceType_target) const
1720  {  {
1721     switch(functionSpaceType_source) {      switch(functionSpaceType_source) {
1722     case(Nodes):          case Nodes:
1723      switch(functionSpaceType_target) {              switch(functionSpaceType_target) {
1724      case(Nodes):                  case Nodes:
1725      case(ReducedNodes):                  case ReducedNodes:
1726      case(ReducedDegreesOfFreedom):                  case ReducedDegreesOfFreedom:
1727      case(DegreesOfFreedom):                  case DegreesOfFreedom:
1728      case(Elements):                  case Elements:
1729      case(ReducedElements):                  case ReducedElements:
1730      case(FaceElements):                  case FaceElements:
1731      case(ReducedFaceElements):                  case ReducedFaceElements:
1732      case(Points):                  case Points:
1733      case(ContactElementsZero):                  case ContactElementsZero:
1734      case(ReducedContactElementsZero):                  case ReducedContactElementsZero:
1735      case(ContactElementsOne):                  case ContactElementsOne:
1736      case(ReducedContactElementsOne):                  case ReducedContactElementsOne:
1737      return true;                      return true;
1738      default:                  default:
1739            stringstream temp;                      stringstream temp;
1740            temp << "Error - Interpolation On Domain: Finley does not know anything about function space type " << functionSpaceType_target;                      temp << "Error - Interpolation On Domain: Finley does not know anything about function space type " << functionSpaceType_target;
1741            throw FinleyAdapterException(temp.str());                      throw FinleyAdapterException(temp.str());
1742     }              }
1743     break;              break;
1744     case(ReducedNodes):          case ReducedNodes:
1745      switch(functionSpaceType_target) {              switch(functionSpaceType_target) {
1746      case(ReducedNodes):                  case ReducedNodes:
1747      case(ReducedDegreesOfFreedom):                  case ReducedDegreesOfFreedom:
1748      case(Elements):                  case Elements:
1749      case(ReducedElements):                  case ReducedElements:
1750      case(FaceElements):                  case FaceElements:
1751      case(ReducedFaceElements):                  case ReducedFaceElements:
1752      case(Points):                  case Points:
1753      case(ContactElementsZero):                  case ContactElementsZero:
1754      case(ReducedContactElementsZero):                  case ReducedContactElementsZero:
1755      case(ContactElementsOne):                  case ContactElementsOne:
1756      case(ReducedContactElementsOne):                  case ReducedContactElementsOne:
1757      return true;                      return true;
1758      case(Nodes):                  case Nodes:
1759      case(DegreesOfFreedom):                  case DegreesOfFreedom:
1760      return false;                      return false;
1761      default:                  default:
1762          stringstream temp;                      stringstream temp;
1763          temp << "Error - Interpolation On Domain: Finley does not know anything about function space type " << functionSpaceType_target;                      temp << "Error - Interpolation On Domain: Finley does not know anything about function space type " << functionSpaceType_target;
1764          throw FinleyAdapterException(temp.str());                      throw FinleyAdapterException(temp.str());
1765     }              }
1766     break;              break;
1767     case(Elements):          case Elements:
1768      if (functionSpaceType_target==Elements) {              if (functionSpaceType_target==Elements) {
1769        return true;                  return true;
1770      } else if (functionSpaceType_target==ReducedElements) {              } else if (functionSpaceType_target==ReducedElements) {
1771        return true;                  return true;
1772          } else {              } else {
1773            return false;                  return false;
1774          }              }
1775     case(ReducedElements):          case ReducedElements:
1776      if (functionSpaceType_target==ReducedElements) {              if (functionSpaceType_target==ReducedElements) {
1777        return true;                  return true;
1778      } else {              } else {
1779            return false;                  return false;
1780      }              }
1781     case(FaceElements):          case FaceElements:
1782      if (functionSpaceType_target==FaceElements) {              if (functionSpaceType_target==FaceElements) {
1783              return true;                  return true;
1784      } else if (functionSpaceType_target==ReducedFaceElements) {              } else if (functionSpaceType_target==ReducedFaceElements) {
1785              return true;                  return true;
1786      } else {              } else {
1787              return false;                  return false;
1788      }              }
1789     case(ReducedFaceElements):          case ReducedFaceElements:
1790      if (functionSpaceType_target==ReducedFaceElements) {              if (functionSpaceType_target==ReducedFaceElements) {
1791              return true;                  return true;
1792      } else {              } else {
1793          return false;                  return false;
1794      }              }
1795     case(Points):          case Points:
1796      if (functionSpaceType_target==Points) {              if (functionSpaceType_target==Points) {
1797              return true;                  return true;
1798      } else {              } else {
1799              return false;                  return false;
1800      }              }
1801     case(ContactElementsZero):          case ContactElementsZero:
1802     case(ContactElementsOne):          case ContactElementsOne:
1803      if (functionSpaceType_target==ContactElementsZero || functionSpaceType_target==ContactElementsOne) {              if (functionSpaceType_target==ContactElementsZero || functionSpaceType_target==ContactElementsOne) {
1804              return true;                  return true;
1805      } else if (functionSpaceType_target==ReducedContactElementsZero || functionSpaceType_target==ReducedContactElementsOne) {              } else if (functionSpaceType_target==ReducedContactElementsZero || functionSpaceType_target==ReducedContactElementsOne) {
1806              return true;                  return true;
1807      } else {              } else {
1808              return false;                  return false;
1809      }              }
1810     case(ReducedContactElementsZero):          case ReducedContactElementsZero:
1811     case(ReducedContactElementsOne):          case ReducedContactElementsOne:
1812      if (functionSpaceType_target==ReducedContactElementsZero || functionSpaceType_target==ReducedContactElementsOne) {              if (functionSpaceType_target==ReducedContactElementsZero || functionSpaceType_target==ReducedContactElementsOne) {
1813              return true;                  return true;
1814      } else {              } else {
1815              return false;                  return false;
1816      }              }
1817     case(DegreesOfFreedom):          case DegreesOfFreedom:
1818      switch(functionSpaceType_target) {              switch(functionSpaceType_target) {
1819      case(ReducedDegreesOfFreedom):                  case ReducedDegreesOfFreedom:
1820      case(DegreesOfFreedom):                  case DegreesOfFreedom:
1821      case(Nodes):                  case Nodes:
1822      case(ReducedNodes):                  case ReducedNodes:
1823      case(Elements):                  case Elements:
1824      case(ReducedElements):                  case ReducedElements:
1825      case(Points):                  case Points:
1826      case(FaceElements):                  case FaceElements:
1827      case(ReducedFaceElements):                  case ReducedFaceElements:
1828      case(ContactElementsZero):                  case ContactElementsZero:
1829      case(ReducedContactElementsZero):                  case ReducedContactElementsZero:
1830      case(ContactElementsOne):                  case ContactElementsOne:
1831      case(ReducedContactElementsOne):                  case ReducedContactElementsOne:
1832      return true;                      return true;
1833      default:                  default:
1834          stringstream temp;                      stringstream temp;
1835          temp << "Error - Interpolation On Domain: Finley does not know anything about function space type " << functionSpaceType_target;                      temp << "Error - Interpolation On Domain: Finley does not know anything about function space type " << functionSpaceType_target;
1836          throw FinleyAdapterException(temp.str());                      throw FinleyAdapterException(temp.str());
1837      }              }
1838      break;              break;
1839     case(ReducedDegreesOfFreedom):          case ReducedDegreesOfFreedom:
1840     switch(functionSpaceType_target) {              switch(functionSpaceType_target) {
1841      case(ReducedDegreesOfFreedom):                  case ReducedDegreesOfFreedom:
1842      case(ReducedNodes):                  case ReducedNodes:
1843      case(Elements):                  case Elements:
1844      case(ReducedElements):                  case ReducedElements:
1845      case(FaceElements):                  case FaceElements:
1846      case(ReducedFaceElements):                  case ReducedFaceElements:
1847      case(Points):                  case Points:
1848      case(ContactElementsZero):                  case ContactElementsZero:
1849      case(ReducedContactElementsZero):                  case ReducedContactElementsZero:
1850      case(ContactElementsOne):                  case ContactElementsOne:
1851      case(ReducedContactElementsOne):                  case ReducedContactElementsOne:
1852      return true;                      return true;
1853      case(Nodes):                  case Nodes:
1854      case(DegreesOfFreedom):                  case DegreesOfFreedom:
1855      return false;                      return false;
1856      default:                  default:
1857          stringstream temp;                      stringstream temp;
1858          temp << "Error - Interpolation On Domain: Finley does not know anything about function space type " << functionSpaceType_target;                      temp << "Error - Interpolation On Domain: Finley does not know anything about function space type " << functionSpaceType_target;
1859          throw FinleyAdapterException(temp.str());                      throw FinleyAdapterException(temp.str());
1860      }              }
1861      break;              break;
1862     default:          default:
1863        stringstream temp;              stringstream temp;
1864        temp << "Error - Interpolation On Domain: Finley does not know anything about function space type " << functionSpaceType_source;              temp << "Error - Interpolation On Domain: Finley does not know anything about function space type " << functionSpaceType_source;
1865        throw FinleyAdapterException(temp.str());              throw FinleyAdapterException(temp.str());
1866        break;              break;
1867     }      }
1868     checkFinleyError();      return false;
1869     return false;  }
1870  }  
1871    signed char MeshAdapter::preferredInterpolationOnDomain(int functionSpaceType_source, int functionSpaceType_target) const
1872  bool MeshAdapter::probeInterpolationACross(int functionSpaceType_source,const AbstractDomain& targetDomain, int functionSpaceType_target) const  {
1873  {      if (probeInterpolationOnDomain(functionSpaceType_source, functionSpaceType_target))
1874     return false;          return 1;
1875  }  
1876        if (probeInterpolationOnDomain(functionSpaceType_target, functionSpaceType_source))
1877  bool MeshAdapter::operator==(const AbstractDomain& other) const          return -1;
1878  {  
1879     const MeshAdapter* temp=dynamic_cast<const MeshAdapter*>(&other);      return 0;
    if (temp!=0) {  
       return (m_finleyMesh==temp->m_finleyMesh);  
    } else {  
       return false;  
    }  
1880  }  }
1881    
1882  bool MeshAdapter::operator!=(const AbstractDomain& other) const  bool MeshAdapter::probeInterpolationACross(int functionSpaceType_source,
1883            const escript::AbstractDomain& targetDomain,
1884            int functionSpaceType_target) const
1885  {  {
1886     return !(operator==(other));      return false;
1887    }
1888    
1889    bool MeshAdapter::operator==(const escript::AbstractDomain& other) const
1890    {
1891        const MeshAdapter* temp=dynamic_cast<const MeshAdapter*>(&other);
1892        if (temp) {
1893            return (m_finleyMesh==temp->m_finleyMesh);
1894        } else {
1895            return false;
1896        }
1897    }
1898    
1899    bool MeshAdapter::operator!=(const escript::AbstractDomain& other) const
1900    {
1901        return !(operator==(other));
1902  }  }
1903    
1904  int MeshAdapter::getSystemMatrixTypeId(const int solver, const int preconditioner, const int package, const bool symmetry) const  int MeshAdapter::getSystemMatrixTypeId(const int solver, const int preconditioner, const int package, const bool symmetry) const
1905  {  {
1906     Finley_Mesh* mesh=m_finleyMesh.get();      Finley_Mesh* mesh=m_finleyMesh.get();
1907     int out=Paso_SystemMatrix_getSystemMatrixTypeId(SystemMatrixAdapter::mapOptionToPaso(solver),SystemMatrixAdapter::mapOptionToPaso(preconditioner), SystemMatrixAdapter::mapOptionToPaso(package),symmetry?1:0, mesh->MPIInfo);      return SystemMatrixAdapter::getSystemMatrixTypeId(solver, preconditioner,
1908     checkPasoError();                 package, symmetry, mesh->MPIInfo);
    return out;  
1909  }  }
1910    
1911  int MeshAdapter::getTransportTypeId(const int solver, const int preconditioner, const int package, const bool symmetry) const  int MeshAdapter::getTransportTypeId(const int solver, const int preconditioner, const int package, const bool symmetry) const
1912  {  {
1913     Finley_Mesh* mesh=m_finleyMesh.get();      Finley_Mesh* mesh=m_finleyMesh.get();
1914     int out=Paso_TransportProblem_getTypeId(SystemMatrixAdapter::mapOptionToPaso(solver),SystemMatrixAdapter::mapOptionToPaso(preconditioner), SystemMatrixAdapter::mapOptionToPaso(package),symmetry?1:0, mesh->MPIInfo);      return TransportProblemAdapter::getTransportTypeId(solver, preconditioner,
1915     checkPasoError();                  package, symmetry, mesh->MPIInfo);
    return out;  
1916  }  }
1917    
1918  escript::Data MeshAdapter::getX() const  escript::Data MeshAdapter::getX() const
1919  {  {
1920     return continuousFunction(asAbstractContinuousDomain()).getX();      return continuousFunction(*this).getX();
1921  }  }
1922    
1923  escript::Data MeshAdapter::getNormal() const  escript::Data MeshAdapter::getNormal() const
1924  {  {
1925     return functionOnBoundary(asAbstractContinuousDomain()).getNormal();      return functionOnBoundary(*this).getNormal();
1926  }  }
1927    
1928  escript::Data MeshAdapter::getSize() const  escript::Data MeshAdapter::getSize() const
1929  {  {
1930     return escript::function(asAbstractContinuousDomain()).getSize();      return escript::function(*this).getSize();
1931  }  }
1932    
1933  const int* MeshAdapter::borrowSampleReferenceIDs(int functionSpaceType) const  const int* MeshAdapter::borrowSampleReferenceIDs(int functionSpaceType) const
1934  {  {
1935     int *out = NULL;      int *out = NULL;
1936     Finley_Mesh* mesh=m_finleyMesh.get();      Finley_Mesh* mesh=m_finleyMesh.get();
1937     switch (functionSpaceType) {      switch (functionSpaceType) {
1938     case(Nodes):          case Nodes:
1939     out=mesh->Nodes->Id;              out=mesh->Nodes->Id;
1940     break;              break;
1941     case(ReducedNodes):          case ReducedNodes:
1942     out=mesh->Nodes->reducedNodesId;              out=mesh->Nodes->reducedNodesId;
1943     break;              break;
1944     case(Elements):          case Elements:
1945     out=mesh->Elements->Id;          case ReducedElements:
1946     break;              out=mesh->Elements->Id;
1947     case(ReducedElements):              break;
1948     out=mesh->Elements->Id;          case FaceElements:
1949     break;          case ReducedFaceElements:
1950     case(FaceElements):              out=mesh->FaceElements->Id;
1951     out=mesh->FaceElements->Id;              break;
1952     break;          case Points:
1953     case(ReducedFaceElements):              out=mesh->Points->Id;
1954     out=mesh->FaceElements->Id;              break;
1955     break;          case ContactElementsZero:
1956     case(Points):          case ReducedContactElementsZero:
1957     out=mesh->Points->Id;          case ContactElementsOne:
1958     break;          case ReducedContactElementsOne:
1959     case(ContactElementsZero):              out=mesh->ContactElements->Id;
1960     out=mesh->ContactElements->Id;              break;
1961     break;          case DegreesOfFreedom:
1962     case(ReducedContactElementsZero):              out=mesh->Nodes->degreesOfFreedomId;
1963     out=mesh->ContactElements->Id;              break;
1964     break;          case ReducedDegreesOfFreedom:
1965     case(ContactElementsOne):              out=mesh->Nodes->reducedDegreesOfFreedomId;
1966     out=mesh->ContactElements->Id;              break;
1967     break;          default:
1968     case(ReducedContactElementsOne):              stringstream temp;
1969     out=mesh->ContactElements->Id;              temp << "Error - Invalid function space type: " << functionSpaceType << " for domain: " << getDescription();
1970     break;              throw FinleyAdapterException(temp.str());
1971     case(DegreesOfFreedom):              break;
1972     out=mesh->Nodes->degreesOfFreedomId;      }
1973     break;      return out;
    case(ReducedDegreesOfFreedom):  
    out=mesh->Nodes->reducedDegreesOfFreedomId;  
    break;  
    default:  
       stringstream temp;  
       temp << "Error - Invalid function space type: " << functionSpaceType << " for domain: " << getDescription();  
       throw FinleyAdapterException(temp.str());  
       break;  
    }  
    return out;  
1974  }  }
1975  int MeshAdapter::getTagFromSampleNo(int functionSpaceType, int sampleNo) const  int MeshAdapter::getTagFromSampleNo(int functionSpaceType, int sampleNo) const
1976  {  {
1977     int out=0;      int out=0;
1978     Finley_Mesh* mesh=m_finleyMesh.get();      Finley_Mesh* mesh=m_finleyMesh.get();
1979     switch (functionSpaceType) {      switch (functionSpaceType) {
1980     case(Nodes):          case Nodes:
1981     out=mesh->Nodes->Tag[sampleNo];              out=mesh->Nodes->Tag[sampleNo];
1982     break;              break;
1983     case(ReducedNodes):          case ReducedNodes:
1984     throw FinleyAdapterException(" Error - ReducedNodes does not support tags.");              throw FinleyAdapterException(" Error - ReducedNodes does not support tags.");
1985     break;              break;
1986     case(Elements):          case Elements:
1987     out=mesh->Elements->Tag[sampleNo];          case ReducedElements:
1988     break;              out=mesh->Elements->Tag[sampleNo];
1989     case(ReducedElements):              break;
1990     out=mesh->Elements->Tag[sampleNo];          case FaceElements:
1991     break;          case ReducedFaceElements:
1992     case(FaceElements):              out=mesh->FaceElements->Tag[sampleNo];
1993     out=mesh->FaceElements->Tag[sampleNo];              break;
1994     break;          case Points:
1995     case(ReducedFaceElements):              out=mesh->Points->Tag[sampleNo];
1996     out=mesh->FaceElements->Tag[sampleNo];              break;
1997     break;          case ContactElementsZero:
1998     case(Points):          case ReducedContactElementsZero:
1999     out=mesh->Points->Tag[sampleNo];          case ContactElementsOne:
2000     break;          case ReducedContactElementsOne:
2001     case(ContactElementsZero):              out=mesh->ContactElements->Tag[sampleNo];
2002     out=mesh->ContactElements->Tag[sampleNo];              break;
2003     break;          case DegreesOfFreedom:
2004     case(ReducedContactElementsZero):              throw FinleyAdapterException(" Error - DegreesOfFreedom does not support tags.");
2005     out=mesh->ContactElements->Tag[sampleNo];              break;
2006     break;          case ReducedDegreesOfFreedom:
2007     case(ContactElementsOne):              throw FinleyAdapterException(" Error - ReducedDegreesOfFreedom does not support tags.");
2008     out=mesh->ContactElements->Tag[sampleNo];              break;
2009     break;          default:
2010     case(ReducedContactElementsOne):              stringstream temp;
2011     out=mesh->ContactElements->Tag[sampleNo];              temp << "Error - Invalid function space type: " << functionSpaceType << " for domain: " << getDescription();
2012     break;              throw FinleyAdapterException(temp.str());
2013     case(DegreesOfFreedom):              break;
2014     throw FinleyAdapterException(" Error - DegreesOfFreedom does not support tags.");      }
2015     break;      return out;
    case(ReducedDegreesOfFreedom):  
    throw FinleyAdapterException(" Error - ReducedDegreesOfFreedom does not support tags.");  
    break;  
    default:  
       stringstream temp;  
       temp << "Error - Invalid function space type: " << functionSpaceType << " for domain: " << getDescription();  
       throw FinleyAdapterException(temp.str());  
       break;  
    }  
    return out;  
2016  }  }
2017    
2018    
2019  void MeshAdapter::setTags(const int functionSpaceType, const int newTag, const escript::Data& mask) const  void MeshAdapter::setTags(const int functionSpaceType, const int newTag, const escript::Data& mask) const
2020  {  {
2021     Finley_Mesh* mesh=m_finleyMesh.get();      Finley_Mesh* mesh=m_finleyMesh.get();
2022     escriptDataC tmp=mask.getDataC();      switch(functionSpaceType) {
2023     switch(functionSpaceType) {          case Nodes:
2024     case(Nodes):              mesh->Nodes->setTags(newTag, mask);
2025     Finley_NodeFile_setTags(mesh->Nodes,newTag,&tmp);              break;
2026     break;          case ReducedNodes:
2027     case(ReducedNodes):              throw FinleyAdapterException("Error - ReducedNodes does not support tags");
2028     throw FinleyAdapterException("Error - ReducedNodes does not support tags");          case DegreesOfFreedom:
2029     break;              throw FinleyAdapterException("Error - DegreesOfFreedom does not support tags");
2030     case(DegreesOfFreedom):          case ReducedDegreesOfFreedom:
2031     throw FinleyAdapterException("Error - DegreesOfFreedom does not support tags");              throw FinleyAdapterException("Error - ReducedDegreesOfFreedom does not support tags");
2032     break;          case Elements:
2033     case(ReducedDegreesOfFreedom):          case ReducedElements:
2034     throw FinleyAdapterException("Error - ReducedDegreesOfFreedom does not support tags");              mesh->Elements->setTags(newTag, mask);
2035     break;              break;
2036     case(Elements):          case FaceElements:
2037     Finley_ElementFile_setTags(mesh->Elements,newTag,&tmp);          case ReducedFaceElements:
2038     break;              mesh->FaceElements->setTags(newTag, mask);
2039     case(ReducedElements):              break;
2040     Finley_ElementFile_setTags(mesh->Elements,newTag,&tmp);          case Points:
2041     break;              mesh->Points->setTags(newTag, mask);
2042     case(FaceElements):              break;
2043     Finley_ElementFile_setTags(mesh->FaceElements,newTag,&tmp);          case ContactElementsZero:
2044     break;          case ReducedContactElementsZero:
2045     case(ReducedFaceElements):          case ContactElementsOne:
2046     Finley_ElementFile_setTags(mesh->FaceElements,newTag,&tmp);          case ReducedContactElementsOne:
2047     break;              mesh->ContactElements->setTags(newTag, mask);
2048     case(Points):              break;
2049     Finley_ElementFile_setTags(mesh->Points,newTag,&tmp);          default:
2050     break;              stringstream temp;
2051     case(ContactElementsZero):              temp << "Error - Finley does not know anything about function space type " << functionSpaceType;
2052     Finley_ElementFile_setTags(mesh->ContactElements,newTag,&tmp);              throw FinleyAdapterException(temp.str());
2053     break;      }
2054     case(ReducedContactElementsZero):      checkFinleyError();
2055     Finley_ElementFile_setTags(mesh->ContactElements,newTag,&tmp);  }
2056     break;  
2057     case(ContactElementsOne):  void MeshAdapter::setTagMap(const string& name, int tag)
2058     Finley_ElementFile_setTags(mesh->ContactElements,newTag,&tmp);  {
2059     break;      Finley_Mesh* mesh=m_finleyMesh.get();
2060     case(ReducedContactElementsOne):      Finley_Mesh_addTagMap(mesh, name.c_str(), tag);
2061     Finley_ElementFile_setTags(mesh->ContactElements,newTag,&tmp);      checkFinleyError();
    break;  
    default:  
       stringstream temp;  
       temp << "Error - Finley does not know anything about function space type " << functionSpaceType;  
       throw FinleyAdapterException(temp.str());  
    }  
    checkFinleyError();  
    return;  
 }  
   
 void MeshAdapter::setTagMap(const string& name,  int tag)  
 {  
    Finley_Mesh* mesh=m_finleyMesh.get();  
    Finley_Mesh_addTagMap(mesh, name.c_str(),tag);  
    checkPasoError();  
    // throwStandardException("MeshAdapter::set TagMap is not implemented.");  
2062  }  }
2063    
2064  int MeshAdapter::getTag(const string& name) const  int MeshAdapter::getTag(const string& name) const
2065  {  {
2066     Finley_Mesh* mesh=m_finleyMesh.get();      Finley_Mesh* mesh=m_finleyMesh.get();
2067     int tag=0;      int tag = Finley_Mesh_getTag(mesh, name.c_str());
2068     tag=Finley_Mesh_getTag(mesh, name.c_str());      checkFinleyError();
2069     checkPasoError();      return tag;
    // throwStandardException("MeshAdapter::getTag is not implemented.");  
    return tag;  
2070  }  }
2071    
2072  bool MeshAdapter::isValidTagName(const string& name) const  bool MeshAdapter::isValidTagName(const string& name) const
2073  {  {
2074     Finley_Mesh* mesh=m_finleyMesh.get();      Finley_Mesh* mesh=m_finleyMesh.get();
2075     return Finley_Mesh_isValidTagName(mesh,name.c_str());      return Finley_Mesh_isValidTagName(mesh, name.c_str());
2076  }  }
2077    
2078  string MeshAdapter::showTagNames() const  string MeshAdapter::showTagNames() const
2079  {  {
2080     stringstream temp;      stringstream temp;
2081     Finley_Mesh* mesh=m_finleyMesh.get();      Finley_Mesh* mesh=m_finleyMesh.get();
2082     Finley_TagMap* tag_map=mesh->TagMap;      TagMap::const_iterator it = mesh->tagMap.begin();
2083     while (tag_map) {      while (it != mesh->tagMap.end()) {
2084        temp << tag_map->name;          temp << it->first;
2085        tag_map=tag_map->next;          ++it;
2086        if (tag_map) temp << ", ";          if (it != mesh->tagMap.end())
2087     }              temp << ", ";
2088     return temp.str();      }
2089        return temp.str();
2090  }  }
2091    
2092  int MeshAdapter::getNumberOfTagsInUse(int functionSpaceCode) const  int MeshAdapter::getNumberOfTagsInUse(int functionSpaceCode) const
2093  {  {
2094    Finley_Mesh* mesh=m_finleyMesh.get();      Finley_Mesh* mesh=m_finleyMesh.get();
2095    dim_t numTags=0;      switch(functionSpaceCode) {
2096    switch(functionSpaceCode) {          case Nodes:
2097     case(Nodes):              return mesh->Nodes->tagsInUse.size();
2098            numTags=mesh->Nodes->numTagsInUse;          case ReducedNodes:
2099            break;              throw FinleyAdapterException("Error - ReducedNodes does not support tags");
2100     case(ReducedNodes):          case DegreesOfFreedom:
2101            throw FinleyAdapterException("Error - ReducedNodes does not support tags");              throw FinleyAdapterException("Error - DegreesOfFreedom does not support tags");
2102            break;          case ReducedDegreesOfFreedom:
2103     case(DegreesOfFreedom):              throw FinleyAdapterException("Error - ReducedDegreesOfFreedom does not support tags");
2104            throw FinleyAdapterException("Error - DegreesOfFreedom does not support tags");          case Elements:
2105            break;          case ReducedElements:
2106     case(ReducedDegreesOfFreedom):              return mesh->Elements->tagsInUse.size();
2107            throw FinleyAdapterException("Error - ReducedDegreesOfFreedom does not support tags");          case FaceElements:
2108            break;          case ReducedFaceElements:
2109     case(Elements):              return mesh->FaceElements->tagsInUse.size();
2110     case(ReducedElements):          case Points:
2111            numTags=mesh->Elements->numTagsInUse;              return mesh->Points->tagsInUse.size();
2112            break;          case ContactElementsZero:
2113     case(FaceElements):          case ReducedContactElementsZero:
2114     case(ReducedFaceElements):          case ContactElementsOne:
2115            numTags=mesh->FaceElements->numTagsInUse;          case ReducedContactElementsOne:
2116            break;              return mesh->ContactElements->tagsInUse.size();
2117     case(Points):          default:
2118            numTags=mesh->Points->numTagsInUse;              stringstream ss;
2119            break;              ss << "Finley does not know anything about function space type "
2120     case(ContactElementsZero):                   << functionSpaceCode;
2121     case(ReducedContactElementsZero):              throw FinleyAdapterException(ss.str());
2122     case(ContactElementsOne):      }
2123     case(ReducedContactElementsOne):      return 0;
           numTags=mesh->ContactElements->numTagsInUse;  
           break;  
    default:  
       stringstream temp;  
       temp << "Error - Finley does not know anything about function space type " << functionSpaceCode;  
       throw FinleyAdapterException(temp.str());  
   }  
   return numTags;  
2124  }  }
2125    
2126  const int* MeshAdapter::borrowListOfTagsInUse(int functionSpaceCode) const  const int* MeshAdapter::borrowListOfTagsInUse(int functionSpaceCode) const
2127  {  {
2128    Finley_Mesh* mesh=m_finleyMesh.get();      Finley_Mesh* mesh=m_finleyMesh.get();
2129    index_t* tags=NULL;      switch(functionSpaceCode) {
2130    switch(functionSpaceCode) {          case Nodes:
2131     case(Nodes):              return &mesh->Nodes->tagsInUse[0];
2132            tags=mesh->Nodes->tagsInUse;          case ReducedNodes:
2133            break;              throw FinleyAdapterException("Error - ReducedNodes does not support tags");
2134     case(ReducedNodes):          case DegreesOfFreedom:
2135            throw FinleyAdapterException("Error - ReducedNodes does not support tags");              throw FinleyAdapterException("Error - DegreesOfFreedom does not support tags");
2136            break;          case ReducedDegreesOfFreedom:
2137     case(DegreesOfFreedom):              throw FinleyAdapterException("Error - ReducedDegreesOfFreedom does not support tags");
2138            throw FinleyAdapterException("Error - DegreesOfFreedom does not support tags");          case Elements:
2139            break;          case ReducedElements:
2140     case(ReducedDegreesOfFreedom):              return &mesh->Elements->tagsInUse[0];
2141            throw FinleyAdapterException("Error - ReducedDegreesOfFreedom does not support tags");          case FaceElements:
2142            break;          case ReducedFaceElements:
2143     case(Elements):              return &mesh->FaceElements->tagsInUse[0];
2144     case(ReducedElements):          case Points:
2145            tags=mesh->Elements->tagsInUse;              return &mesh->Points->tagsInUse[0];
2146            break;          case ContactElementsZero:
2147     case(FaceElements):          case ReducedContactElementsZero:
2148     case(ReducedFaceElements):          case ContactElementsOne:
2149            tags=mesh->FaceElements->tagsInUse;          case ReducedContactElementsOne:
2150            break;              return &mesh->ContactElements->tagsInUse[0];
2151     case(Points):          default:
2152            tags=mesh->Points->tagsInUse;              stringstream temp;
2153            break;              temp << "Error - Finley does not know anything about function space type " << functionSpaceCode;
2154     case(ContactElementsZero):              throw FinleyAdapterException(temp.str());
2155     case(ReducedContactElementsZero):      }
2156     case(ContactElementsOne):      return NULL;
    case(ReducedContactElementsOne):  
           tags=mesh->ContactElements->tagsInUse;  
           break;  
    default:  
       stringstream temp;  
       temp << "Error - Finley does not know anything about function space type " << functionSpaceCode;  
       throw FinleyAdapterException(temp.str());  
   }  
   return tags;  
2157  }  }
2158    
2159    
2160  bool MeshAdapter::canTag(int functionSpaceCode) const  bool MeshAdapter::canTag(int functionSpaceCode) const
2161  {  {
2162    switch(functionSpaceCode) {      switch(functionSpaceCode) {
2163     case(Nodes):          case Nodes:
2164     case(Elements):          case Elements:
2165     case(ReducedElements):          case ReducedElements:
2166     case(FaceElements):          case FaceElements:
2167     case(ReducedFaceElements):          case ReducedFaceElements:
2168     case(Points):          case Points:
2169     case(ContactElementsZero):          case ContactElementsZero:
2170     case(ReducedContactElementsZero):          case ReducedContactElementsZero:
2171     case(ContactElementsOne):          case ContactElementsOne:
2172     case(ReducedContactElementsOne):          case ReducedContactElementsOne:
2173            return true;              return true;
2174     case(ReducedNodes):          default:
2175     case(DegreesOfFreedom):              return false;
2176     case(ReducedDegreesOfFreedom):      }
       return false;  
    default:  
     return false;  
   }  
2177  }  }
2178    
2179  AbstractDomain::StatusType MeshAdapter::getStatus() const  escript::AbstractDomain::StatusType MeshAdapter::getStatus() const
2180  {  {
2181    Finley_Mesh* mesh=m_finleyMesh.get();      Finley_Mesh* mesh=m_finleyMesh.get();
2182    return Finley_Mesh_getStatus(mesh);      return Finley_Mesh_getStatus(mesh);
2183  }  }
2184    
2185  int MeshAdapter::getApproximationOrder(const int functionSpaceCode) const  int MeshAdapter::getApproximationOrder(const int functionSpaceCode) const
2186  {  {
2187          Finley_Mesh* mesh=m_finleyMesh.get();
2188    Finley_Mesh* mesh=m_finleyMesh.get();      int order =-1;
2189    int order =-1;      switch(functionSpaceCode) {
2190    switch(functionSpaceCode) {          case Nodes:
2191     case(Nodes):          case DegreesOfFreedom:
2192     case(DegreesOfFreedom):              order=mesh->approximationOrder;
2193            order=mesh->approximationOrder;              break;
2194            break;          case ReducedNodes:
2195     case(ReducedNodes):          case ReducedDegreesOfFreedom:
2196     case(ReducedDegreesOfFreedom):              order=mesh->reducedApproximationOrder;
2197            order=mesh->reducedApproximationOrder;              break;
2198            break;          case Elements:
2199     case(Elements):          case FaceElements:
2200     case(FaceElements):          case Points:
2201     case(Points):          case ContactElementsZero:
2202     case(ContactElementsZero):          case ContactElementsOne:
2203     case(ContactElementsOne):              order=mesh->integrationOrder;
2204            order=mesh->integrationOrder;              break;
2205            break;          case ReducedElements:
2206     case(ReducedElements):          case ReducedFaceElements:
2207     case(ReducedFaceElements):          case ReducedContactElementsZero:
2208     case(ReducedContactElementsZero):          case ReducedContactElementsOne:
2209     case(ReducedContactElementsOne):              order=mesh->reducedIntegrationOrder;
2210            order=mesh->reducedIntegrationOrder;              break;
2211            break;          default:
2212     default:              stringstream temp;
2213        stringstream temp;              temp << "Error - Finley does not know anything about function space type " << functionSpaceCode;
2214        temp << "Error - Finley does not know anything about function space type " << functionSpaceCode;              throw FinleyAdapterException(temp.str());
2215        throw FinleyAdapterException(temp.str());      }
2216    }      return order;
2217    return order;  }
2218    
2219    bool MeshAdapter::supportsContactElements() const
2220    {
2221        return true;
2222  }  }
2223    
2224  ReferenceElementSetWrapper::ReferenceElementSetWrapper(ElementTypeId id, index_t order, index_t reducedOrder)  ReferenceElementSetWrapper::ReferenceElementSetWrapper(Finley_ElementTypeId id,
2225            index_t order, index_t reducedOrder)
2226  {  {
2227    m_refSet = Finley_ReferenceElementSet_alloc(id, order, reducedOrder);      m_refSet = Finley_ReferenceElementSet_alloc(id, order, reducedOrder);
2228  }  }
2229    
2230  ReferenceElementSetWrapper::~ReferenceElementSetWrapper()  ReferenceElementSetWrapper::~ReferenceElementSetWrapper()
2231  {  {
2232    Finley_ReferenceElementSet_dealloc(m_refSet);      Finley_ReferenceElementSet_dealloc(m_refSet);
2233    }
2234    
2235    void MeshAdapter::addDiracPoints(const vector<double>& points,
2236                                     const vector<int>& tags) const
2237    {
2238        // points will be flattened
2239        const int dim = getDim();
2240        int numPoints=points.size()/dim;
2241        int numTags=tags.size();
2242        Finley_Mesh* mesh=m_finleyMesh.get();
2243    
2244        if ( points.size() % dim != 0 ) {
2245            throw FinleyAdapterException("Error - number of coords does not appear to be a multiple of dimension.");
2246        }
2247    
2248        if ((numTags > 0) && (numPoints != numTags))
2249            throw FinleyAdapterException("Error - if tags are given number of tags and points must match.");
2250    
2251        Finley_Mesh_addPoints(mesh, numPoints, &points[0], &tags[0]);
2252        checkFinleyError();
2253  }  }
2254    
2255    // void MeshAdapter::addDiracPoints(const boost::python::list& points, const boost::python::list& tags) const
2256    // {
2257    //       const int dim = getDim();
2258    //       int numPoints=boost::python::extract<int>(points.attr("__len__")());
2259    //       int numTags=boost::python::extract<int>(tags.attr("__len__")());
2260    //       Finley_Mesh* mesh=m_finleyMesh.get();
2261    //
2262    //       if  ( (numTags > 0) && ( numPoints !=  numTags ) )
2263    //       throw FinleyAdapterException("Error - if tags are given number of tags and points must match.");
2264    //
2265    //       double* points_ptr=TMPMEMALLOC(numPoints * dim, double);
2266    //       int*    tags_ptr= TMPMEMALLOC(numPoints, int);
2267    //
2268    //       for (int i=0;i<numPoints;++i) {
2269    //         int tag_id=-1;
2270    //         int numComps=boost::python::extract<int>(points[i].attr("__len__")());
2271    //         if  ( numComps !=   dim ) {
2272    //                stringstream temp;
2273    //                temp << "Error - illegal number of components " << numComps << " for point " << i;
2274    //                throw FinleyAdapterException(temp.str());
2275    //         }
2276    //         points_ptr[ i * dim     ] = boost::python::extract<double>(points[i][0]);
2277    //         if ( dim > 1 ) points_ptr[ i * dim + 1 ] = boost::python::extract<double>(points[i][1]);
2278    //         if ( dim > 2 ) points_ptr[ i * dim + 2 ] = boost::python::extract<double>(points[i][2]);
2279    //
2280    //         if ( numTags > 0) {
2281    //                boost::python::extract<string> ex_str(tags[i]);
2282    //                if  ( ex_str.check() ) {
2283    //                    tag_id=getTag( ex_str());
2284    //                } else {
2285    //                     boost::python::extract<int> ex_int(tags[i]);
2286    //                     if ( ex_int.check() ) {
2287    //                         tag_id=ex_int();
2288    //                     } else {
2289    //                          stringstream temp;
2290    //                          temp << "Error - unable to extract tag for point " << i;
2291    //                          throw FinleyAdapterException(temp.str());
2292    //                    }
2293    //                }
2294    //         }
2295    //            tags_ptr[i]=tag_id;
2296    //       }
2297    //
2298    //       Finley_Mesh_addPoints(mesh, numPoints, points_ptr, tags_ptr);
2299    //       checkPasoError();
2300    //
2301    //       TMPMEMFREE(points_ptr);
2302    //       TMPMEMFREE(tags_ptr);
2303    // }
2304    
2305    /*
2306    void MeshAdapter:: addDiracPoint( const boost::python::list& point, const int tag) const
2307    {
2308        boost::python::list points =  boost::python::list();
2309        boost::python::list tags =  boost::python::list();
2310        points.append(point);
2311        tags.append(tag);
2312        addDiracPoints(points, tags);
2313    }
2314    */
2315    
2316    /*
2317    void MeshAdapter:: addDiracPointWithTagName( const boost::python::list& point, const string& tag) const
2318    {
2319        boost::python::list points =   boost::python::list();
2320        boost::python::list tags =   boost::python::list();
2321        points.append(point);
2322        tags.append(tag);
2323        addDiracPoints(points, tags);
2324    }
2325    */
2326  }  // end of namespace  }  // end of namespace
2327    

Legend:
Removed from v.2989  
changed lines
  Added in v.4437

  ViewVC Help
Powered by ViewVC 1.1.26