Skip to content
Snippets Groups Projects
Commit 24945659 authored by Christophe Geuzaine's avatar Christophe Geuzaine
Browse files

reorganized solver directory + added c++ solver example
parent bdd922be
No related branches found
No related tags found
No related merge requests found
#ifndef _GMSH_CLIENT_H_
#define _GMSH_CLIENT_H_
/*
* 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>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _AIX
#include <strings.h>
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <sys/time.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netdb.h>
class GmshClient {
private:
int _sock;
void _SendData(int socket, void *buffer, int bytes)
{
int sofar, remaining, len;
char *buf;
buf = (char *)buffer;
sofar = 0;
remaining = bytes;
do {
len = write(socket, buf + sofar, remaining);
sofar += len;
remaining -= len;
} while(remaining > 0);
}
void _SendString(int socket, int type, char str[])
{
int len = strlen(str);
_SendData(socket, &type, sizeof(int));
_SendData(socket, &len, sizeof(int));
_SendData(socket, str, len);
}
long _GetTime()
{
struct timeval tp;
gettimeofday(&tp, (struct timezone *)0);
return (long)tp.tv_sec * 1000000 + (long)tp.tv_usec;
}
void _Idle(double delay)
{
long t1 = _GetTime();
while(1) {
if(_GetTime() - t1 > 1.e6 * delay)
break;
}
}
public:
GmshClient() : _sock(0) {}
~GmshClient(){}
int Connect(char *sockname)
{
struct sockaddr_un addr_un;
struct sockaddr_in addr_in;
int len, sock;
int tries;
struct hostent *server;
int portno, remotelen;
char remote[256], *port;
/* slight delay to be sure that the socket is bound by the
server before we attempt to connect to it... */
_Idle(0.1);
if(strstr(sockname, "/") || strstr(sockname, "\\") || !strstr(sockname, ":")){
/* UNIX socket (testing ":" is not enough with Windows paths) */
portno = -1;
}
else{
/* INET socket */
port = strstr(sockname, ":");
portno = atoi(port+1);
remotelen = strlen(sockname) - strlen(port);
if(remotelen > 0)
strncpy(remote, sockname, remotelen);
remote[remotelen] = '\0';
}
/* create socket */
if(portno < 0){
sock = socket(PF_UNIX, SOCK_STREAM, 0);
if(sock < 0)
return -1; /* Error: Couldn't create socket */
/* try to connect socket to given name */
strcpy(addr_un.sun_path, sockname);
addr_un.sun_family = AF_UNIX;
len = strlen(addr_un.sun_path) + sizeof(addr_un.sun_family);
for(tries = 0; tries < 5; tries++) {
if(connect(sock, (struct sockaddr *)&addr_un, len) >= 0){
_sock = sock;
return sock;
}
_Idle(0.1);
}
}
else{
/* try to connect socket to given name */
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0)
return -1; /* Error: Couldn't create socket */
if(!(server = gethostbyname(remote)))
return -3; /* Error: No such host */
memset((char *) &addr_in, 0, sizeof(addr_in));
addr_in.sin_family = AF_INET;
memcpy((char *)&addr_in.sin_addr.s_addr, (char *)server->h_addr, server->h_length);
addr_in.sin_port = htons(portno);
addr_in.sin_addr.s_addr = INADDR_ANY;
for(tries = 0; tries < 5; tries++) {
if(connect(sock, (struct sockaddr *)&addr_in, sizeof(addr_in)) >= 0){
_sock = sock;
return sock;
}
_Idle(0.1);
}
}
return -2; /* Error: Couldn't connect */
}
void Start(int pid)
{
char tmp[256];
sprintf(tmp, "%d", getpid());
_SendString(_sock, 1, tmp);
}
void Stop()
{
_SendString(_sock, 2, "Goodbye!");
}
void Info(char *str)
{
_SendString(_sock, 10, str);
}
void Warning(char *str)
{
_SendString(_sock, 11, str);
}
void Error(char *str)
{
_SendString(_sock, 12, str);
}
void Progress(char *str)
{
_SendString(_sock, 13, str);
}
void View(char *str)
{
_SendString(_sock, 20, str);
}
void Option(int num, char *str)
{
if(num < 1) num = 1;
if(num > 5) num = 5;
_SendString(_sock, 100 + num - 1, str);
}
void Disconnect()
{
close(_sock);
}
};
#endif
# $Id: Makefile,v 1.1 2005-01-13 20:36:54 geuzaine Exp $
#
# Copyright (C) 1997-2005 C. Geuzaine, J.-F. Remacle
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA.
#
# Please report all bugs and problems to <gmsh@geuz.org>.
include ../../../variables
andre: andre.cpp
${CXX} ${OPTIM} -o andre.exe andre.cpp
clean:
rm -f *.o *.exe *.pos
depend:
true
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#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 server\n");
exit(1);
}
else{
client.Start(getpid());
if(what_to_do == send_options) {
// send the available options for this computation
client.Option(1, "FormulationH");
client.Option(1, "\"Convergence Test\"");
client.Option(1, "\"Blabla blblablabli\"");
}
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...");
sleep(1);
client.Info("Done computing curve");
FILE *file = fopen("andre.pos", "w");
if(!file)
client.Error("Unable to open output file");
else {
fprintf(file, "General.GraphicsWidth = 500;\n");
fprintf(file, "General.GraphicsHeight = 450;\n");
fprintf(file, "General.SmallAxes = 0;\n");
fprintf(file, "View.Type = 2;\n");
fprintf(file, "View.AutoPosition = 0;\n");
fprintf(file, "View.PositionX = 100;\n");
fprintf(file, "View.PositionY = 50;\n");
fprintf(file, "View.Width = 350;\n");
fprintf(file, "View.Height = 350;\n");
fprintf(file, "Delete View[0];\n");
fprintf(file, "View \"test\"{\n");
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.View("andre.pos");
}
}
client.Info("Done!");
}
client.Stop();
client.Disconnect();
}
}
Solver.Name1 = "Solveur Andre";
Solver.Help1 = "Mon petit solveur";
Solver.Executable1 = "./andre.exe";
Solver.OptionCommand1 = "-options";
Solver.FirstOption1 = "Mes options";
Solver.FirstButton1 = "Run !";
Solver.FirstButtonCommand1 = "-run %s";
Solver.ClientServer1 = 1;
Solver.MergeViews1 = 1;
Solver.PopupMessages1 = 1;
/* $Id: GmshClient.c,v 1.10 2005-01-01 18:57:19 geuzaine Exp $ */
/* $Id: GmshClient.c,v 1.1 2005-01-13 20:36:54 geuzaine Exp $ */
/*
* Copyright (C) 1997-2004 C. Geuzaine, J.-F. Remacle
*
......@@ -130,9 +130,9 @@ int Gmsh_Connect(char *sockname)
return -1; /* Error: Couldn't create socket */
if(!(server = gethostbyname(remote)))
return -3; /* Error: No such host */
bzero((char *) &addr_in, sizeof(addr_in));
memset((char *) &addr_in, 0, sizeof(addr_in));
addr_in.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&addr_in.sin_addr.s_addr, server->h_length);
memcpy((char *)&addr_in.sin_addr.s_addr, (char *)server->h_addr, server->h_length);
addr_in.sin_port = htons(portno);
addr_in.sin_addr.s_addr = INADDR_ANY;
for(tries = 0; tries < 5; tries++) {
......
File moved
# $Id: Makefile,v 1.5 2005-01-01 19:35:41 geuzaine Exp $
# $Id: Makefile,v 1.1 2005-01-13 20:36:54 geuzaine Exp $
#
# Copyright (C) 1997-2005 C. Geuzaine, J.-F. Remacle
#
......@@ -19,17 +19,13 @@
#
# Please report all bugs and problems to <gmsh@geuz.org>.
include ../../variables
include ../../../variables
mysolver: mysolver.c GmshClient.c
${CXX} ${OPTIM} -o mysolver.exe mysolver.c GmshClient.c
tgz:
tar zcvf mysolver.tgz mysolver.c mysolver.opt GmshClient.c GmshClient.h
tar zcvf myperlsolver.tgz myperlsolver.pl myperlsolver.opt GMSH_CLIENT.pm
clean:
rm -f *.o *.exe
rm -f *.o *.exe *.pos
depend:
true
/* $Id: mysolver.c,v 1.5 2004-05-22 01:24:19 geuzaine Exp $ */
/* $Id: mysolver.c,v 1.1 2005-01-13 20:36:54 geuzaine Exp $ */
/*
* Copyright (C) 1997-2004 C. Geuzaine, J.-F. Remacle
*
......
File moved
# $Id: GMSH_CLIENT.pm,v 1.1 2003-05-09 16:30:01 geuzaine Exp $
# $Id: GMSH_CLIENT.pm,v 1.1 2005-01-13 20:36:54 geuzaine Exp $
#
# Copyright (c) 2002 Laurent CHAMPANEY <laurent.champaney@meca.uvsq.fr>.
# All rights reserved.
......
File moved
#!/usr/bin/perl
#
# $Id: myperlsolver.pl,v 1.1 2003-05-09 16:30:01 geuzaine Exp $
# $Id: myperlsolver.pl,v 1.1 2005-01-13 20:36:54 geuzaine Exp $
#
# Copyright (c) 2002 Laurent CHAMPANEY <laurent.champaney@meca.uvsq.fr>.
#
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment