File: debugging.cpp

package info (click to toggle)
netgen 6.2.2601%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,076 kB
  • sloc: cpp: 166,627; tcl: 6,310; python: 2,868; sh: 528; makefile: 90
file content (223 lines) | stat: -rw-r--r-- 7,591 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <meshing.hpp>
#include "debugging.hpp"

namespace netgen
{
    unique_ptr<Mesh> GetOpenElements( const Mesh & m, int dom, bool only_quads )
    {
        static Timer t("GetOpenElements"); RegionTimer rt(t);
        auto mesh = make_unique<Mesh>();
        *mesh = m;

        Array<bool, PointIndex> interesting_points(mesh->GetNP());
        interesting_points = false;

        mesh->FindOpenElements(dom);
        NgArray<Element2d> openelements;
        openelements = mesh->OpenElements();

        for (auto & el : openelements)
            for (auto i : el.PNums())
                interesting_points[i] = true;

        for (auto & el : mesh->VolumeElements())
        {
            int num_interesting_points = 0;

            for (auto pi : el.PNums())
                if(interesting_points[pi])
                    num_interesting_points++;

            if(num_interesting_points==0)
                el.Delete();
            el.SetIndex(num_interesting_points);
        }

        mesh->SetMaterial(1, "1_point");
        mesh->SetMaterial(2, "2_points");
        mesh->SetMaterial(3, "3_points");
        mesh->SetMaterial(4, "4_points");

        mesh->ClearSurfaceElements();

        for (auto & el : openelements)
            if(!only_quads || el.GetNP() == 4)
                mesh->AddSurfaceElement( el );

        mesh->Compress();
        return mesh;
    }

    unique_ptr<Mesh> FilterMesh( const Mesh & m, FlatArray<PointIndex> points, FlatArray<SurfaceElementIndex> sels, FlatArray<ElementIndex> els )
    {
        static Timer t("GetOpenElements"); RegionTimer rt(t);
        auto mesh_ptr = make_unique<Mesh>();
        auto & mesh = *mesh_ptr;
        mesh = m;

        Array<bool, PointIndex> keep_point(mesh.GetNP());
        Array<bool, SurfaceElementIndex> keep_sel(mesh.GetNSE());
        Array<bool, ElementIndex> keep_el(mesh.GetNE());
        mesh.LineSegments().DeleteAll();

        keep_point = false;
        for(auto pi : points)
            keep_point[pi] = true;

        auto set_keep = [&] (auto & input, auto & keep_array, auto & els)
        {
            keep_array = false;
            for(auto ind : input)
                keep_array[ind] = true;

            for(auto ind : Range(els))
            {
                bool & keep = keep_array[ind];
                if(keep) continue;

                for(auto pi : mesh[ind].PNums())
                    keep |= keep_point[pi];

                if(!keep)
                    mesh[ind].Delete();
            }

            for(auto i = 0; i<els.Size(); i++)
                if(els[i].IsDeleted())
                {
                    els.DeleteElement(i);
                    i--;
                }
        };

        set_keep(sels, keep_sel, mesh.SurfaceElements());
        set_keep(els, keep_el, mesh.VolumeElements());
        //mesh.Compress();

        return mesh_ptr;
    }

    void CheckMesh (const Mesh& mesh, MESHING_STEP step)
    {
      if (step == MESHCONST_OPTVOLUME)
        {
          bool have_error = false;
          for (auto el : mesh.VolumeElements())
            {
              double volume = el.Volume(mesh.Points());
              if (volume < 0)
                {
                  have_error = true;
                  cout << "volume of element " << el << " is negative: " << volume << endl;
                }
            }
          if (have_error)
            throw Exception("Negative volume");

        CheckElementsAroundEdges(mesh);
        }
    }

    void CheckElementsAroundEdges (const Mesh& mesh)
    {
      static Mesh last_good_mesh;

      Array<std::tuple<PointIndex, PointIndex>> edges;
      auto elementsonnode = mesh.CreatePoint2ElementTable();
      BuildEdgeList(mesh, elementsonnode, edges);
      mesh.BoundaryEdge(1, 2); // trigger build of boundary edges

      ArrayMem<ElementIndex, 20> hasbothpoints;
      for (auto [pi0, pi1] : edges)
        {
          if (mesh.BoundaryEdge(pi0, pi1))
            continue;

          hasbothpoints.SetSize(0);
          for (ElementIndex ei : elementsonnode[pi0])
            if (mesh[ei].PNums().Contains(pi1))
              hasbothpoints.Append(ei);

          bool skip = false;
          for (ElementIndex ei : hasbothpoints)
            {
              if (mesh[ei].GetType() != TET)
                {
                  skip = true;
                  break;
                }
            }
          if (skip)
            continue;

          int nsuround = hasbothpoints.Size();
          ArrayMem<PointIndex, 50> suroundpts(nsuround + 1);
          suroundpts = PointIndex::INVALID;
          ArrayMem<bool, 50> tetused(nsuround);
          tetused = false;
          tetused[0] = true;

          auto el = mesh[hasbothpoints[0]];
          PointIndex pi2 = PointIndex::INVALID;
          PointIndex pi3 = PointIndex::INVALID;
          for (auto pi : el.PNums())
            if (pi != pi0 && pi != pi1)
              {
                pi3 = pi2;
                pi2 = pi;
              }
          suroundpts[0] = pi2;
          suroundpts[1] = pi3;

          for (auto i : Range(2, nsuround + 1))
            {
              PointIndex oldpi = suroundpts[i - 1];
              PointIndex newpi = PointIndex::INVALID;

              for (int k = 0; k < nsuround && !newpi.IsValid(); k++)
                if (!tetused[k])
                  {
                    const Element& nel = mesh[hasbothpoints[k]];
                    for (int k2 = 0; k2 < 4 && !newpi.IsValid(); k2++)
                      if (nel[k2] == oldpi)
                        {
                          newpi = nel[0] - pi0 + nel[1] - pi1 + nel[2] - oldpi + nel[3];
                          tetused[k] = true;
                          suroundpts[i] = newpi;

                          ArrayMem<PointIndex, 4> nelpts{nel[0], nel[1], nel[2], nel[3]};
                          ArrayMem<PointIndex, 4> check_points{pi0, pi1, oldpi, newpi};
                          QuickSort(check_points);
                          QuickSort(nelpts);
                          if (check_points != nelpts)
                            {
                              cout << __FILE__ << ":" << __LINE__ << "\tFound error" << endl;
                              cout << "i = " << i << endl;
                              cout << "oldpi = " << oldpi << endl;
                              cout << "newpi = " << newpi << endl;
                              cout << "Elements: " << endl;
                              cout << "nel " << nel << endl;
                              for (auto ei : hasbothpoints)
                                cout << mesh[ei] << endl;
                              cout << endl;
                              cout << "check_points: " << check_points << endl;
                              cout << "nelpts: " << nelpts << endl;
                              cout << "hasbothpoints: " << hasbothpoints << endl;
                              cout << "suroundpts: " << suroundpts << endl;
                              throw Exception("Found error");
                            }
                        }
                  }
            }
          if (suroundpts.Last() != suroundpts[0])
            {
              cout << __FILE__ << ":" << __LINE__ << "\tFound error" << endl;
              cout << "hasbothpoints: " << hasbothpoints << endl;
              cout << "suroundpts: " << suroundpts << endl;
              for (auto ei : hasbothpoints)
                cout << mesh[ei] << endl;
              throw Exception("Found error");
            }
        }
    }
} // namespace netgen