diff --git a/Common/GmshMessage.cpp b/Common/GmshMessage.cpp index 2824dc42cb57626099ce5c5e8fa2bcd891642820..fcc680c0da49548511364e45ffc4625d1590ae38 100644 --- a/Common/GmshMessage.cpp +++ b/Common/GmshMessage.cpp @@ -664,9 +664,13 @@ void Msg::SetOnelabString(std::string name, std::string val, bool visible) class localGmsh : public onelab::localClient { public: localGmsh() : onelab::localClient("Gmsh") {} - void sendMergeFileRequest(const std::string &msg) + void sendMergeFileRequest(const std::string &name) { - MergePostProcessingFile(msg, CTX::instance()->solver.autoShowLastStep, + if(name.find(".geo")!= std::string::npos){ + GModel::current()->setFileName(name); + } + else + MergePostProcessingFile(name, CTX::instance()->solver.autoShowLastStep, CTX::instance()->solver.autoHideNewViews, true); } void sendInfo(const std::string &msg){ Msg::Info("%s", msg.c_str()); } diff --git a/Common/GmshSocket.h b/Common/GmshSocket.h index 7117caa2d5c5b7d6528780f0efdd47d5992d071f..aa8d39b264d418f65ab374e08260a6683ccc514f 100644 --- a/Common/GmshSocket.h +++ b/Common/GmshSocket.h @@ -27,7 +27,10 @@ #ifndef _GMSH_SOCKET_H_ #define _GMSH_SOCKET_H_ +#if !defined(ONELAB_LOADER) #include "GmshConfig.h" +#endif + #include <string> #include <stdio.h> #include <stdlib.h> diff --git a/Fltk/menuWindow.cpp b/Fltk/menuWindow.cpp index e281f24736742293c8fb2b3caaa0746a78f0dd9a..61317e24a8053e2c95cb3ed24db1adde014e1b75 100644 --- a/Fltk/menuWindow.cpp +++ b/Fltk/menuWindow.cpp @@ -711,6 +711,7 @@ static void geometry_edit_cb(Fl_Widget *w, void *data) void geometry_reload_cb(Fl_Widget *w, void *data) { std::string fileName = GModel::current()->getFileName(); + std::cout << "FHF geometry_reload " << fileName << std::endl; OpenProject(fileName); drawContext::global()->draw(); } diff --git a/Fltk/onelabWindow.cpp b/Fltk/onelabWindow.cpp index 7964dc6852ce5547699b5860da75fadb9c382615..c43f637c2b795f2aef1bf712f9090978f3d6fa9f 100644 --- a/Fltk/onelabWindow.cpp +++ b/Fltk/onelabWindow.cpp @@ -57,7 +57,7 @@ class onelabGmshServer : public GmshServer{ double start = GetTimeInSeconds(); while(1){ if(timeout > 0 && GetTimeInSeconds() - start > timeout) - return 2; // timout + return 2; // timeout if(_client->getPid() < 0 || (_client->getExecutable().empty() && !CTX::instance()->solver.listen)) return 1; // process has been killed or we stopped listening @@ -545,10 +545,13 @@ void onelab_cb(Fl_Widget *w, void *data) if(isMetamodel){ #if defined(HAVE_ONELAB_METAMODEL) metamodel(action); + geometry_reload_cb(0, 0); + //std::cout << "FHFap:" << GModel::current()->getFileName() << std::endl; + //std::cout << "FHFap:" << GModel::current()->getName() << std::endl; #endif } else{ - // iterate over all other clients (there should narmally only be one) + // iterate over all other clients (there should normally only be one) for(onelab::server::citer it = onelab::server::instance()->firstClient(); it != onelab::server::instance()->lastClient(); it++){ onelab::client *c = it->second; @@ -1355,8 +1358,10 @@ int metamodel_cb(const std::string &name, const std::string &action) FlGui::instance()->onelab->rebuildSolverList(); - if(FlGui::instance()->available()) + if(FlGui::instance()->available()){ + onelab_cb(0, (void*)"initialize"); onelab_cb(0, (void*)"check"); + } else metamodel(action); } diff --git a/contrib/onelab/OnelabClients.cpp b/contrib/onelab/OnelabClients.cpp index 3bc27721176a8c200e76bd36ecc5175041617f4c..2dc91b939461ee99450e01dabcaf15b3945e7be0 100644 --- a/contrib/onelab/OnelabClients.cpp +++ b/contrib/onelab/OnelabClients.cpp @@ -3,7 +3,6 @@ #include "StringUtils.h" #include <algorithm> - class onelabMetaModelServer : public GmshServer{ private: localNetworkSolverClient *_client; @@ -11,6 +10,7 @@ class onelabMetaModelServer : public GmshServer{ onelabMetaModelServer(localNetworkSolverClient *client) : GmshServer(), _client(client) {} ~onelabMetaModelServer(){} + int NonBlockingSystemCall(const char *command) { #if defined(WIN32) @@ -44,12 +44,13 @@ class onelabMetaModelServer : public GmshServer{ return system(cmd.c_str()); #endif }// non blocking + int NonBlockingWait(int socket, double waitint, double timeout) { double start = GetTimeInSeconds(); while(1){ if(timeout > 0 && GetTimeInSeconds() - start > timeout) - return 2; // timout + return 2; // timeout // if(_client->getPid() < 0 || (_client->getCommandLine().empty() && // !CTX::instance()->solver.listen)) if(_client->getPid() < 0 || (_client->getCommandLine().empty())) @@ -67,14 +68,12 @@ class onelabMetaModelServer : public GmshServer{ onelab::server::instance()->set(ps[0]); //onelab_cb(0, (void*)"refresh"); } - // wait at most waitint seconds and respond to FLTK events - //FlGui::instance()->wait(waitint); } else if(ret > 0){ return 0; // data is there! } else{ - // an error happened + // ret<0 an error happened _client->setPid(-1); _client->setGmshServer(0); return 1; @@ -175,7 +174,7 @@ bool localNetworkSolverClient::run() while(1) { if(_pid < 0) break; - int stop = server->NonBlockingWait(sock, 0.1, 0.); + int stop = server->NonBlockingWait(sock, 0.001, 0.); if(stop || _pid < 0) { OLMsg::Info("Stop=%d _pid=%d",stop, _pid); break; @@ -193,8 +192,8 @@ bool localNetworkSolverClient::run() OLMsg::Error("Did not receive message body: stopping server"); break; } - // else - // std::cout << "FHF: Received message=" << message << std::endl; + //else + // std::cout << "FHF: Received message=" << message << std::endl; switch (type) { case GmshSocket::GMSH_START: @@ -325,7 +324,6 @@ bool localNetworkSolverClient::run() case GmshSocket::GMSH_MERGE_FILE: OLMsg::Info("Merge Post-Processing File %s",message.c_str()); SystemCall("gmsh "+ message); - //implémentation pour le cas du loader en mode console. break; default: OLMsg::Warning("Received unknown message type (%d)", type); @@ -352,7 +350,6 @@ bool localNetworkSolverClient::kill() return false; } - // client LOCALSOLVERCLIENT std::string localSolverClient::toChar(){ @@ -368,6 +365,7 @@ void localSolverClient::setAction(const std::string action){ std::string name=getName() + "/Action"; onelab::string s(name, action); s.setVisible(false); + s.setNeverChanged(true); set(s); } @@ -404,7 +402,7 @@ bool localSolverClient::checkCommandLine(){ if(!getCommandLine().empty()){ setAction("initialize"); - run(); + run(); // does nothing for Interfaced clients, initializes native clients } else{ if(OLMsg::hasGmsh) { @@ -447,6 +445,27 @@ bool localSolverClient::buildRmCommand(std::string &cmd){ return false; } +void localSolverClient::addNumberChoice(std::string name, double val) +{ + std::vector<double> choices; + std::vector<onelab::number> ps; + get(ps, name); + if(ps.size()){ + choices = ps[0].getChoices(); + } + else{ + ps.resize(1); + ps[0].setName(name); + } + ps[0].setAttribute("Highlight","Coral"); + ps[0].setReadOnly(false); + ps[0].setVisible(true); + ps[0].setValue(val); + choices.push_back(val); + ps[0].setChoices(choices); + set(ps[0]); +} + void localSolverClient::PostArray(std::vector<std::string> choices) { int nb=0; @@ -457,7 +476,7 @@ void localSolverClient::PostArray(std::vector<std::string> choices) std::string fileName = getWorkingDir()+choices[4*nb]; //checkIfPresent or make available locally double val=find_in_array(lin,col,read_array(fileName,' ')); - OLMsg::AddOnelabNumberChoice(choices[4*nb+3],val); + addNumberChoice(choices[4*nb+3],val); OLMsg::Info("Upload parameter <%s>=%e from file <%s>", choices[4*nb+3].c_str(),val,fileName.c_str()); nb++; @@ -466,18 +485,14 @@ void localSolverClient::PostArray(std::vector<std::string> choices) void localSolverClient::GmshMerge(std::vector<std::string> choices) { - //if(!OLMsg::hasGmsh || choices.empty()) return; for(unsigned int i = 0; i < choices.size(); i++){ - if(!choices[i].empty()){ - std::string fileName=getWorkingDir()+choices[i]; - //checkIfPresent or make available locally - OLMsg::SendMergeFileRequest(fileName); - OLMsg::Info("Send merge request <%s>",fileName.c_str()); - } + std::string fileName=getWorkingDir()+choices[i]; + //checkIfPresent or make available locally + OLMsg::Info("Send merge request <%s>",fileName.c_str()); + OLMsg::MergeFile(fileName); } } - // REMOTE CLIENT bool remoteClient::checkCommandLine(const std::string &commandLine){ @@ -638,16 +653,20 @@ void MetaModel::registerClient(const std::string &name, const std::string &type, if(host.empty() || rdir.empty()){ //local client if(!type.compare(0,6,"interf")) c= new InterfacedClient(name,cmdl,getWorkingDir()); - else if(!type.compare(0,6,"encaps")) + else if(!type.compare(0,6,"native")) c= new NativeClient(name,cmdl,getWorkingDir()); + else if(!type.compare(0,6,"encaps")) + c= new EncapsulatedClient(name,cmdl,getWorkingDir()); else OLMsg::Fatal("Unknown client type", type.c_str()); } else{ // remote client if(!type.compare(0,6,"interf")) c= new RemoteInterfacedClient(name,cmdl,getWorkingDir(),host,rdir); - else if(!type.compare(0,6,"encaps")) + else if(!type.compare(0,6,"native")) c= new RemoteNativeClient(name,cmdl,getWorkingDir(),host,rdir); + // else if(!type.compare(0,6,"encaps")) + // c= new RemoteEncapsulatedClient(name,cmdl,getWorkingDir(),host,rdir); else OLMsg::Fatal("Unknown remote client type", type.c_str()); } @@ -664,7 +683,7 @@ void MetaModel::PostArray(std::vector<std::string> choices) std::string fileName = OLMsg::GetOnelabString("Arguments/WorkingDir")+choices[4*nb]; double val=find_in_array(lin,col,read_array(fileName,' ')); - OLMsg::AddOnelabNumberChoice(choices[4*nb+3],val); + addNumberChoice(choices[4*nb+3],val); OLMsg::Info("PostArray <%s>=%e",choices[4*nb+3].c_str(),val); nb++; } @@ -713,13 +732,11 @@ void InterfacedClient::compute(){ std::string cmd; std::vector<std::string> choices; - setAction("compute"); OLMsg::Info("Computes <%s> changed=%d", getName().c_str(), onelab::server::instance()->getChanged(getName())); if(getActive() && onelab::server::instance()->getChanged(getName())){ analyze(); - //convert(); if(getList("InputFiles",choices)){ for(unsigned int i = 0; i < choices.size(); i++){ //remove .ol ext @@ -728,16 +745,22 @@ void InterfacedClient::compute(){ } } - if(buildRmCommand(cmd)) - SystemCall(cmd,true); //blocking + setAction("compute"); + if(buildRmCommand(cmd)){ + OLMsg::Info("Calling <%s>",cmd.c_str()); + SystemCall(cmd,true); + } cmd.assign(""); if(!getWorkingDir().empty()) cmd.append("cd " + getWorkingDir() + cmdSep); - cmd.append(FixWindowsPath(getCommandLine() + " ")); - cmd.append(getString("Arguments")); + cmd.append(FixWindowsPath(getCommandLine())); + cmd.append(" " + getString("Arguments")); - SystemCall(cmd.c_str(),true); //blocking + if(cmd.size()){ + OLMsg::Info("Calling <%s>",cmd.c_str()); + SystemCall(cmd.c_str(),true); + } if(getList("OutputFiles",choices)){ for(unsigned int i = 0; i < choices.size(); i++){ @@ -765,8 +788,10 @@ void NativeClient::compute() { OLMsg::Info("Computes <%s> changed=%d", getName().c_str(), onelab::server::instance()->getChanged(getName())); if(getActive() && onelab::server::instance()->getChanged(getName())){ - if(buildRmCommand(cmd)) + if(buildRmCommand(cmd)){ + OLMsg::Info("Calling <%s>",cmd.c_str()); SystemCall(cmd,true); + } if(getList("InputFiles",choices)){ for(unsigned int i = 0; i < choices.size(); i++){ @@ -783,6 +808,98 @@ void NativeClient::compute() { } } +// ENCAPSULATED Client + +// bool EncapsulatedClient::checkCommandLine(){ +// return localSolverClient::checkCommandLine(getCommandLine()); +// } + +void EncapsulatedClient::analyze() { + int pos; + std::vector<std::string> choices; + + setAction("check"); + OLMsg::Info("Analyses <%s> changed=%d", getName().c_str(), + onelab::server::instance()->getChanged(getName())); + getList("InputFiles", choices); + for(unsigned int i = 0; i < choices.size(); i++){ + if((pos=choices[i].find(onelabExtension)) != std::string::npos){ + // if .ol file + checkIfPresentLocal(choices[i]); + parse_onefile(choices[i]); + } + } + convert(); +} + +void EncapsulatedClient::convert() { + int pos; + std::vector<std::string> choices; + getList("InputFiles", choices); + for(unsigned int i = 0; i < choices.size(); i++){ + if((pos=choices[i].find(onelabExtension)) != std::string::npos){ + checkIfPresentLocal(choices[i]); + // remove .ol extension + std::string ofileName = getWorkingDir() + choices[i].substr(0,pos); + std::ofstream outfile(ofileName.c_str()); + if (outfile.is_open()) + convert_onefile(choices[i],outfile); + else + OLMsg::Fatal("The file <%s> cannot be opened",ofileName.c_str()); + outfile.close(); + } + } +} + +std::string EncapsulatedClient::buildCommandLine(){ + std::string command; + command.assign("lol"); + return command; +} + +void EncapsulatedClient::compute(){ + std::string cmd; + std::vector<std::string> choices; + + setAction("compute"); + std::string name=getName(); + OLMsg::Info("Computes <%s> changed=%d", name.c_str(), + onelab::server::instance()->getChanged(name)); + + if(getActive() && onelab::server::instance()->getChanged(name)){ + analyze(); + if(getList("InputFiles",choices)){ + for(unsigned int i = 0; i < choices.size(); i++){ + //remove .ol ext + checkIfPresentLocal( + choices[i].substr(0,choices[i].find(onelabExtension))); + } + } + + setAction("compute"); + if(buildRmCommand(cmd)){ + OLMsg::Info("Calling <%s>",cmd.c_str()); + SystemCall(cmd,true); + } + + cmd.assign(""); + if(!getWorkingDir().empty()) + cmd.append("cd " + getWorkingDir() + cmdSep); + cmd.append(FixWindowsPath(getCommandLine())); + cmd.append(" " + getString("Arguments")); + + OLMsg::SetOnelabString(name+"/FullCmdLine",cmd,false); + run(); + + if(getList("OutputFiles",choices)){ + for(unsigned int i = 0; i < choices.size(); i++){ + checkIfPresentLocal(choices[i]); + } + } + } + OLMsg::Info("Client %s completed",name.c_str()); +} + // REMOTE INTERFACED Client bool RemoteInterfacedClient::checkCommandLine(){ @@ -1006,25 +1123,6 @@ bool isPath(const std::string &in) return true; } -void GmshDisplay(onelab::remoteNetworkClient *loader, std::string fileName, std::vector<std::string> choices){ - if(choices.empty()) return; -#if defined(WIN32) - std::string cmd = "gmsh.exe "; -#else - std::string cmd = "gmsh "; -#endif - cmd.append( fileName + ".geo "); - for(unsigned int i = 0; i < choices.size(); i++){ - std::string fileName=OLMsg::GetOnelabString("Arguments/WorkingDir")+choices[i]; - cmd.append(fileName+" "); - if(OLMsg::hasGmsh){ - loader->sendMergeFileRequest(fileName); - OLMsg::Info("Send merge request <%s>",fileName.c_str()); - } - } - if(!OLMsg::hasGmsh) system(cmd.c_str()); -} - std::vector <double> extract_column(const int col, array data){ std::vector<double> column; for ( int i=0; i<data.size(); i++) diff --git a/contrib/onelab/OnelabClients.h b/contrib/onelab/OnelabClients.h index ffcaf6619b57b4f15b16f66ac68b50e7d83faa56..c786eba8f7b851d2600169bc1d8cfab3896d27f3 100644 --- a/contrib/onelab/OnelabClients.h +++ b/contrib/onelab/OnelabClients.h @@ -35,7 +35,6 @@ std::string itoa(const int i); std::string ftoa(const double x); bool checkIfPresent(std::string fileName); int mySystem(std::string commandLine); -void GmshDisplay(onelab::remoteNetworkClient *loader, std::string fileName, std::vector<std::string> choices); std::string getCurrentWorkdir(); std::string getUserHomedir(); std::string sanitize(const std::string &in); @@ -159,6 +158,7 @@ class localSolverClient : public onelab::localClient{ virtual bool checkCommandLine(); virtual void analyze() =0; virtual void compute() =0; + void addNumberChoice(std::string name, double val); void PostArray(std::vector<std::string> choices); void GmshMerge(std::vector<std::string> choices); }; @@ -275,17 +275,28 @@ class InterfacedClient : public localSolverClient { }; class NativeClient : public localNetworkSolverClient { - // utilise localNetworkClient::run public: NativeClient(const std::string &name, const std::string &cmdl, const std::string &wdir) : localNetworkSolverClient(name,cmdl,wdir) {} ~NativeClient(){} - //uses localNetworkSolver::buildCommandLine(); virtual void analyze(); - virtual void compute() ; + virtual void compute(); }; + +class EncapsulatedClient : public localNetworkSolverClient{ + public: + EncapsulatedClient(const std::string &name, const std::string &cmdl, const std::string &wdir) : localNetworkSolverClient(name,cmdl,wdir) {} + ~EncapsulatedClient(){} + + std::string buildCommandLine(); + void analyze(); + void convert(); + void compute(); +}; + + class RemoteInterfacedClient : public InterfacedClient, public remoteClient { public: RemoteInterfacedClient(const std::string &name, const std::string &cmdl, const std::string &wdir, const std::string &host, const std::string &rdir) diff --git a/contrib/onelab/OnelabMessage.cpp b/contrib/onelab/OnelabMessage.cpp index 84160636229066b77df1d5532964a675a596be65..712d7ed25c70abaaa9f69d2bb0d4cacfb3cbcf83 100644 --- a/contrib/onelab/OnelabMessage.cpp +++ b/contrib/onelab/OnelabMessage.cpp @@ -14,7 +14,7 @@ #define ALWAYS_TRUE 1 -onelab::remoteNetworkClient *OLMsg::loader = 0; +//onelab::remoteNetworkClient *OLMsg::loader = 0; int OLMsg::_commRank = 0; int OLMsg::_commSize = 1; @@ -91,9 +91,9 @@ void OLMsg::Fatal(const char *fmt, ...) } OLMsg::SetOnelabString("MetaModel/STATUS","STOP"); - FinalizeClient(); + //FinalizeClient(); FinalizeOnelab(); - delete loader; + //delete loader; Exit(1); } @@ -215,191 +215,27 @@ void OLMsg::StatusBar(int num, bool log, const char *fmt, ...) } } -/* -void OLMsg::Debug(const char *fmt, ...) -{ - if(_verbosity < 99) return; - - char str[1024]; - va_list args; - va_start(args, fmt); - vsnprintf(str, sizeof(str), fmt, args); - va_end(args); +// void OLMsg::InitializeOnelab(const std::string &name, const std::string &sockname) +// { +// if(_onelabClient) delete _onelabClient; +// if (sockname.empty()) +// _onelabClient = new onelab::localClient(name); +// else{ +// onelab::remoteNetworkClient *c = +// new onelab::remoteNetworkClient(name, sockname); +// _onelabClient = c; +// _client = c->getGmshClient(); +// } +// } - if(_callback) (*_callback)("Debug", str); - if(_client) _client->Info(str); - - if(ALWAYS_TRUE){ - if(_commSize > 1) - fprintf(stdout, "Debug : [On processor %d] %s\n", _commRank, str); - else - fprintf(stdout, "Debug : %s\n", str); - fflush(stdout); - } -} - -void OLMsg::ProgressMeter(int n, int N, const char *fmt, ...) -{ - if(_commRank || _verbosity < 3) return; - - double percent = 100. * (double)n/(double)N; - - if(percent >= _progressMeterCurrent){ - char str[1024]; - va_list args; - va_start(args, fmt); - vsnprintf(str, sizeof(str), fmt, args); - va_end(args); - - if(strlen(fmt)) strcat(str, " "); - - char str2[1024]; - sprintf(str2, "(%d %%)", _progressMeterCurrent); - strcat(str, str2); - - if(_client) _client->Progress(str); - - if(ALWAYS_TRUE){ - fprintf(stdout, "%s \r", str); - fflush(stdout); - } - - while(_progressMeterCurrent < percent) - _progressMeterCurrent += _progressMeterStep; - } - - if(n > N - 1){ - if(_client) _client->Progress("Done!"); - - if(ALWAYS_TRUE){ - fprintf(stdout, "Done! \r"); - fflush(stdout); - } - } -} - -void OLMsg::PrintTimers() -{ - // do a single stdio call! - std::string str; - for(std::map<std::string, double>::iterator it = _timers.begin(); - it != _timers.end(); it++){ - if(it != _timers.begin()) str += ", "; - char tmp[256]; - sprintf(tmp, "%s = %gs ", it->first.c_str(), it->second); - str += std::string(tmp); - } - if(!str.size()) return; - - if(ALWAYS_TRUE){ - if(_commSize > 1) - fprintf(stdout, "Timers : [On processor %d] %s\n", _commRank, str.c_str()); - else - fprintf(stdout, "Timers : %s\n", str.c_str()); - fflush(stdout); - } -} - -void OLMsg::PrintErrorCounter(const char *title) -{ - if(_commRank || _verbosity < 1) return; - if(!_warningCount && !_errorCount) return; - - std::string prefix = _errorCount ? "Error : " : "Warning : "; - std::string help("Check the full log for details"); - std::string line(std::max(strlen(title), help.size()), '-'); - char warn[128], err[128]; - sprintf(warn, "%5d warning%s", _warningCount, _warningCount == 1 ? "" : "s"); - sprintf(err, "%5d error%s", _errorCount, _errorCount == 1 ? "" : "s"); - - if(ALWAYS_TRUE){ - fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n", (prefix + line).c_str(), - (prefix + title).c_str(), (prefix + warn).c_str(), - (prefix + err).c_str(), (prefix + help).c_str(), - (prefix + line).c_str()); - fflush(stderr); - } -} - -double OLMsg::GetValue(const char *text, double defaultval) -{ - printf("%s (default=%.16g): ", text, defaultval); - char str[256]; - char *ret = fgets(str, sizeof(str), stdin); - if(!ret || !strlen(str) || !strcmp(str, "\n")) - return defaultval; - else - return atof(str); -} - -std::string OLMsg::GetString(const char *text, std::string defaultval) -{ - printf("%s (default=%s): ", text, defaultval.c_str()); - char str[256]; - char *ret = fgets(str, sizeof(str), stdin); - if(!ret || !strlen(str) || !strcmp(str, "\n")) - return defaultval; - else - return std::string(str); -} - -int OLMsg::GetAnswer(const char *question, int defaultval, const char *zero, - const char *one, const char *two) -{ - if(two) - printf("%s\n\n0=[%s] 1=[%s] 2=[%s] (default=%d): ", question, - zero, one, two, defaultval); - else - printf("%s\n\n0=[%s] 1=[%s] (default=%d): ", question, - zero, one, defaultval); - char str[256]; - char *ret = fgets(str, sizeof(str), stdin); - if(!ret || !strlen(str) || !strcmp(str, "\n")) - return defaultval; - else - return atoi(ret); -} - -void OLMsg::InitClient(std::string sockname) -{ - if(_client) delete _client; - _client = new GmshClient(); - if(_client->Connect(sockname.c_str()) < 0){ - OLMsg::Error("Unable to connect to server on %s", sockname.c_str()); - delete _client; - _client = 0; - } - else - _client->Start(); -} - -void OLMsg::Barrier() -{ -} -*/ -void OLMsg::FinalizeClient() -{ - if(_client){ - _client->Stop(); - _client->Disconnect(); - delete _client; - } - _client = 0; -} - -void OLMsg::InitializeOnelab(const std::string &name, const std::string &sockname) +void OLMsg::InitializeOnelab(const std::string &name) { if(_onelabClient) delete _onelabClient; - if (sockname.empty()) - _onelabClient = new onelab::localClient(name); - else{ - onelab::remoteNetworkClient *c = - new onelab::remoteNetworkClient(name, sockname); - _onelabClient = c; - _client = c->getGmshClient(); - } + _onelabClient = new onelab::localClient(name); + OLMsg::hasGmsh = OLMsg::GetOnelabNumber("IsMetamodel"); } + double OLMsg::GetOnelabNumber(std::string name) { if(_onelabClient){ @@ -509,29 +345,6 @@ std::string OLMsg::GetOnelabAttributeNumber(std::string name,std::string attrib) return str; } -void OLMsg::AddOnelabNumberChoice(std::string name, double val) -{ - if(_onelabClient){ - std::vector<double> choices; - std::vector<onelab::number> ps; - _onelabClient->get(ps, name); - if(ps.size()){ - choices = ps[0].getChoices(); - } - else{ - ps.resize(1); - ps[0].setName(name); - } - ps[0].setAttribute("Highlight","Coral"); // only used by PostArray - ps[0].setReadOnly(false); - ps[0].setVisible(true); - ps[0].setValue(val); - choices.push_back(val); - ps[0].setChoices(choices); - _onelabClient->set(ps[0]); - } -} - int fullNameLessThan::compareFullNames(const std::string a, const std::string b) const{ std::string::const_iterator ita, itb; ita=a.begin(); itb=b.begin(); @@ -577,88 +390,213 @@ std::string OLMsg::obtainFullName(const std::string &name){ } } -void OLMsg::SendMergeFileRequest(const std::string &name){ - _onelabClient->sendMergeFileRequest(name); -} +void OLMsg::MergeFile(const std::string &name){ + if(_onelabClient) + _onelabClient->sendMergeFileRequest(name); + else + OLMsg::Info("Not connected to Gmsh"); +} + + +// void OLMsg::AddOnelabNumberChoice(std::string name, double val) +// { +// if(_onelabClient){ +// std::vector<double> choices; +// std::vector<onelab::number> ps; +// _onelabClient->get(ps, name); +// if(ps.size()){ +// choices = ps[0].getChoices(); +// } +// else{ +// ps.resize(1); +// ps[0].setName(name); +// } +// ps[0].setVisible(false); +// choices.push_back(val); +// ps[0].setChoices(choices); +// _onelabClient->set(ps[0]); +// } +// } -int OLMsg::Synchronize_Down(){ - OLMsg::_fullNameDict.clear(); - std::vector<onelab::number> numbers; - loader->get(numbers,""); - if(numbers.size()){ - for(std::vector<onelab::number>::const_iterator it = numbers.begin(); - it != numbers.end(); it++){ - if(_onelabClient) _onelabClient->set(*it); - OLMsg::recordFullName((*it).getName()); - //std::cout << "FHF d " << (*it).getName() << "=" << (*it).getChanged() << std::endl; - } +void OLMsg::FinalizeOnelab(){ + if(_onelabClient){ + delete _onelabClient; + _onelabClient = 0; + _client = 0; } - std::vector<onelab::string> strings; - loader->get(strings,""); - if(strings.size()){ - for(std::vector<onelab::string>::const_iterator it = strings.begin(); - it != strings.end(); it++){ - if(_onelabClient) _onelabClient->set(*it); - OLMsg::recordFullName((*it).getName()); - //std::cout << "FHF d " << (*it).getName() << "=" << (*it).getChanged() << std::endl; - } +} + + + +/* +void OLMsg::Debug(const char *fmt, ...) +{ + if(_verbosity < 99) return; + + char str[1024]; + va_list args; + va_start(args, fmt); + vsnprintf(str, sizeof(str), fmt, args); + va_end(args); + + if(_callback) (*_callback)("Debug", str); + if(_client) _client->Info(str); + + if(ALWAYS_TRUE){ + if(_commSize > 1) + fprintf(stdout, "Debug : [On processor %d] %s\n", _commRank, str); + else + fprintf(stdout, "Debug : %s\n", str); + fflush(stdout); } - std::vector<onelab::region> regions; - loader->get(regions,""); - if(regions.size()){ - for(std::vector<onelab::region>::const_iterator it = regions.begin(); - it != regions.end(); it++){ - if(_onelabClient) _onelabClient->set(*it); - OLMsg::recordFullName((*it).getName()); +} + +void OLMsg::ProgressMeter(int n, int N, const char *fmt, ...) +{ + if(_commRank || _verbosity < 3) return; + + double percent = 100. * (double)n/(double)N; + + if(percent >= _progressMeterCurrent){ + char str[1024]; + va_list args; + va_start(args, fmt); + vsnprintf(str, sizeof(str), fmt, args); + va_end(args); + + if(strlen(fmt)) strcat(str, " "); + + char str2[1024]; + sprintf(str2, "(%d %%)", _progressMeterCurrent); + strcat(str, str2); + + if(_client) _client->Progress(str); + + if(ALWAYS_TRUE){ + fprintf(stdout, "%s \r", str); + fflush(stdout); } + + while(_progressMeterCurrent < percent) + _progressMeterCurrent += _progressMeterStep; } - return(numbers.size()+strings.size()+regions.size()); -} - -int OLMsg::Synchronize_Up(){ - std::vector<onelab::number> numbers; - _onelabClient->get(numbers,""); - if(numbers.size()){ - for(std::vector<onelab::number>::const_iterator it = numbers.begin(); - it != numbers.end(); it++){ - loader->set(*it); - //std::cout << "FHF u " << (*it).getName() << "=" - //<< (*it).getChanged() << std::endl; + + if(n > N - 1){ + if(_client) _client->Progress("Done!"); + + if(ALWAYS_TRUE){ + fprintf(stdout, "Done! \r"); + fflush(stdout); } } - loader->get(numbers,""); - if(numbers.size()){ - for(std::vector<onelab::number>::const_iterator it = numbers.begin(); - it != numbers.end(); it++){ - //std::cout << "FHF control " << (*it).getName() << "=" - //<< (*it).getChanged() << std::endl; - } +} + +void OLMsg::PrintTimers() +{ + // do a single stdio call! + std::string str; + for(std::map<std::string, double>::iterator it = _timers.begin(); + it != _timers.end(); it++){ + if(it != _timers.begin()) str += ", "; + char tmp[256]; + sprintf(tmp, "%s = %gs ", it->first.c_str(), it->second); + str += std::string(tmp); } + if(!str.size()) return; - std::vector<onelab::string> strings; - _onelabClient->get(strings,""); - if(strings.size()){ - for(std::vector<onelab::string>::const_iterator it = strings.begin(); - it != strings.end(); it++){ - loader->set(*it); - //std::cout << "FHF u " << (*it).getName() << "=" << (*it).getChanged() << std::endl; - } + if(ALWAYS_TRUE){ + if(_commSize > 1) + fprintf(stdout, "Timers : [On processor %d] %s\n", _commRank, str.c_str()); + else + fprintf(stdout, "Timers : %s\n", str.c_str()); + fflush(stdout); } - std::vector<onelab::region> regions; - _onelabClient->get(regions,""); - if(regions.size()){ - for(std::vector<onelab::region>::const_iterator it = regions.begin(); - it != regions.end(); it++){ - loader->set(*it); - } +} + +void OLMsg::PrintErrorCounter(const char *title) +{ + if(_commRank || _verbosity < 1) return; + if(!_warningCount && !_errorCount) return; + + std::string prefix = _errorCount ? "Error : " : "Warning : "; + std::string help("Check the full log for details"); + std::string line(std::max(strlen(title), help.size()), '-'); + char warn[128], err[128]; + sprintf(warn, "%5d warning%s", _warningCount, _warningCount == 1 ? "" : "s"); + sprintf(err, "%5d error%s", _errorCount, _errorCount == 1 ? "" : "s"); + + if(ALWAYS_TRUE){ + fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n", (prefix + line).c_str(), + (prefix + title).c_str(), (prefix + warn).c_str(), + (prefix + err).c_str(), (prefix + help).c_str(), + (prefix + line).c_str()); + fflush(stderr); } - return(numbers.size()+strings.size()+regions.size()); } -void OLMsg::FinalizeOnelab(){ - if(_onelabClient){ - delete _onelabClient; - _onelabClient = 0; +double OLMsg::GetValue(const char *text, double defaultval) +{ + printf("%s (default=%.16g): ", text, defaultval); + char str[256]; + char *ret = fgets(str, sizeof(str), stdin); + if(!ret || !strlen(str) || !strcmp(str, "\n")) + return defaultval; + else + return atof(str); +} + +std::string OLMsg::GetString(const char *text, std::string defaultval) +{ + printf("%s (default=%s): ", text, defaultval.c_str()); + char str[256]; + char *ret = fgets(str, sizeof(str), stdin); + if(!ret || !strlen(str) || !strcmp(str, "\n")) + return defaultval; + else + return std::string(str); +} + +int OLMsg::GetAnswer(const char *question, int defaultval, const char *zero, + const char *one, const char *two) +{ + if(two) + printf("%s\n\n0=[%s] 1=[%s] 2=[%s] (default=%d): ", question, + zero, one, two, defaultval); + else + printf("%s\n\n0=[%s] 1=[%s] (default=%d): ", question, + zero, one, defaultval); + char str[256]; + char *ret = fgets(str, sizeof(str), stdin); + if(!ret || !strlen(str) || !strcmp(str, "\n")) + return defaultval; + else + return atoi(ret); +} + +void OLMsg::InitClient(std::string sockname) +{ + if(_client) delete _client; + _client = new GmshClient(); + if(_client->Connect(sockname.c_str()) < 0){ + OLMsg::Error("Unable to connect to server on %s", sockname.c_str()); + delete _client; _client = 0; } + else + _client->Start(); +} + +void OLMsg::Barrier() +{ +} + +void OLMsg::FinalizeClient() +{ + if(_client){ + _client->Stop(); + _client->Disconnect(); + delete _client; + } + _client = 0; } +*/ diff --git a/contrib/onelab/OnelabMessage.h b/contrib/onelab/OnelabMessage.h index 5e5b8f18946392bc04d400eec13e06cedc6a51bb..bbd51fdcfb1a53ce6d3452b266829770c2e5ec83 100644 --- a/contrib/onelab/OnelabMessage.h +++ b/contrib/onelab/OnelabMessage.h @@ -91,11 +91,13 @@ class OLMsg { static std::string GetString(const char *text, std::string defaultval); static int GetAnswer(const char *question, int defaultval, const char *zero, const char *one, const char *two=0); - static void InitClient(std::string sockname); + //static void InitClient(std::string sockname); static GmshClient *GetClient(){ return _client; } - static void FinalizeClient(); - static void InitializeOnelab(const std::string &name, - const std::string &sockname); + //static void FinalizeClient(); + + static void InitializeOnelab(const std::string &name); + static void FinalizeOnelab(); + static void SetOnelabClient(onelab::client *client){ _onelabClient = client; } static void SetOnelabNumber(std::string name, double val, bool visible=true); static void GetOnelabNumber(std::string name, double *val); @@ -115,16 +117,15 @@ class OLMsg { std::vector<double> > &fopt, std::map<std::string, std::vector<std::string> > &copt); - static void AddOnelabNumberChoice(std::string name, double val); - - static void SendMergeFileRequest(const std::string &name); + static void MergeFile(const std::string &name); + //static void AddOnelabNumberChoice(std::string name, double val); // communication with loader - static onelab::remoteNetworkClient *loader; + //static onelab::remoteNetworkClient *loader; + //static int Synchronize_Down(); + //static int Synchronize_Up(); + static bool hasGmsh; - static int Synchronize_Down(); - static int Synchronize_Up(); - static void FinalizeOnelab(); static void recordFullName(const std::string &name); static std::string obtainFullName(const std::string &name); }; diff --git a/contrib/onelab/OnelabParser.cpp b/contrib/onelab/OnelabParser.cpp index 821ca83d84e0971914431c06083338f7697ec76e..398cd9d45057f78d54520e3a2080940c7a837933 100644 --- a/contrib/onelab/OnelabParser.cpp +++ b/contrib/onelab/OnelabParser.cpp @@ -13,6 +13,7 @@ namespace olkey{ static std::string include(label+"include"); static std::string message(label+"msg"); static std::string showParam(label+"show"); + static std::string showGmsh(label+"merge"); static std::string dump(label+"dump"); static std::string ifcond(label+"if"); static std::string iftrue(label+"iftrue"), ifntrue(label+"ifntrue"); @@ -522,9 +523,7 @@ void localSolverClient::parse_sentence(std::string line) { name.assign(longName(name)); get(numbers,name); if(numbers.size()){ - if(arguments[0].empty()) - numbers[0].setValue(0); - else + if(arguments[0].size()) numbers[0].setValue(atof(resolveGetVal(arguments[0]).c_str())); numbers[0].setReadOnly(1); set(numbers[0]); @@ -532,10 +531,11 @@ void localSolverClient::parse_sentence(std::string line) { else{ get(strings,name); if(strings.size()){ - if(arguments[0].empty()) + if(arguments[0].empty()) // resets an empty string strings[0].setValue(""); else strings[0].setValue(arguments[0]); + numbers[0].setReadOnly(1); set(strings[0]); } else{ @@ -643,6 +643,7 @@ void localSolverClient::modify_tags(const std::string lab, const std::string com olkey::include.assign(olkey::label+"include"); olkey::message.assign(olkey::label+"msg"); olkey::showParam.assign(olkey::label+"show"); + olkey::showGmsh.assign(olkey::label+"merge"); olkey::dump.assign(olkey::label+"dump"); olkey::ifcond.assign(olkey::label+"if"); olkey::iftrue.assign(olkey::label+"iftrue"); @@ -770,23 +771,38 @@ void localSolverClient::parse_oneline(std::string line, std::ifstream &infile) { cursor = pos+olkey::showParam.length(); if(enclosed(line.substr(cursor),arguments,pos)<1) OLMsg::Fatal("Misformed <%s> statement: (%s)", - olkey::showParam.c_str(),line.c_str()); - std::string lname=longName(arguments[0]); - std::string msg; - get(numbers,lname); - if (numbers.size()) - msg.assign(numbers[0].toChar()); - else{ - get(strings,lname); - if (strings.size()) - msg.assign(strings[0].toChar()); - else - OLMsg::Fatal("Unknown parameter <%s> in <%s> statement", - arguments[0].c_str(),olkey::showParam.c_str()); + olkey::showParam.c_str(),line.c_str()); + for(unsigned int i = 0; i < arguments.size(); i++){ + std::string lname=longName(arguments[i]); + std::string msg; + get(numbers,lname); + if (numbers.size()) + msg.assign(numbers[0].toChar()); + else{ + get(strings,lname); + if (strings.size()) + msg.assign(strings[0].toChar()); + else + OLMsg::Fatal("Unknown parameter <%s> in <%s> statement", + arguments[i].c_str(),olkey::showParam.c_str()); + } + for(unsigned int j = 0; j < msg.size(); j++) + if(msg[j] == onelab::parameter::charSep()) msg[j] = '|'; + OLMsg::Info("%s",msg.c_str()); + } + } + else if ( (pos=line.find(olkey::showGmsh)) != std::string::npos) { + // onelab.showGmsh + cursor = pos+olkey::showGmsh.length(); + if(enclosed(line.substr(cursor),arguments,pos)<1) + OLMsg::Fatal("Misformed <%s> statement: (%s)", + olkey::showGmsh.c_str(),line.c_str()); + if (OLMsg::GetOnelabString(getName()+"/MergedGeo").empty()){ + std::string fileName=resolveGetVal(arguments[0]); + OLMsg::MergeFile(fileName); + OLMsg::SetOnelabString(getName()+"/MergedGeo",fileName); + OLMsg::Info("Merge a geometry <%s> to Gmsh", fileName.c_str()); } - for(unsigned int i = 0; i < msg.size(); i++) - if(msg[i] == onelab::parameter::charSep()) msg[i] = '|'; - OLMsg::Info("%s",msg.c_str()); } else if ( (pos=line.find(olkey::dump)) != std::string::npos) { // onelab.dump @@ -1150,7 +1166,7 @@ void MetaModel::client_sentence(const std::string &name, if(!action.compare("register")){ if(isTodo(REGISTER)){ - // syntax name.register([interf...|encaps...]{,cmdl{host{,rdir}}}}) ; + // syntax name.register([interf...|native]{,cmdl{host{,rdir}}}}) ; if(!findClientByName(name)){ OLMsg::Info("Define client <%s>", name.c_str()); std::string cmdl="",host="",rdir=""; @@ -1295,6 +1311,15 @@ void MetaModel::client_sentence(const std::string &name, else OLMsg::Fatal("Unknown client <%s>", name.c_str()); } + else if(!action.compare("compute")){ + localSolverClient *c; + if(c=findClientByName(name)){ + c->checkCommandLine(); + c->compute(); + } + else + OLMsg::Fatal("Unknown client <%s>", name.c_str()); + } else if(!action.compare("up")){ if(arguments.size()%4==0){ if(isTodo(REGISTER)){ diff --git a/contrib/onelab/loader.cpp b/contrib/onelab/loader.cpp index 5b37b49c236ffcb57d88df87e30c975483beb30f..6d58d2ead0dbb56f2e657201ea4c85397ba1617b 100644 --- a/contrib/onelab/loader.cpp +++ b/contrib/onelab/loader.cpp @@ -1,9 +1,10 @@ #include "StringUtils.h" #include "onelabUtils.h" #include "OnelabClients.h" +#include "metamodel.h" +#include <algorithm> onelab::server *onelab::server::_server = 0; - std::string clientName; std::string stateToChar(){ @@ -194,17 +195,17 @@ bool menu() { choice=0; } else if (choice==4){ - metamodel(ANALYZE); + metamodel("check"); choice=0; } else if (choice==5){ - metamodel(COMPUTE); + metamodel("compute"); choice=0; } else if (choice==6){ initializeLoops(); do{ - metamodel(COMPUTE); + metamodel("compute"); }while(incrementLoops()); choice=0; } @@ -222,38 +223,66 @@ void PrintUsage(const char *name){ int main(int argc, char *argv[]){ bool launchMenu=false; - parseMode todo=COMPUTE; int i = 1; - - clientName.assign("meta"); - OLMsg::loader = new onelab::remoteNetworkClient(clientName, ""); - // OLMsg::_onelabclient is a onelab:LocalClient independent of MetaModel - OLMsg::InitializeOnelab("onelab",""); - + std::string caseName="", todo="compute", modelName="", workingDir=""; + onelab::remoteNetworkClient *client = 0; while(i < argc) { if(argv[i][0] == '-') { if(!strcmp(argv[i] + 1, "a")) { i++; - todo=ANALYZE; + todo="check"; } else if(!strcmp(argv[i] + 1, "i")) { i++; launchMenu=true; } + else if(!strcmp(argv[i] + 1, "onelab")) { + std::string clientName=argv[i+1]; + client = new onelab::remoteNetworkClient(clientName,argv[i+2]); + if(client){ + std::string action, cmd; + + std::vector<onelab::string> ps; + client->get(ps,clientName+"/Action"); + if(ps.size() && ps[0].getValue().size()) + action.assign(ps[0].getValue()); + + if(!action.compare("compute")){ + std::vector<onelab::string> ps; + client->get(ps,clientName+"/FullCmdLine"); + if(ps.size() && ps[0].getValue().size()) + cmd.assign(ps[0].getValue()); + + if(cmd.size()){ + OLMsg::Info("Loader calls <%s>",cmd.c_str()); + SystemCall(cmd.c_str(),true); //true->blocking + } + else + OLMsg::Info("No full command line found for <%s>", + clientName.c_str()); + } + OLMsg::Info("Stopping client <%s>", clientName.c_str()); + delete client; + } + exit(1); + } } else { - std::string caseName=argv[i]; - if(caseName.size()){ - modelName.assign(SplitFileName(caseName)[1]); - workingDir.assign(SplitFileName(caseName)[0]); - } - else - OLMsg::Fatal("No valid input model name."); + caseName=argv[i]; + modelName.assign(SplitFileName(caseName)[1]); + workingDir.assign(SplitFileName(caseName)[0]); i++; } } + if(caseName.empty()) + OLMsg::Fatal("No valid input model name <%s>.", caseName.c_str()); + + OLMsg::InitializeOnelab("onelab"); + OLMsg::SetOnelabString("Arguments/FileName",modelName); + OLMsg::SetOnelabString("Arguments/WorkingDir",workingDir); + if(launchMenu) menu(); else diff --git a/contrib/onelab/metamodel.cpp b/contrib/onelab/metamodel.cpp index 818d31fa4ad3e5f47f9e3635cc33f9b1ab7ce655..6943690d02c9c7b6565fcef98b0c93aca3d6a5ab 100644 --- a/contrib/onelab/metamodel.cpp +++ b/contrib/onelab/metamodel.cpp @@ -3,26 +3,35 @@ void initializeMetamodel(onelab::client *client) { + //called by "metamodel_cb" + //copies the Msg::_onelabClient to OLMsg::_onelabClient + //This pointer refers to an object of class localGmsh() (cf GmshMessage.cpp) + //which is a onelab::client with sone Gmsh features (merge and messages). OLMsg::SetOnelabClient(client); } int metamodel(const std::string &action){ + OLMsg::Info("Start metamodel"); parseMode todo; - if(action == "check"){ + if(action == "initialize") + todo = INITIALIZE; + else if(action == "check") todo = ANALYZE; - } else if(action == "compute"){ todo = COMPUTE; } else{ - todo = STOP; + OLMsg::Fatal("Unknown action <%s>", action.c_str()); } std::string modelName = OLMsg::GetOnelabString("Arguments/FileName"); std::string workingDir = OLMsg::GetOnelabString("Arguments/WorkingDir"); std::string clientName = "meta"; + std::cout << "FHF ModelName=" << modelName << std::endl; + std::cout << "FHF WorkingDir=" << workingDir << std::endl; + MetaModel *myModel = new MetaModel(clientName, workingDir, clientName, modelName); myModel->setTodo(todo); @@ -53,4 +62,5 @@ int metamodel(const std::string &action){ delete myModel; OLMsg::Info("Leave metamodel"); + OLMsg::Info("=============================================="); }