Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visualization of 2D grid functions with 3 vector dimensions #327

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

najlkin
Copy link
Contributor

@najlkin najlkin commented Mar 4, 2025

This addresses the issue of elements with vdim=3 in 2D (or 1D) from #326 and vector elements RT/ND_R2D spaces from #291 .
◀️ dependency: RT/ND_R2D support requires GLVis compiled with mfem/mfem#4731 .
image

@najlkin
Copy link
Contributor Author

najlkin commented Mar 4, 2025

@helloworld922 , can you test this? It worked for me with the reproducer 😉 .

@najlkin najlkin changed the title Visualization of 2D grid functions with 3 vector dimension Visualization of 2D grid functions with 3 vector dimensions Mar 4, 2025
@helloworld922
Copy link

It doesn't crash now, but now it doesn't update the vector data on subsequent plots for time series data.

Here's an updated reproducer:

#include <mfem.hpp>

int main(int argc, char **argv) {
  using namespace mfem;
  int order = 2;
  int vdims = 3;
  int mesh_dims = 2;
  Mesh mesh;
  int nelems1d = 10;
  switch (mesh_dims) {
  case 1:
    mesh = Mesh::MakeCartesian1D(nelems1d);
    break;
  case 2:
    mesh =
        Mesh::MakeCartesian2D(nelems1d, nelems1d, Element::Type::QUADRILATERAL, false, 1, 1);
    break;
  case 3:
    mesh = Mesh::MakeCartesian3D(nelems1d, nelems1d, nelems1d, Element::Type::HEXAHEDRON, 1, 1, 1);
    break;
  }

  L2_FECollection l2_coll(order, mesh_dims);
  FiniteElementSpace vec_space(&mesh, &l2_coll, vdims);
  Array<int> offset(2);
  offset[0] = 0;
  offset[1] = offset[0] + vec_space.GetVSize();
  BlockVector data(offset, Device::GetMemoryType());
  GridFunction vec_gf;
  vec_gf.MakeRef(&vec_space, data, offset[0]);

  mfem::VectorFunctionCoefficient vec_coeff(
      vec_space.GetVDim(),
      std::function<void(const mfem::Vector &, mfem::real_t, mfem::Vector &)>(
          [&](const mfem::Vector &pos, mfem::real_t time, mfem::Vector &res) {
            res[0] = sin(pos[0] + time);
            res[1] = cos(pos[1] + time);
            res[2] = time;
          }));
  mfem::real_t time = 0;
  mfem::real_t dt = 1e-2;
  vec_coeff.SetTime(time);
  vec_gf.ProjectCoefficient(vec_coeff);
  vec_gf.SyncAliasMemory(data);

  socketstream sout;
  char vishost[] = "localhost";
  int visport = 19916;
  sout.open(vishost, visport);
  if (!sout) {
    std::cout << "Unable to connect to GLVis server at " << vishost << ':'
              << visport << std::endl;
    std::cout << "GLVis visualization disabled.\n";
    return -1;
  }
  sout.precision(8);
  sout << "solution\n" << mesh << vec_gf;
  sout << "window_title 't = " << time << "'\n";
  sout << "pause\n";
  sout << std::flush;

  long long step = 0;
  while (true) {
    ++step;
    time = step * dt;
    vec_coeff.SetTime(time);
    vec_gf.ProjectCoefficient(vec_coeff);
    vec_gf.SyncAliasMemory(data);
    if (!sout) {
      break;
    }
    sout << "solution\n" << mesh << vec_gf;
    sout << "window_title 't = " << time << "'\n" << std::flush;
  }
}

@najlkin
Copy link
Contributor Author

najlkin commented Mar 4, 2025

Ok, it should work now 👍 I moved things around a bit.

@helloworld922
Copy link

It looks like it works for L2 spaces now. Thanks!

@mlstowell
Copy link
Contributor

The "updated reproducer" causes GLVis to crash on my Mac. I'm not sure what I might be doing differently from you two.

@najlkin
Copy link
Contributor Author

najlkin commented Mar 5, 2025

@mlstowell , any error message? Maybe try it with MFEM checked out at mfem/mfem#4731 . It is required for that RT/ND_R1/2D elements and should not be necessary without them, but still better to try 😉 .

@mlstowell
Copy link
Contributor

Nothing useful. The reproducer shows

Error in send(): Broken pipe
Error in send(): Broken pipe

GLVis shows:

OpenGL Visualization
Testing if OpenGL core profile window can be created...success!
Handle for window created.
High-dpi display detected: using window scaling: 2 x 2
2025-03-05 11:49:05.706 glvis[95478:5520998] +[IMKClient subclass]: chose IMKClient_Modern
Using GLEW 2.2.0
Using GL 4.1 Metal - 89.3
Loading CoreGLDevice...
Using GLSL 410
Window should be up
Font pattern 'Ubuntu Light:style=Regular' matched no fonts.
Font pattern 'Ubuntu:style=Regular:weight=80' matched no fonts.
Font pattern 'OpenSans:style=Regular' matched no fonts.
Font pattern 'DejaVu Sans:style=Book:width=Normal' matched no fonts.
Font pattern 'DejaVu LGC Sans:style=Book:width=Normal' matched no fonts.
Font pattern 'Bitstream Vera Sans:style=Roman' matched no fonts.
Font pattern 'FreeSans:style=Medium' matched no fonts.
Font pattern 'Ubuntu Mono:style=Regular' matched no fonts.
Font pattern 'DejaVu Sans Mono:style=Book' matched no fonts.
Font pattern 'DejaVu LGC Sans Mono:style=Book' matched no fonts.
Font pattern 'Helvetica:style=Regular' matched fonts:
 - Helvetica:style=Regular,標準體,Almindelig,Κανονικά,Normaali,Courant,רגיל,Szabályos,Regolare,レギュラー,일반체,Regulier,Normal,Обычный,ปกติ,Reguler,Звичайний,Thường,रेग्युलर,Biasa,常规体,عادي:weight=80:file=/System/Library/Fonts/Helvetica.ttc:index=0:scalable=True
Screen DPI: w = 255 ppi, h = 255 ppi (255 x 255)
Fonts use fixed ppi: 192 x 192
Loaded font: /System/Library/Fonts/Helvetica.ttc, index: 0, height: 12
GlVisFont::LoadFont(): font texture dimensions are (1622, 32)
Initializing textures.
VisualizationSceneVector::VisualizationSceneVector
Trace/BPT trap

@mlstowell
Copy link
Contributor

Oops, I see the problem...

@mlstowell
Copy link
Contributor

It's working for me now. I'll run some more testing after lunch but I do like this approach. Thanks @najlkin!

@najlkin
Copy link
Contributor Author

najlkin commented Mar 5, 2025

So have you tracked that down? The thing is GLVis cannot require the latest MFEM. It is ok for new features like that RT/ND_R1/2D elements, but not the core things 🤔 .

@mlstowell
Copy link
Contributor

@najlkin, have a look at PR #329 which improves the vector scaling. I'm still seeing other issues but this may correct some of them

@mlstowell
Copy link
Contributor

Another issue I'm seeing is that sometimes the GLVis window is empty. I believe this sometimes happens when the z-coordinates are uninitialized and the scaling of the image is not computed correctly. I was looking at setting the z-coordinate in StreamState::Extrude2D3VMeshAndSolution but I'm not sure if that's the best solution.

@najlkin
Copy link
Contributor Author

najlkin commented Mar 6, 2025

Another issue I'm seeing is that sometimes the GLVis window is empty. I believe this sometimes happens when the z-coordinates are uninitialized and the scaling of the image is not computed correctly. I was looking at setting the z-coordinate in StreamState::Extrude2D3VMeshAndSolution but I'm not sure if that's the best solution.

I have not seen it, but then it is a bug of Mesh::SetCurvature().

@najlkin
Copy link
Contributor Author

najlkin commented Mar 6, 2025

Hmm, XYZ_VectorFunction in mesh.cpp, which is used for the projection, looks fine 🤔 .

@mlstowell
Copy link
Contributor

Another issue I'm seeing is that sometimes the GLVis window is empty. I believe this sometimes happens when the z-coordinates are uninitialized and the scaling of the image is not computed correctly. I was looking at setting the z-coordinate in StreamState::Extrude2D3VMeshAndSolution but I'm not sure if that's the best solution.

I have not seen it, but then it is a bug of Mesh::SetCurvature().

Yes, exactly. I was debating where it should be fixed. I think you're correct. If Mesh::SetCurvature is being used to increase the space dimension then it must ensure that the new coordinate is valid everywhere.

@najlkin
Copy link
Contributor Author

najlkin commented Mar 6, 2025

We may include it to mfem/mfem#4731 , but I do not know where it is coming from. Maybe something relies on the vertices, which are still 2D, while Nodes are 3D 🤔 However, I do not know what and where. Valgrind does not show anything useful.

@najlkin
Copy link
Contributor Author

najlkin commented Mar 6, 2025

Yeah, it seems that SetVerticesFromNodes(Nodes); at the end of SetCurvature() solves that, but I am not sure it is the right thing to do. It would be better to know what uses that invalid vertices 🤔 .

@najlkin
Copy link
Contributor Author

najlkin commented Mar 6, 2025

Ok, I added such fix to mfem/mfem#4731 , because many methods in GLVis rely on vertices instead of nodes and there is no way to know what dimension they have after the change as the higher ones are simply uninitialized 💣 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants