Merge branch 'webgui_debugging' into boundarylayer_fixes

This commit is contained in:
Matthias Hochsteger 2024-03-22 15:44:55 +01:00
commit a0788a4928
5 changed files with 268 additions and 95 deletions

View File

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

View File

@ -15,7 +15,7 @@ target_sources(nglib PRIVATE
boundarylayer2d.cpp boundarylayer2d.cpp
) )
target_link_libraries( nglib PRIVATE netgen_metis "$<BUILD_INTERFACE:netgen_python>" ) target_link_libraries( nglib PRIVATE usockets netgen_metis "$<BUILD_INTERFACE:netgen_python>" )
install(FILES install(FILES
adfront2.hpp adfront3.hpp basegeom.hpp bcfunctions.hpp bisect.hpp adfront2.hpp adfront3.hpp basegeom.hpp bcfunctions.hpp bisect.hpp

View File

@ -1,10 +1,19 @@
#include <meshing.hpp> #include "debugging.hpp"
namespace netgen #include <libusockets.h>
{
unique_ptr<Mesh> GetOpenElements( const Mesh & m, int dom = 0 ) #include <core/python_ngcore.hpp>
{
static Timer t("GetOpenElements"); RegionTimer rt(t); #include "pybind11/gil.h"
#ifdef NETGEN_DEBUGGING_GUI
#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);
auto mesh = make_unique<Mesh>(); auto mesh = make_unique<Mesh>();
*mesh = m; *mesh = m;
@ -16,19 +25,15 @@ namespace netgen
openelements = mesh->OpenElements(); openelements = mesh->OpenElements();
for (auto& el : openelements) for (auto& el : openelements)
for (auto i : el.PNums()) for (auto i : el.PNums()) interesting_points[i] = true;
interesting_points[i] = true;
for (auto & el : mesh->VolumeElements()) for (auto& el : mesh->VolumeElements()) {
{
int num_interesting_points = 0; int num_interesting_points = 0;
for (auto pi : el.PNums()) for (auto pi : el.PNums())
if(interesting_points[pi]) if (interesting_points[pi]) num_interesting_points++;
num_interesting_points++;
if(num_interesting_points==0) if (num_interesting_points == 0) el.Delete();
el.Delete();
el.SetIndex(num_interesting_points); el.SetIndex(num_interesting_points);
} }
@ -40,15 +45,16 @@ namespace netgen
mesh->ClearSurfaceElements(); mesh->ClearSurfaceElements();
for (auto & el : openelements) for (auto& el : openelements) mesh->AddSurfaceElement(el);
mesh->AddSurfaceElement( el );
return mesh; return mesh;
} }
unique_ptr<Mesh> FilterMesh( const Mesh & m, FlatArray<PointIndex> points, FlatArray<SurfaceElementIndex> sels, FlatArray<ElementIndex> els ) unique_ptr<Mesh> FilterMesh(const Mesh& m, FlatArray<PointIndex> points,
{ FlatArray<SurfaceElementIndex> sels,
static Timer t("GetOpenElements"); RegionTimer rt(t); FlatArray<ElementIndex> els) {
static Timer t("GetOpenElements");
RegionTimer rt(t);
auto mesh_ptr = make_unique<Mesh>(); auto mesh_ptr = make_unique<Mesh>();
auto& mesh = *mesh_ptr; auto& mesh = *mesh_ptr;
mesh = m; mesh = m;
@ -59,30 +65,23 @@ namespace netgen
mesh.LineSegments().DeleteAll(); mesh.LineSegments().DeleteAll();
keep_point = false; keep_point = false;
for(auto pi : points) for (auto pi : points) keep_point[pi] = true;
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; keep_array = false;
for(auto ind : input) for (auto ind : input) keep_array[ind] = true;
keep_array[ind] = true;
for(auto ind : Range(els)) for (auto ind : Range(els)) {
{
bool& keep = keep_array[ind]; bool& keep = keep_array[ind];
if (keep) continue; if (keep) continue;
for(auto pi : mesh[ind].PNums()) for (auto pi : mesh[ind].PNums()) keep |= keep_point[pi];
keep |= keep_point[pi];
if(!keep) if (!keep) mesh[ind].Delete();
mesh[ind].Delete();
} }
for (auto i = 0; i < els.Size(); i++) for (auto i = 0; i < els.Size(); i++)
if(els[i].IsDeleted()) if (els[i].IsDeleted()) {
{
els.DeleteElement(i); els.DeleteElement(i);
i--; i--;
} }
@ -95,6 +94,138 @@ namespace netgen
return mesh_ptr; 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 string& name, 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");
auto smesh = make_shared<Mesh>();
*smesh = m;
const auto py_data =
webgui.attr("Draw")(smesh, py::arg("show") = false).attr("GetData")();
const string d = py::cast<string>(dumps(py_data));
data = json::parse(d);
Send(d);
}
void DebuggingGUI::DrawPoints(const string& name, const Mesh& m,
FlatArray<PointIndex> points) {}
void DebuggingGUI::DrawLines(const string& name, const Mesh& m,
FlatArray<SegmentIndex> lines) {}
void DebuggingGUI::DrawTrigs(const string& name, const Mesh& m,
FlatArray<SurfaceElementIndex> trigs) {}
void DebuggingGUI::DrawTets(const string& name, const Mesh& m,
FlatArray<ElementIndex> tets) {}
void DebuggingGUI::AddComponent(const Mesh& m) {}
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,12 +1,51 @@
#include "meshclass.hpp" #include "meshing.hpp"
#define NETGEN_DEBUGGING_GUI
namespace netgen #ifdef NETGEN_DEBUGGING_GUI
{ #include "json.hpp"
using json = nlohmann::json;
#endif // NETGEN_DEBUGGING_GUI
namespace netgen {
unique_ptr<Mesh> GetOpenElements(const Mesh& m, int dom = 0); 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 string& name, const Mesh& m);
void DrawPoints(const string& name, const Mesh& m,
FlatArray<PointIndex> points);
void DrawLines(const string& name, const Mesh& m,
FlatArray<SegmentIndex> lines);
void DrawTrigs(const string& name, const Mesh& m,
FlatArray<SurfaceElementIndex> trigs);
void DrawTets(const string& name, const Mesh& m,
FlatArray<ElementIndex> tets);
void AddComponent(const Mesh& m);
private:
thread gui_thread;
void *app, *loop, *token;
std::set<void*> websockets;
json data;
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

@ -739,6 +739,8 @@ namespace netgen
MeshQuality3d (mesh3d); MeshQuality3d (mesh3d);
} }
debug_gui.DrawMesh("Mesh", mesh3d);
multithread.task = savetask; multithread.task = savetask;
return MESHING3_OK; return MESHING3_OK;
} }