/[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

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

Legend:
Removed from v.1776  
changed lines
  Added in v.4441

  ViewVC Help
Powered by ViewVC 1.1.26