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

Diff of /branches/diaplayground/finley/src/CPPAdapter/MeshAdapter.cpp

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

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

Legend:
Removed from v.1366  
changed lines
  Added in v.5122

  ViewVC Help
Powered by ViewVC 1.1.26