Clean up debugging code

This commit is contained in:
Matthias Hochsteger 2024-09-27 11:40:34 +02:00
parent 08a00cb32a
commit dd85220021
5 changed files with 95 additions and 311 deletions

View File

@ -8,7 +8,6 @@
/**************************************************************************/
#include <core/array.hpp>
#include <ostream>
namespace netgen
{
@ -219,11 +218,11 @@ namespace netgen
// print array
template <typename T, int BASE, typename TIND>
inline std::ostream & operator<< (std::ostream & s, const NgFlatArray<T,BASE,TIND> & a)
inline ostream & operator<< (ostream & s, const NgFlatArray<T,BASE,TIND> & a)
{
// for (TIND i = a.Begin(); i < a.End(); i++)
for (auto i : a.Range())
s << i << ": " << a[i] << std::endl;
s << i << ": " << a[i] << endl;
return s;
}
@ -556,10 +555,10 @@ namespace netgen
template <typename T1, typename T2>
inline std::ostream & operator<< (std::ostream & s, const NgIndirectArray<T1,T2> & ia)
inline ostream & operator<< (ostream & s, const NgIndirectArray<T1,T2> & ia)
{
for (int i = ia.Begin(); i < ia.End(); i++)
s << i << ": " << ia[i] << std::endl;
s << i << ": " << ia[i] << endl;
return s;
}

View File

@ -1,16 +1,10 @@
#include "debugging.hpp"
#include <meshing.hpp>
#include <core/python_ngcore.hpp>
#ifdef NETGEN_DEBUGGING_GUI
#include <libusockets.h>
#include "websockets/App.h"
#endif // NETGEN_DEBUGGING_GUI
namespace netgen {
unique_ptr<Mesh> GetOpenElements(const Mesh& m, int dom) {
static Timer t("GetOpenElements");
RegionTimer rt(t);
namespace netgen
{
unique_ptr<Mesh> GetOpenElements( const Mesh & m, int dom = 0 )
{
static Timer t("GetOpenElements"); RegionTimer rt(t);
auto mesh = make_unique<Mesh>();
*mesh = m;
@ -22,15 +16,19 @@ unique_ptr<Mesh> GetOpenElements(const Mesh& m, int dom) {
openelements = mesh->OpenElements();
for (auto & el : openelements)
for (auto i : el.PNums()) interesting_points[i] = true;
for (auto i : el.PNums())
interesting_points[i] = true;
for (auto& el : mesh->VolumeElements()) {
for (auto & el : mesh->VolumeElements())
{
int num_interesting_points = 0;
for (auto pi : el.PNums())
if (interesting_points[pi]) num_interesting_points++;
if(interesting_points[pi])
num_interesting_points++;
if (num_interesting_points == 0) el.Delete();
if(num_interesting_points==0)
el.Delete();
el.SetIndex(num_interesting_points);
}
@ -42,16 +40,15 @@ unique_ptr<Mesh> GetOpenElements(const Mesh& m, int dom) {
mesh->ClearSurfaceElements();
for (auto& el : openelements) mesh->AddSurfaceElement(el);
for (auto & el : openelements)
mesh->AddSurfaceElement( el );
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);
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;
@ -62,23 +59,30 @@ unique_ptr<Mesh> FilterMesh(const Mesh& m, FlatArray<PointIndex> points,
mesh.LineSegments().DeleteAll();
keep_point = false;
for (auto pi : points) keep_point[pi] = true;
for(auto pi : points)
keep_point[pi] = true;
auto set_keep = [&](auto& input, auto& keep_array, auto& els) {
auto set_keep = [&] (auto & input, auto & keep_array, auto & els)
{
keep_array = false;
for (auto ind : input) keep_array[ind] = true;
for(auto ind : input)
keep_array[ind] = true;
for (auto ind : Range(els)) {
for(auto ind : Range(els))
{
bool & keep = keep_array[ind];
if(keep) continue;
for (auto pi : mesh[ind].PNums()) keep |= keep_point[pi];
for(auto pi : mesh[ind].PNums())
keep |= keep_point[pi];
if (!keep) mesh[ind].Delete();
if(!keep)
mesh[ind].Delete();
}
for(auto i = 0; i<els.Size(); i++)
if (els[i].IsDeleted()) {
if(els[i].IsDeleted())
{
els.DeleteElement(i);
i--;
}
@ -91,170 +95,6 @@ unique_ptr<Mesh> FilterMesh(const Mesh& m, FlatArray<PointIndex> points,
return mesh_ptr;
}
#ifdef NETGEN_DEBUGGING_GUI
DebuggingGUI::DebuggingGUI() { Start(); }
DebuggingGUI::~DebuggingGUI() { Stop(); }
struct PerSocketData {};
using WebSocket = uWS::WebSocket<false, true, PerSocketData>;
void DebuggingGUI::Start() {
gui_thread = thread([&]() {
auto a = uWS::App();
loop = a.getLoop();
app = &a;
auto read_file = [](const string& path) {
std::ifstream in(path);
std::stringstream buffer;
buffer << in.rdbuf();
return buffer.str();
};
const string webgui_file = "/home/matthias/src/webgui/dist/webgui.js";
const string main_file = "/home/matthias/a.html";
a.get("/", [&](auto* res, auto* req) { res->end(read_file(main_file)); })
// .any("*", [&](auto* res, auto* req) { cout << "any " << endl; })
.get("/index.html",
[&](auto* res, auto* req) { res->end(read_file(main_file)); })
.get("/webgui.js",
[&](auto* res, auto* req) { res->end(read_file(webgui_file)); })
.connect("/*",
[&](auto* res, auto* req) {
cout << "connect " << req->getUrl() << endl;
})
.listen(7871, [&](auto* token_) {
token = token_;
if (token) {
cout << "Listening on port " << 7871 << endl;
}
});
a.ws<PerSocketData>(
"/ws",
{.compression = uWS::CompressOptions(uWS::DEDICATED_COMPRESSOR_4KB |
uWS::DEDICATED_DECOMPRESSOR),
.maxPayloadLength = 100 * 1024 * 1024,
.idleTimeout = 16,
.maxBackpressure = 100 * 1024 * 1024,
.closeOnBackpressureLimit = false,
.resetIdleTimeoutOnSend = false,
.sendPingsAutomatically = true,
.upgrade = nullptr,
.open =
[&](auto* ws) {
/* Open event here, you may access ws->getUserData() which points
* to a PerSocketData struct */
// cout << "open websocket " << endl;
websockets.insert(ws);
Send(data);
},
.message =
[](auto* ws, std::string_view message, uWS::OpCode opCode) {
cout << "got message " << message << endl;
ws->send(message, opCode, message.length() > 16 * 1024);
},
.dropped =
[](auto* /*ws*/, std::string_view /*message*/,
uWS::OpCode /*opCode*/) {
/* A message was dropped due to set maxBackpressure and
* closeOnBackpressureLimit limit */
cout << "ws: dropped message" << endl;
},
.drain =
[](auto* /*ws*/) {
/* Check ws->getBufferedAmount() here */
cout << "ws: drain" << endl;
},
.close =
[&](auto* ws, int /*code*/, std::string_view /*message*/) {
/* You may access ws->getUserData() here */
// cout << "close" << endl;
websockets.erase(ws);
}});
a.run();
cout << "Exiting GUI thread" << endl;
loop = nullptr;
app = nullptr;
});
}
void DebuggingGUI::Stop() {
cout << "close socket" << endl;
for (auto* ws : websockets) ((WebSocket*)ws)->close();
us_listen_socket_close(0, (struct us_listen_socket_t*)token);
cout << "join thread" << endl;
gui_thread.join();
cout << "joined thread" << endl;
}
void DebuggingGUI::DrawMesh(const Mesh& m) {
py::gil_scoped_acquire acquire;
const auto webgui = py::module::import("netgen.webgui");
const auto dumps = py::module::import("json").attr("dumps");
mesh = make_shared<Mesh>();
*mesh = m;
const auto py_data =
webgui.attr("Draw")(mesh, py::arg("show") = false).attr("GetData")();
const string d = py::cast<string>(dumps(py_data));
data = json::parse(d);
Send(d);
}
void DebuggingGUI::DrawPoints(FlatArray<Point<3>> position, string name,
string color) {
DrawObject(position, "points", name, color);
}
void DebuggingGUI::DrawLines(FlatArray<Point<3>> position, string name,
string color) {
DrawObject(position, "lines", name, color);
}
void DebuggingGUI::DrawTrigs(FlatArray<Point<3>> position, string name,
string color) {
// Array<Point<3>> pnts;
// cout << "trigs " << position << endl;
// for (auto i : Range(position.Size()/3)) {
// pnts.Append(position[3*i+0]);
// pnts.Append(position[3*i+1]);
// pnts.Append(position[3*i+1]);
// pnts.Append(position[3*i+2]);
// pnts.Append(position[3*i+2]);
// pnts.Append(position[3*i+0]);
// }
// cout << "pnts size " << pnts.Size() << endl;
DrawObject(position, "trigs", name, color);
}
void DebuggingGUI::DrawObject(FlatArray<Point<3>> position, string type, string name, string color) {
json p = json::array_t();
for (auto pnt : position) {
p.push_back(pnt[0]);
p.push_back(pnt[1]);
p.push_back(pnt[2]);
}
json d;
d["type"] = type;
d["name"] = name;
d["color"] = color;
d["position"] = p;
data["objects"].push_back(d);
Send(data);
}
void DebuggingGUI::Send(const string& message) {
if (loop) {
((uWS::Loop*)loop)->defer([=]() {
for (auto* ws : websockets) {
((WebSocket*)ws)->send(message, uWS::OpCode::TEXT);
}
});
}
}
DebuggingGUI debug_gui = DebuggingGUI();
#endif // NETGEN_DEBUGGING_GUI
} // namespace netgen

View File

@ -1,53 +1,12 @@
#include "meshing.hpp"
#include "meshclass.hpp"
// #define NETGEN_DEBUGGING_GUI
#ifdef NETGEN_DEBUGGING_GUI
#include "json.hpp"
using json = nlohmann::json;
#endif // NETGEN_DEBUGGING_GUI
namespace netgen {
namespace netgen
{
unique_ptr<Mesh> GetOpenElements( const Mesh & m, int dom = 0 );
unique_ptr<Mesh> FilterMesh(
const Mesh& m, FlatArray<PointIndex> points,
FlatArray<SurfaceElementIndex> sels = Array<SurfaceElementIndex>{},
FlatArray<ElementIndex> els = Array<ElementIndex>{});
unique_ptr<Mesh> FilterMesh( const Mesh & m, FlatArray<PointIndex> points, FlatArray<SurfaceElementIndex> sels = Array<SurfaceElementIndex>{}, FlatArray<ElementIndex> els = Array<ElementIndex>{} );
#ifdef NETGEN_DEBUGGING_GUI
class DebuggingGUI {
public:
DebuggingGUI();
~DebuggingGUI();
void Start();
void Stop();
void DrawMesh(const Mesh& m);
void DrawPoints(FlatArray<Point<3>> position, string name = "points",
string color = "black");
void DrawLines(FlatArray<Point<3>> position, string name = "lines",
string color = "black");
void DrawTrigs(FlatArray<Point<3>> position, string name = "trigs",
string color = "black");
void DrawObject(FlatArray<Point<3>> position, string type, string name,
string color);
void ResetObjects() { data["objects"] = json::array_t(); }
private:
thread gui_thread;
void *app, *loop, *token;
std::set<void*> websockets;
json data;
shared_ptr<Mesh> mesh;
void Send(const json& data) { Send(data.dump()); }
void Send(const string& message);
};
extern DebuggingGUI debug_gui;
#endif // NETGEN_DEBUGGING_GUI
} // namespace netgen
}

View File

@ -563,18 +563,6 @@ namespace netgen
el.SetIndex(m_.domain);
mesh.AddVolumeElement(el);
}
// auto & part_ident = m.GetIdentifications().GetIdentifiedPoints();
// for (auto i : IntRange(1, part_ident.GetNBags()+1))
// for (auto j : IntRange(1, part_ident.GetBagSize(i)+1))
// {
// INDEX_2 i2;
// int nr;
// part_ident.GetData (i, j, i2, nr);
// i2.I1() = pmap[i2.I1()];
// i2.I2() = pmap[i2.I2()];
// ident_points.Set(i2, nr);
// }
}
}
@ -736,8 +724,6 @@ namespace netgen
MeshQuality3d (mesh3d);
}
// debug_gui.DrawMesh("Mesh", mesh3d);
multithread.task = savetask;
return MESHING3_OK;
}