[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5. Solver module

Five external solvers can be interfaced simultaneously with Gmsh.

If you just want to start a solver from the solver module, with no further interactions between the solver and Gmsh, just edit the options relative to one of the five available solvers (e.g., Solver.Name0, Solver.Executable0, ...; see 5.1 Solver options), and set the corresponding "client-server" option to zero (e.g., Solver.ClientServer0 = 0). This doesn't require any modification to be made to the solver.

If you want the solver to interact with Gmsh (for error messages, option definitions, post-processing, etc.), you need to link your solver with the `GmshClient.c' file and add the appropriate function calls inside your program. You can then proceed as in the previous case, but this time you should set the client-server option to 1 (e.g., Solver.ClientServer0 = 1), so that Gmsh and the solver can communicate through a Unix socket. See 5.2 Solver example, for an example of how to interface a C++ solver. Bindings for solvers written in other languages (C, Perl and Python) are available in the source distribution.

5.1 Solver options  
5.2 Solver example  


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.1 Solver options

Solver.SocketName
Name of socket (TCP/IP if it contains the `:' character, UNIX otherwise)
Default value: ".gmshsock"
Saved in: General.OptionsFileName

Solver.Name0
Name of solver 0
Default value: "GetDP"
Saved in: General.OptionsFileName

Solver.Help0
Help string for solver 0
Default value: "A General environment for the treatment of Discrete Problems. Copyright (C) 1997-2008 Patrick Dular and Christophe Geuzaine. Visit http://www.geuz.org/getdp/ for more info"
Saved in: General.OptionsFileName

Solver.Executable0
System command to launch solver 0 (should not contain the `&' character)
Default value: "getdp"
Saved in: General.OptionsFileName

Solver.Extension0
Default file name extension for solver 0
Default value: ".pro"
Saved in: General.OptionsFileName

Solver.MeshName0
Default mesh file name for solver 0
Default value: ""
Saved in: General.OptionsFileName

Solver.MeshCommand0
Command used to specify the mesh file for solver 0
Default value: "-msh %s"
Saved in: General.OptionsFileName

Solver.SocketCommand0
Command to specify the socket to solver 0
Default value: "-socket %s"
Saved in: General.OptionsFileName

Solver.NameCommand0
Command to specify the problem name to solver 0
Default value: "%s"
Saved in: General.OptionsFileName

Solver.OptionCommand0
Command to get options from solver 0
Default value: ""
Saved in: General.OptionsFileName

Solver.FirstOption0
Label of first option for solver 0
Default value: "Resolution"
Saved in: General.OptionsFileName

Solver.SecondOption0
Label of second option for solver 0
Default value: "PostOperation"
Saved in: General.OptionsFileName

Solver.ThirdOption0
Label of third option for solver 0
Default value: ""
Saved in: General.OptionsFileName

Solver.FourthOption0
Label of fourth option for solver 0
Default value: ""
Saved in: General.OptionsFileName

Solver.FifthOption0
Label of fifth option for solver 0
Default value: ""
Saved in: General.OptionsFileName

Solver.FirstButton0
Label of first button for solver 0
Default value: "Pre"
Saved in: General.OptionsFileName

Solver.FirstButtonCommand0
Command associated with the first button for solver 0
Default value: "-pre %s"
Saved in: General.OptionsFileName

Solver.SecondButton0
Label of second button for solver 0
Default value: "Cal"
Saved in: General.OptionsFileName

Solver.SecondButtonCommand0
Command associated with the second button for solver 0
Default value: "-cal"
Saved in: General.OptionsFileName

Solver.ThirdButton0
Label of third button for solver 0
Default value: "Pos"
Saved in: General.OptionsFileName

Solver.ThirdButtonCommand0
Command associated with the third button for solver 0
Default value: "-pos %s"
Saved in: General.OptionsFileName

Solver.FourthButton0
Label of fourth button for solver 0
Default value: ""
Saved in: General.OptionsFileName

Solver.FourthButtonCommand0
Command associated with the fourth button for solver 0
Default value: ""
Saved in: General.OptionsFileName

Solver.FifthButton0
Label of fifth button for solver 0
Default value: ""
Saved in: General.OptionsFileName

Solver.FifthButtonCommand0
Command associated with the fifth button for solver 0
Default value: ""
Saved in: General.OptionsFileName

Solver.Name1
Name of solver 1
Default value: ""
Saved in: General.OptionsFileName

Solver.Help1
Help string for solver 1
Default value: ""
Saved in: General.OptionsFileName

Solver.Executable1
System command to launch solver 1 (should not contain the `&' character)
Default value: ""
Saved in: General.OptionsFileName

Solver.Extension1
Default file name extension for solver 1
Default value: ""
Saved in: General.OptionsFileName

Solver.MeshName1
Default mesh file name for solver 1
Default value: ""
Saved in: General.OptionsFileName

Solver.MeshCommand1
Command used to specify the mesh file for solver 1
Default value: ""
Saved in: General.OptionsFileName

Solver.SocketCommand1
Command to specify the socket to solver 1
Default value: "-socket %s"
Saved in: General.OptionsFileName

Solver.NameCommand1
Command to specify the problem name to solver 1
Default value: "%s"
Saved in: General.OptionsFileName

Solver.OptionCommand1
Command to get options from solver 1
Default value: ""
Saved in: General.OptionsFileName

Solver.FirstOption1
Label of first option for solver 1
Default value: ""
Saved in: General.OptionsFileName

Solver.SecondOption1
Label of second option for solver 1
Default value: ""
Saved in: General.OptionsFileName

Solver.ThirdOption1
Label of third option for solver 1
Default value: ""
Saved in: General.OptionsFileName

Solver.FourthOption1
Label of fourth option for solver 1
Default value: ""
Saved in: General.OptionsFileName

Solver.FifthOption1
Label of fifth option for solver 1
Default value: ""
Saved in: General.OptionsFileName

Solver.FirstButton1
Label of first button for solver 1
Default value: ""
Saved in: General.OptionsFileName

Solver.FirstButtonCommand1
Command associated with the first button for solver 1
Default value: ""
Saved in: General.OptionsFileName

Solver.SecondButton1
Label of second button for solver 1
Default value: ""
Saved in: General.OptionsFileName

Solver.SecondButtonCommand1
Command associated with the second button for solver 1
Default value: ""
Saved in: General.OptionsFileName

Solver.ThirdButton1
Label of third button for solver 1
Default value: ""
Saved in: General.OptionsFileName

Solver.ThirdButtonCommand1
Command associated with the third button for solver 1
Default value: ""
Saved in: General.OptionsFileName

Solver.FourthButton1
Label of fourth button for solver 1
Default value: ""
Saved in: General.OptionsFileName

Solver.FourthButtonCommand1
Command associated with the fourth button for solver 1
Default value: ""
Saved in: General.OptionsFileName

Solver.FifthButton1
Label of fifth button for solver 1
Default value: ""
Saved in: General.OptionsFileName

Solver.FifthButtonCommand1
Command associated with the fifth button for solver 1
Default value: ""
Saved in: General.OptionsFileName

Solver.Name2
Name of solver 2
Default value: ""
Saved in: General.OptionsFileName

Solver.Help2
Help string for solver 2
Default value: ""
Saved in: General.OptionsFileName

Solver.Executable2
System command to launch solver 2 (should not contain the `&' character)
Default value: ""
Saved in: General.OptionsFileName

Solver.Extension2
Default file name extension for solver 2
Default value: ""
Saved in: General.OptionsFileName

Solver.MeshName2
Default mesh file name for solver 2
Default value: ""
Saved in: General.OptionsFileName

Solver.MeshCommand2
Command used to specify the mesh file for solver 2
Default value: ""
Saved in: General.OptionsFileName

Solver.SocketCommand2
Command to specify the socket to solver 2
Default value: "-socket %s"
Saved in: General.OptionsFileName

Solver.NameCommand2
Command to specify the problem name to solver 2
Default value: "%s"
Saved in: General.OptionsFileName

Solver.OptionCommand2
Command to get options from solver 2
Default value: ""
Saved in: General.OptionsFileName

Solver.FirstOption2
Label of first option for solver 2
Default value: ""
Saved in: General.OptionsFileName

Solver.SecondOption2
Label of second option for solver 2
Default value: ""
Saved in: General.OptionsFileName

Solver.ThirdOption2
Label of third option for solver 2
Default value: ""
Saved in: General.OptionsFileName

Solver.FourthOption2
Label of fourth option for solver 2
Default value: ""
Saved in: General.OptionsFileName

Solver.FifthOption2
Label of fifth option for solver 2
Default value: ""
Saved in: General.OptionsFileName

Solver.FirstButton2
Label of first button for solver 2
Default value: ""
Saved in: General.OptionsFileName

Solver.FirstButtonCommand2
Command associated with the first button for solver 2
Default value: ""
Saved in: General.OptionsFileName

Solver.SecondButton2
Label of second button for solver 2
Default value: ""
Saved in: General.OptionsFileName

Solver.SecondButtonCommand2
Command associated with the second button for solver 2
Default value: ""
Saved in: General.OptionsFileName

Solver.ThirdButton2
Label of third button for solver 2
Default value: ""
Saved in: General.OptionsFileName

Solver.ThirdButtonCommand2
Command associated with the third button for solver 2
Default value: ""
Saved in: General.OptionsFileName

Solver.FourthButton2
Label of fourth button for solver 2
Default value: ""
Saved in: General.OptionsFileName

Solver.FourthButtonCommand2
Command associated with the fourth button for solver 2
Default value: ""
Saved in: General.OptionsFileName

Solver.FifthButton2
Label of fifth button for solver 2
Default value: ""
Saved in: General.OptionsFileName

Solver.FifthButtonCommand2
Command associated with the fifth button for solver 2
Default value: ""
Saved in: General.OptionsFileName

Solver.Name3
Name of solver 3
Default value: ""
Saved in: General.OptionsFileName

Solver.Help3
Help string for solver 3
Default value: ""
Saved in: General.OptionsFileName

Solver.Executable3
System command to launch solver 3 (should not contain the `&' character)
Default value: ""
Saved in: General.OptionsFileName

Solver.Extension3
Default file name extension for solver 3
Default value: ""
Saved in: General.OptionsFileName

Solver.MeshName3
Default mesh file name for solver 3
Default value: ""
Saved in: General.OptionsFileName

Solver.MeshCommand3
Command used to specify the mesh file for solver 3
Default value: ""
Saved in: General.OptionsFileName

Solver.SocketCommand3
Command to specify the socket to solver 3
Default value: "-socket %s"
Saved in: General.OptionsFileName

Solver.NameCommand3
Command to specify the problem name to solver 3
Default value: "%s"
Saved in: General.OptionsFileName

Solver.OptionCommand3
Command to get options from solver 3
Default value: ""
Saved in: General.OptionsFileName

Solver.FirstOption3
Label of first option for solver 3
Default value: ""
Saved in: General.OptionsFileName

Solver.SecondOption3
Label of second option for solver 3
Default value: ""
Saved in: General.OptionsFileName

Solver.ThirdOption3
Label of third option for solver 3
Default value: ""
Saved in: General.OptionsFileName

Solver.FourthOption3
Label of fourth option for solver 3
Default value: ""
Saved in: General.OptionsFileName

Solver.FifthOption3
Label of fifth option for solver 3
Default value: ""
Saved in: General.OptionsFileName

Solver.FirstButton3
Label of first button for solver 3
Default value: ""
Saved in: General.OptionsFileName

Solver.FirstButtonCommand3
Command associated with the first button for solver 3
Default value: ""
Saved in: General.OptionsFileName

Solver.SecondButton3
Label of second button for solver 3
Default value: ""
Saved in: General.OptionsFileName

Solver.SecondButtonCommand3
Command associated with the second button for solver 3
Default value: ""
Saved in: General.OptionsFileName

Solver.ThirdButton3
Label of third button for solver 3
Default value: ""
Saved in: General.OptionsFileName

Solver.ThirdButtonCommand3
Command associated with the third button for solver 3
Default value: ""
Saved in: General.OptionsFileName

Solver.FourthButton3
Label of fourth button for solver 3
Default value: ""
Saved in: General.OptionsFileName

Solver.FourthButtonCommand3
Command associated with the fourth button for solver 3
Default value: ""
Saved in: General.OptionsFileName

Solver.FifthButton3
Label of fifth button for solver 3
Default value: ""
Saved in: General.OptionsFileName

Solver.FifthButtonCommand3
Command associated with the fifth button for solver 3
Default value: ""
Saved in: General.OptionsFileName

Solver.Name4
Name of solver 4
Default value: ""
Saved in: General.OptionsFileName

Solver.Help4
Help string for solver 4
Default value: ""
Saved in: General.OptionsFileName

Solver.Executable4
System command to launch solver 4 (should not contain the `&' character)
Default value: ""
Saved in: General.OptionsFileName

Solver.Extension4
Default file name extension for solver 4
Default value: ""
Saved in: General.OptionsFileName

Solver.MeshName4
Default mesh file name for solver 4
Default value: ""
Saved in: General.OptionsFileName

Solver.MeshCommand4
Command used to specify the mesh file for solver 4
Default value: ""
Saved in: General.OptionsFileName

Solver.SocketCommand4
Command to specify the socket to solver 4
Default value: "-socket %s"
Saved in: General.OptionsFileName

Solver.NameCommand4
Command to specify the problem name to solver 4
Default value: "%s"
Saved in: General.OptionsFileName

Solver.OptionCommand4
Command to get options from solver 4
Default value: ""
Saved in: General.OptionsFileName

Solver.FirstOption4
Label of first option for solver 4
Default value: ""
Saved in: General.OptionsFileName

Solver.SecondOption4
Label of second option for solver 4
Default value: ""
Saved in: General.OptionsFileName

Solver.ThirdOption4
Label of third option for solver 4
Default value: ""
Saved in: General.OptionsFileName

Solver.FourthOption4
Label of fourth option for solver 4
Default value: ""
Saved in: General.OptionsFileName

Solver.FifthOption4
Label of fifth option for solver 4
Default value: ""
Saved in: General.OptionsFileName

Solver.FirstButton4
Label of first button for solver 4
Default value: ""
Saved in: General.OptionsFileName

Solver.FirstButtonCommand4
Command associated with the first button for solver 4
Default value: ""
Saved in: General.OptionsFileName

Solver.SecondButton4
Label of second button for solver 4
Default value: ""
Saved in: General.OptionsFileName

Solver.SecondButtonCommand4
Command associated with the second button for solver 4
Default value: ""
Saved in: General.OptionsFileName

Solver.ThirdButton4
Label of third button for solver 4
Default value: ""
Saved in: General.OptionsFileName

Solver.ThirdButtonCommand4
Command associated with the third button for solver 4
Default value: ""
Saved in: General.OptionsFileName

Solver.FourthButton4
Label of fourth button for solver 4
Default value: ""
Saved in: General.OptionsFileName

Solver.FourthButtonCommand4
Command associated with the fourth button for solver 4
Default value: ""
Saved in: General.OptionsFileName

Solver.FifthButton4
Label of fifth button for solver 4
Default value: ""
Saved in: General.OptionsFileName

Solver.FifthButtonCommand4
Command associated with the fifth button for solver 4
Default value: ""
Saved in: General.OptionsFileName

Solver.AlwaysListen
Always listen to incoming connection requests?
Default value: 0
Saved in: General.OptionsFileName

Solver.ClientServer0
Connect solver 0 to the Gmsh server
Default value: 1
Saved in: General.OptionsFileName

Solver.ClientServer1
Connect solver 1 to the Gmsh server
Default value: 0
Saved in: General.OptionsFileName

Solver.ClientServer2
Connect solver 2 to the Gmsh server
Default value: 0
Saved in: General.OptionsFileName

Solver.ClientServer3
Connect solver 3 to the Gmsh server
Default value: 0
Saved in: General.OptionsFileName

Solver.ClientServer4
Connect solver 4 to the Gmsh server
Default value: 0
Saved in: General.OptionsFileName

Solver.MaximumDelay
Maximum delay (in seconds) allowed for solver response
Default value: 4
Saved in: General.OptionsFileName

Solver.MergeViews0
Automatically merge any post-processing view created by solver 0
Default value: 1
Saved in: General.OptionsFileName

Solver.MergeViews1
Automatically merge any post-processing view created by solver 1
Default value: 1
Saved in: General.OptionsFileName

Solver.MergeViews2
Automatically merge any post-processing view created by solver 2
Default value: 1
Saved in: General.OptionsFileName

Solver.MergeViews3
Automatically merge any post-processing view created by solver 3
Default value: 1
Saved in: General.OptionsFileName

Solver.MergeViews4
Automatically merge any post-processing view created by solver 4
Default value: 1
Saved in: General.OptionsFileName

Solver.Plugins
Enable default solver plugins?
Default value: 0
Saved in: General.OptionsFileName

Solver.PopupMessages0
Automatically display messages produced by solver 0
Default value: 1
Saved in: General.OptionsFileName

Solver.PopupMessages1
Automatically display messages produced by solver 1
Default value: 1
Saved in: General.OptionsFileName

Solver.PopupMessages2
Automatically display messages produced by solver 2
Default value: 1
Saved in: General.OptionsFileName

Solver.PopupMessages3
Automatically display messages produced by solver 3
Default value: 1
Saved in: General.OptionsFileName

Solver.PopupMessages4
Automatically display messages produced by solver 4
Default value: 1
Saved in: General.OptionsFileName


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.2 Solver example

Here is a small example of how to interface a C++ solver with Gmsh. The following listing reproduces the `utils/solvers/c++/solver.cpp' file from the Gmsh source distribution (C, Perl and Python examples are also available).

// $Id: solver.cpp,v 1.10 2007-09-04 13:47:08 remacle Exp $
//
// Copyright (C) 1997-2005 C. Geuzaine, J.-F. Remacle
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, and/or sell copies of the
// Software, and to permit persons to whom the Software is furnished
// to do so, provided that the above copyright notice(s) and this
// permission notice appear in all copies of the Software and that
// both the above copyright notice(s) and this permission notice
// appear in supporting documentation.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE
// COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR
// ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY
// DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
// ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
// OF THIS SOFTWARE.
// 
// Please report all bugs and problems to <gmsh@geuz.org>.

// This file contains a dummy client solver for Gmsh. It does not
// solve anything, but shows how to program your own solver to interact
// with the Gmsh solver module.
//
// To compile this solver, type something like:
//
// g++ solver.cpp -o solver.exe
//
// To run it, merge the contents of the file solver.opt into your
// default Gmsh option file, or launch Gmsh with the command:
//
// gmsh -option solver.opt 
//
// You will then see a new button labeled "My C++ solver" in Gmsh's
// solver menu.

#include <math.h>
#include "GmshClient.h"

typedef enum { send_options, run_code } action;

int main(int argc, char *argv[])
{
  action what_to_do = run_code;
  char *name = NULL, *option = NULL, *socket = NULL;

  // parse command line

  int i = 0;
  while(i < argc) {
    if(argv[i][0] == '-') {
      if(!strcmp(argv[i] + 1, "socket")) {
        i++; 
	if(argv[i]) socket = argv[i++];
      }
      else if(!strcmp(argv[i] + 1, "options")) {
        i++;
        what_to_do = send_options;
      }
      else if(!strcmp(argv[i] + 1, "run")) {
        i++;
        what_to_do = run_code;
        if(argv[i]) option = argv[i++];
      }
    }
    else
      name = argv[i++];
  }

  if(!socket) {
    printf("No socket specified: running non-interactively...\n");
    exit(1);
  }

  // connect to Gmsh

  GmshClient client;
  if(client.Connect(socket) < 0){
    printf("Unable to connect to Gmsh\n");
    exit(1);
  }

  client.Start();

  if(what_to_do == send_options) {
    // send the available options for this computation
    client.Option(1, "FormulationH");
    client.Option(1, "ConvTest");
    client.Option(1, "Blablabli");
  }
  else if(what_to_do == run_code){
    // do the computation and merge some views
    for(int i = 0; i < 10; i++){
      client.Info("Computing curve...");
      // fake computation for 500ms:
#if !defined(WIN32) || defined(__CYGWIN__)
      usleep(500 * 1000);
#else
      Sleep(500);
#endif
      client.Info("Done computing curve");
      FILE *file = fopen("solver.pos", "w");
      if(!file)
	client.Error("Unable to open output file");
      else {
	fprintf(file, "View.Type = 2;\n");
	fprintf(file, "View.Axes = 3;\n");
	fprintf(file, "Delete View[0];\n");
	fprintf(file, "View \"%s\"{\n", option);
	for(int j = 0; j < 100; j++)
	  fprintf(file, "SP(%d,0,0){%g};\n", j,sin(j*i*M_PI/10.));
	fprintf(file, "};\n");
	fclose(file);
	client.MergeFile("solver.pos");
      }
    }
    client.Info("Done!");
  }

  client.Stop();
  client.Disconnect();
}

To define the above solver as the second external solver in Gmsh, you then need to define the following options (either merge them in your Gmsh option file, or use the -option command-line option--see 8.3 Command-line options):

Solver.Name1 = "My C++ Solver";
Solver.Executable1 = "solver.exe";
Solver.OptionCommand1 = "-options";
Solver.FirstOption1 = "My options";
Solver.FirstButton1 = "Run !";
Solver.FirstButtonCommand1 = "-run %s";
Solver.ClientServer1 = 1;
Solver.MergeViews1 = 1;
Solver.PopupMessages1 = 1;


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

Back to geuz.org/gmsh