<commit_before>#include "apfSIM.h"
#include "apfSIMDataOf.h"
#include <apf.h>
#include <apfShape.h>
#include <SimModel.h>
#include <MeshSim.h>
#include <SimPartitionedMesh.h>
#include <gmi.h>
#include <gmi_sim.h>

namespace apf {

Field * createSIMField(Mesh * m, const char * name, int valueType, FieldShape * shape)
{
  return makeField(m,name,valueType,0,shape,new SIMDataOf<double>);
}

Field * createSIMLagrangeField(Mesh * m, const char * name, int valueType, int order)
{
  return createSIMField(m,name,valueType,getLagrange(order));
}

Field * createSIMFieldOn(Mesh * m, const char * name, int valueType)
{
  return createField(m,name,valueType,m->getShape());
}

MeshSIM::MeshSIM(pParMesh m):
  mesh(m)
{
  part = PM_mesh(mesh,0);
  d = M_numRegions(part) ? 3 : 2;
  iterDim = -1;
  model = gmi_import_sim(M_model(part));
}

MeshSIM::~MeshSIM()
{
  gmi_destroy(model);
}

int MeshSIM::getDimension()
{
  return d;
}

class IteratorSIM
{
  public:
    virtual ~IteratorSIM() {}
    virtual MeshEntity* iterate() = 0;
};

class VertexIteratorSIM : public IteratorSIM
{
  public:
    VertexIteratorSIM(pMesh part)
    {iterator = M_vertexIter(part);}
    virtual ~VertexIteratorSIM()
    {VIter_delete(iterator);}
    virtual MeshEntity* iterate()
    {return reinterpret_cast<MeshEntity*>(VIter_next(iterator));}
  private:
    VIter iterator;
};

class EdgeIteratorSIM : public IteratorSIM
{
  public:
    EdgeIteratorSIM(pMesh part)
    {iterator = M_edgeIter(part);}
    virtual ~EdgeIteratorSIM()
    {EIter_delete(iterator);}
    virtual MeshEntity* iterate()
    {return reinterpret_cast<MeshEntity*>(EIter_next(iterator));}
  private:
    EIter iterator;
};

class FaceIteratorSIM : public IteratorSIM
{
  public:
    FaceIteratorSIM(pMesh part)
    {iterator = M_faceIter(part);}
    virtual ~FaceIteratorSIM()
    {FIter_delete(iterator);}
    virtual MeshEntity* iterate()
    {return reinterpret_cast<MeshEntity*>(FIter_next(iterator));}
  private:
    FIter iterator;
};

class RegionIteratorSIM : public IteratorSIM
{
  public:
    RegionIteratorSIM(pMesh part)
    {iterator = M_regionIter(part);}
    virtual ~RegionIteratorSIM()
    {RIter_delete(iterator);}
    virtual MeshEntity* iterate()
    {return reinterpret_cast<MeshEntity*>(RIter_next(iterator));}
  private:
    RIter iterator;
};

std::size_t MeshSIM::count(int dimension)
{
  if (dimension == 0)
    return M_numVertices(part);
  if (dimension == 1)
    return M_numEdges(part);
  if (dimension == 2)
    return M_numFaces(part);
  return M_numRegions(part);
}

MeshIterator* MeshSIM::begin(int dimension)
{
  IteratorSIM* iterator = 0;
  if (dimension == 0)
    iterator = new VertexIteratorSIM(part);
  if (dimension == 1)
    iterator = new EdgeIteratorSIM(part);
  if (dimension == 2)
    iterator = new FaceIteratorSIM(part);
  if (dimension == 3)
    iterator = new RegionIteratorSIM(part);
  return reinterpret_cast<MeshIterator*>(iterator);
}

MeshEntity* MeshSIM::iterate(MeshIterator* it)
{
  return reinterpret_cast<IteratorSIM*>(it)->iterate();
}

void MeshSIM::end(MeshIterator* it)
{
  delete reinterpret_cast<IteratorSIM*>(it);
}

bool MeshSIM::isShared(MeshEntity* e)
{
  return EN_isOnPartBdry(reinterpret_cast<pEntity>(e));
}

bool MeshSIM::isOwned(MeshEntity* e)
{
  return EN_isOwnerProc(reinterpret_cast<pEntity>(e));
}

int MeshSIM::getOwner(MeshEntity* e)
{
  return EN_ownerProc(reinterpret_cast<pEntity>(e));
}

static void pListToDynamicArray(pPList list, DynamicArray<MeshEntity*>& array)
{
  int n = PList_size(list);
  array.setSize(n);
  for (int i=0; i < n; ++i)
    array[i] = reinterpret_cast<MeshEntity*>(PList_item(list,i));
  PList_delete(list);
}

static int pListToArray(pPList list, MeshEntity** array)
{
  int n = PList_size(list);
  for (int i=0; i < n; ++i)
    array[i] = reinterpret_cast<MeshEntity*>(PList_item(list,i));
  PList_delete(list);
  return n;
}

void MeshSIM::getAdjacent(MeshEntity* e,
    int dimension,
    DynamicArray<MeshEntity*>& adjacent)
{
  pEntity entity = reinterpret_cast<pEntity>(e);
  eType ent_type = EN_type(entity);
  if (apf::getDimension(this, e) == dimension)
  {
    adjacent.setSize(1);
    adjacent[0] = e;
    return;
  }
  if (ent_type == Tvertex)
  {
    pVertex vertex = static_cast<pVertex>(entity);
    if (dimension == 1)
    {
      int n = V_numEdges(vertex);
      adjacent.setSize(n);
      for (int i=0; i < n; ++i)
        adjacent[i] = reinterpret_cast<MeshEntity*>(V_edge(vertex,i));
    }
    if (dimension == 2)
      pListToDynamicArray(V_faces(vertex),adjacent);
    if (dimension == 3)
      pListToDynamicArray(V_regions(vertex),adjacent);
  }
  if (ent_type == Tedge)
  {
    pEdge edge = static_cast<pEdge>(entity);
    if (dimension == 0)
    {
      adjacent.setSize(2);
      for (int i=0; i < 2; ++i)
        adjacent[i] = reinterpret_cast<MeshEntity*>(E_vertex(edge,i));
    }
    if (dimension == 2)
    {
      int n = E_numFaces(edge);
      adjacent.setSize(n);
      for (int i=0; i < n; ++i)
        adjacent[i] = reinterpret_cast<MeshEntity*>(E_face(edge,i));
    }
    if (dimension == 3)
      pListToDynamicArray(E_regions(edge),adjacent);
  }
  if (ent_type == Tface)
  {
    pFace face = static_cast<pFace>(entity);
    if (dimension == 0)
      pListToDynamicArray(F_vertices(face,1),adjacent);
    if (dimension == 1)
    {
      int n = F_numEdges(face);
      adjacent.setSize(n);
      for (int i=0; i < n; ++i)
        adjacent[i] = reinterpret_cast<MeshEntity*>(F_edge(face,i));
    }
    if (dimension == 3)
    {
      adjacent.setSize(countUpward(e));
      int a=0;
      for (int i=0; i < 2; ++i)
        adjacent[a++] = reinterpret_cast<MeshEntity*>(F_region(face,i));
    }
  }
  if (ent_type == Tregion)
  {
    pRegion region = static_cast<pRegion>(entity);<commit_msg>fixed logic in MeshSIM::getAdjacent when finding regions adjacent to a face that caused a list of size 2 to be returned even when only one region was adjacent to a face, resulting in NULL pointer errors<commit_after>#include "apfSIM.h"
#include "apfSIMDataOf.h"
#include <apf.h>
#include <apfShape.h>
#include <SimModel.h>
#include <MeshSim.h>
#include <SimPartitionedMesh.h>
#include <gmi.h>
#include <gmi_sim.h>

namespace apf {

Field * createSIMField(Mesh * m, const char * name, int valueType, FieldShape * shape)
{
  return makeField(m,name,valueType,0,shape,new SIMDataOf<double>);
}

Field * createSIMLagrangeField(Mesh * m, const char * name, int valueType, int order)
{
  return createSIMField(m,name,valueType,getLagrange(order));
}

Field * createSIMFieldOn(Mesh * m, const char * name, int valueType)
{
  return createField(m,name,valueType,m->getShape());
}

MeshSIM::MeshSIM(pParMesh m):
  mesh(m)
{
  part = PM_mesh(mesh,0);
  d = M_numRegions(part) ? 3 : 2;
  iterDim = -1;
  model = gmi_import_sim(M_model(part));
}

MeshSIM::~MeshSIM()
{
  gmi_destroy(model);
}

int MeshSIM::getDimension()
{
  return d;
}

class IteratorSIM
{
  public:
    virtual ~IteratorSIM() {}
    virtual MeshEntity* iterate() = 0;
};

class VertexIteratorSIM : public IteratorSIM
{
  public:
    VertexIteratorSIM(pMesh part)
    {iterator = M_vertexIter(part);}
    virtual ~VertexIteratorSIM()
    {VIter_delete(iterator);}
    virtual MeshEntity* iterate()
    {return reinterpret_cast<MeshEntity*>(VIter_next(iterator));}
  private:
    VIter iterator;
};

class EdgeIteratorSIM : public IteratorSIM
{
  public:
    EdgeIteratorSIM(pMesh part)
    {iterator = M_edgeIter(part);}
    virtual ~EdgeIteratorSIM()
    {EIter_delete(iterator);}
    virtual MeshEntity* iterate()
    {return reinterpret_cast<MeshEntity*>(EIter_next(iterator));}
  private:
    EIter iterator;
};

class FaceIteratorSIM : public IteratorSIM
{
  public:
    FaceIteratorSIM(pMesh part)
    {iterator = M_faceIter(part);}
    virtual ~FaceIteratorSIM()
    {FIter_delete(iterator);}
    virtual MeshEntity* iterate()
    {return reinterpret_cast<MeshEntity*>(FIter_next(iterator));}
  private:
    FIter iterator;
};

class RegionIteratorSIM : public IteratorSIM
{
  public:
    RegionIteratorSIM(pMesh part)
    {iterator = M_regionIter(part);}
    virtual ~RegionIteratorSIM()
    {RIter_delete(iterator);}
    virtual MeshEntity* iterate()
    {return reinterpret_cast<MeshEntity*>(RIter_next(iterator));}
  private:
    RIter iterator;
};

std::size_t MeshSIM::count(int dimension)
{
  if (dimension == 0)
    return M_numVertices(part);
  if (dimension == 1)
    return M_numEdges(part);
  if (dimension == 2)
    return M_numFaces(part);
  return M_numRegions(part);
}

MeshIterator* MeshSIM::begin(int dimension)
{
  IteratorSIM* iterator = 0;
  if (dimension == 0)
    iterator = new VertexIteratorSIM(part);
  if (dimension == 1)
    iterator = new EdgeIteratorSIM(part);
  if (dimension == 2)
    iterator = new FaceIteratorSIM(part);
  if (dimension == 3)
    iterator = new RegionIteratorSIM(part);
  return reinterpret_cast<MeshIterator*>(iterator);
}

MeshEntity* MeshSIM::iterate(MeshIterator* it)
{
  return reinterpret_cast<IteratorSIM*>(it)->iterate();
}

void MeshSIM::end(MeshIterator* it)
{
  delete reinterpret_cast<IteratorSIM*>(it);
}

bool MeshSIM::isShared(MeshEntity* e)
{
  return EN_isOnPartBdry(reinterpret_cast<pEntity>(e));
}

bool MeshSIM::isOwned(MeshEntity* e)
{
  return EN_isOwnerProc(reinterpret_cast<pEntity>(e));
}

int MeshSIM::getOwner(MeshEntity* e)
{
  return EN_ownerProc(reinterpret_cast<pEntity>(e));
}

static void pListToDynamicArray(pPList list, DynamicArray<MeshEntity*>& array)
{
  int n = PList_size(list);
  array.setSize(n);
  for (int i=0; i < n; ++i)
    array[i] = reinterpret_cast<MeshEntity*>(PList_item(list,i));
  PList_delete(list);
}

static int pListToArray(pPList list, MeshEntity** array)
{
  int n = PList_size(list);
  for (int i=0; i < n; ++i)
    array[i] = reinterpret_cast<MeshEntity*>(PList_item(list,i));
  PList_delete(list);
  return n;
}

void MeshSIM::getAdjacent(MeshEntity* e,
    int dimension,
    DynamicArray<MeshEntity*>& adjacent)
{
  pEntity entity = reinterpret_cast<pEntity>(e);
  eType ent_type = EN_type(entity);
  if (apf::getDimension(this, e) == dimension)
  {
    adjacent.setSize(1);
    adjacent[0] = e;
    return;
  }
  if (ent_type == Tvertex)
  {
    pVertex vertex = static_cast<pVertex>(entity);
    if (dimension == 1)
    {
      int n = V_numEdges(vertex);
      adjacent.setSize(n);
      for (int i=0; i < n; ++i)
        adjacent[i] = reinterpret_cast<MeshEntity*>(V_edge(vertex,i));
    }
    if (dimension == 2)
      pListToDynamicArray(V_faces(vertex),adjacent);
    if (dimension == 3)
      pListToDynamicArray(V_regions(vertex),adjacent);
  }
  if (ent_type == Tedge)
  {
    pEdge edge = static_cast<pEdge>(entity);
    if (dimension == 0)
    {
      adjacent.setSize(2);
      for (int i=0; i < 2; ++i)
        adjacent[i] = reinterpret_cast<MeshEntity*>(E_vertex(edge,i));
    }
    if (dimension == 2)
    {
      int n = E_numFaces(edge);
      adjacent.setSize(n);
      for (int i=0; i < n; ++i)
        adjacent[i] = reinterpret_cast<MeshEntity*>(E_face(edge,i));
    }
    if (dimension == 3)
      pListToDynamicArray(E_regions(edge),adjacent);
  }
  if (ent_type == Tface)
  {
    pFace face = static_cast<pFace>(entity);
    if (dimension == 0)
      pListToDynamicArray(F_vertices(face,1),adjacent);
    if (dimension == 1)
    {
      int n = F_numEdges(face);
      adjacent.setSize(n);
      for (int i=0; i < n; ++i)
        adjacent[i] = reinterpret_cast<MeshEntity*>(F_edge(face,i));
    }
    if (dimension == 3)
    {
      adjacent.setSize(countUpward(e));
      int a=0;
      for (int i=0; i < 2; ++i)
      {
	MeshEntity * me =  reinterpret_cast<MeshEntity*>(F_region(face,i));
	if(me != NULL)
	  adjacent[a++] = me;
      }
    }
  }
  if (ent_type == Tregion)
  {
    pRegion region = static_cast<pRegion>(entity);
{'fraction_non_alphanumeric': 0.14104515474378487, 'fraction_numerical': 0.006341958396752917, 'mean_word_length': 4.142173913043478, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3576582', 'n_tokens_mistral': 4421, 'n_tokens_neox': 4251, 'n_words': 861}