File: custom_gui.cpp

package info (click to toggle)
gmsh 4.15.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 52,880 kB
  • sloc: cpp: 440,657; ansic: 114,930; f90: 15,611; python: 13,907; yacc: 7,438; java: 3,491; lisp: 3,206; lex: 633; perl: 571; makefile: 500; xml: 414; sh: 407; javascript: 113; modula3: 32
file content (127 lines) | stat: -rw-r--r-- 4,593 bytes parent folder | download | duplicates (5)
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
#include <cmath>
#include <thread>
#include <set>
#include "gmsh.h"

// This example shows how to implement a custom user interface running
// computationally expensive calculations in separate threads. The threads can
// update the user interface in real-time.

// flag that will be set to interrupt a calculation
bool stop_computation = false;

// a computationally expensive routine, that will be run in its own thread
void compute(const std::string &arg)
{
  std::vector<double> iterations, progress;
  gmsh::onelab::getNumber("My App/Iterations", iterations);
  gmsh::onelab::getNumber("My App/Show progress?", progress);
  int n = iterations.size() ? static_cast<int>(iterations[0]) : 1;
  bool show = (progress.size() && progress[0]) ? true : false;
  int p = 0;
  double k = 0., last_refresh = 0.;
  for(int j = 0; j < n; j++) {
    // stop computation if requested by clicking on "Stop it!"
    if(stop_computation) break;
    k = sin(k) + cos(j / 45.);
    // show progress in real time?
    if(show && n > 1 && !(j % (n / 100))) {
      p++;
      gmsh::onelab::setString(arg, {std::to_string(p) + "%"});
      // any code in a thread other than the main thread that modifies the user
      // interface should be locked
      gmsh::fltk::lock();
      gmsh::logger::write(arg + " progress " + std::to_string(p) + "%");
      gmsh::fltk::unlock();
      // ask the main thread to process pending events and to update the user
      // interface, maximum 10 times per second
      if(gmsh::logger::getWallTime() - last_refresh > 0.1) {
        last_refresh = gmsh::logger::getWallTime();
        gmsh::fltk::awake("update");
      }
    }
  }
  gmsh::onelab::setNumber(arg + " result", {k});
  gmsh::onelab::setString("ONELAB/Action", {"done computing"});
  gmsh::fltk::awake("update");
}

bool checkForEvent(const std::string &parameters)
{
  std::vector<std::string> action;
  gmsh::onelab::getString("ONELAB/Action", action);
  if(action.empty()) {
  }
  else if(action[0] == "should compute") {
    gmsh::onelab::setString("ONELAB/Action", {""});
    gmsh::onelab::setString("ONELAB/Button", {"Stop!", "should stop"});
    // force interface update (to show the new button label)
    gmsh::fltk::update();
    // start computationally intensive calculations in their own threads
    std::vector<double> v;
    gmsh::onelab::getNumber("My App/Number of threads", v);
    int n = v.size() ? static_cast<int>(v[0]) : 1;
    for(unsigned int i = 0; i < n; i++) {
      std::thread t(compute, "My App/Thread " + std::to_string(i + 1));
      t.detach();
    }
  }
  else if(action[0] == "should stop") {
    stop_computation = true;
  }
  else if(action[0] == "done computing") {
    // should not detach threads, and join them all here
    gmsh::onelab::setString("ONELAB/Action", {""});
    gmsh::onelab::setString("ONELAB/Button", {"Do it!", "should compute"});
    gmsh::fltk::update();
    stop_computation = false;
  }
  else if(action[0] == "reset") {
    // user clicked on "Reset database"
    gmsh::onelab::setString("ONELAB/Action", {""});
    gmsh::onelab::set(parameters);
    gmsh::fltk::update();
  }
  else if(action[0] == "check") {
    // could perform action here after each change in ONELAB parameters,
    // e.g. rebuild a CAD model, update other parameters, ...
  }
  return true;
}

int main(int argc, char **argv)
{
  gmsh::initialize();

  // hide the standard Gmsh modules
  gmsh::option::setNumber("General.ShowModuleMenu", 0);

  // create some ONELAB parameters to control the number of iterations and
  // threads, the progress display and the custom ONELAB button (when pressed,
  // it will set the "ONELAB/Action" parameter to "should compute")
  std::string parameters = R"( [
    { "type":"number", "name":"My App/Iterations", "values":[1e6], "min":1e4,
      "max":1e9, "step":1e5, "attributes":{"Highlight":"AliceBlue"} },
    { "type":"number", "name":"My App/Number of threads", "values":[2],
      "choices":[1, 2, 3, 4], "attributes":{"Highlight":"AliceBlue"} },
    { "type":"number", "name":"My App/Show progress?", "values":[1],
      "choices":[0, 1] },
    { "type":"string", "name":"ONELAB/Button", "values":["Do it!", "should compute"],
      "visible":false }
  ] )";

  gmsh::onelab::set(parameters);

  // create the graphical user interface
  std::set<std::string> args(argv, argv + argc);
  if(!args.count("-nopopup")) {
    gmsh::fltk::initialize();
    // wait for events until the GUI is closed
    while(gmsh::fltk::isAvailable() && checkForEvent(parameters))
      gmsh::fltk::wait();
  }

  gmsh::finalize();

  return 0;
}