PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/projects/Simulation.java

https://github.com/abinit/abinitgui
Java | 434 lines | 277 code | 67 blank | 90 comment | 57 complexity | b027b7aa27a907c7cd0a1c13ee633910 MD5 | raw file
Possible License(s): GPL-3.0
  1. /*
  2. Copyright (c) 2009-2013 Flavio Miguel ABREU ARAUJO (flavio.abreuaraujo@uclouvain.be)
  3. Yannick GILLET (yannick.gillet@uclouvain.be)
  4. Université catholique de Louvain, Louvain-la-Neuve, Belgium
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions
  8. are met:
  9. 1. Redistributions of source code must retain the above copyright
  10. notice, this list of conditions, and the following disclaimer.
  11. 2. Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions, and the disclaimer that follows
  13. these conditions in the documentation and/or other materials
  14. provided with the distribution.
  15. 3. The names of the author may not be used to endorse or promote
  16. products derived from this software without specific prior written
  17. permission.
  18. In addition, we request (but do not require) that you include in the
  19. end-user documentation provided with the redistribution and/or in the
  20. software itself an acknowledgement equivalent to the following:
  21. "This product includes software developed by the
  22. Abinit Project (http://www.abinit.org/)."
  23. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  24. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  25. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
  27. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  30. USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  31. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  33. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. SUCH DAMAGE.
  35. For more information on the Abinit Project, please see
  36. <http://www.abinit.org/>.
  37. */
  38. package projects;
  39. import core.Atom;
  40. import core.Exec;
  41. import core.MainFrame;
  42. import core.RetMSG;
  43. import core.Utils;
  44. import java.io.BufferedWriter;
  45. import java.io.File;
  46. import java.io.FileOutputStream;
  47. import java.io.IOException;
  48. import java.io.OutputStreamWriter;
  49. import java.io.PrintWriter;
  50. import java.util.ArrayList;
  51. import projects.RemoteJob;
  52. public class Simulation {
  53. private String name = "default";
  54. private String inputFileName = "./test3.in";
  55. private RemoteJob job;
  56. public static int RUNNING = 1;
  57. public static int FINISHED = 2;
  58. public static int READY = 0;
  59. private int status;
  60. private ArrayList<Atom> listPseudos;
  61. private boolean usingExtInputFile = false;
  62. public Simulation() {
  63. status = READY;
  64. job = new RemoteJob();
  65. listPseudos = new ArrayList<>();
  66. }
  67. public String getName() {
  68. return name;
  69. }
  70. public boolean isUsingExtInputFile()
  71. {
  72. return usingExtInputFile;
  73. }
  74. public void setUsingExtInputFile(boolean usingExtInputFile)
  75. {
  76. this.usingExtInputFile = usingExtInputFile;
  77. }
  78. public ArrayList<Atom> getListPseudos()
  79. {
  80. return listPseudos;
  81. }
  82. public void setListPseudos(ArrayList<Atom> listPseudos)
  83. {
  84. this.listPseudos = listPseudos;
  85. }
  86. public void setName(String name) {
  87. this.name = name;
  88. }
  89. public String getInputFileName() {
  90. return inputFileName;
  91. }
  92. public void setInputFileName(String fileName) {
  93. this.inputFileName = fileName;
  94. }
  95. @Override
  96. public String toString() {
  97. return name;
  98. //return "Simulation(name = " + name + "; fileName = " + inputFileName + ")";
  99. }
  100. public int getStatus() {
  101. return status;
  102. }
  103. public void setStatus(int status) {
  104. this.status = status;
  105. }
  106. public RemoteJob getRemoteJob() {
  107. return job;
  108. }
  109. public void setRemoteJob(RemoteJob job) {
  110. this.job = job;
  111. }
  112. void updateStatus() {
  113. job.updateStatus();
  114. }
  115. public void createFileTree(Machine mach, MainFrame mf)
  116. {
  117. String path = mach.getSimulationPath();
  118. if (path.equals("")) {
  119. path = ".";
  120. }
  121. mach.createTree(path, mf);
  122. if(mach.getType() == Machine.REMOTE_MACHINE || mach.getType() == Machine.GATEWAY_MACHINE)
  123. {
  124. mf.getLocalExec().createTree(path);
  125. }
  126. }
  127. public boolean submit(MainFrame mf)
  128. {
  129. Machine mach = mf.getMachineDatabase().getMachine(job.getMachineName());
  130. if(mach == null)
  131. {
  132. mf.printERR("No machine selected for this simulation");
  133. return false;
  134. }
  135. // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  136. boolean isLocalMachine = (mach.getType() == Machine.LOCAL_MACHINE);
  137. boolean isRemoteGatewayMachine = (mach.getType() == Machine.GATEWAY_MACHINE);
  138. boolean isRemoteAbinitMachine = (mach.getType() == Machine.REMOTE_MACHINE);
  139. String rootPath = mach.getSimulationPath();
  140. String pathToAbinit = mach.getAbinitPath();
  141. SubmissionScript script = getRemoteJob().getScript();
  142. String inputFile;
  143. if (usingExtInputFile) {
  144. inputFile = inputFileName;
  145. } else {
  146. mf.printERR("Choose an option please ! (use an external"
  147. + " inputfile or created a inputfile)");
  148. return false;
  149. }
  150. // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  151. if (isLocalMachine && Utils.osName().startsWith("Windows")) {
  152. mf.printERR("Please connect to a remote host before submitting a simulation on Windows platform !");
  153. return false;
  154. }
  155. if(!mach.isConnected())
  156. {
  157. mf.printOUT("Trying to connect ...");
  158. mach.connection(mf);
  159. }
  160. // if ((remoteGatewayRadioButton_isSelected
  161. // || remoteAbinitRadioButton_isSelected) && remoteExec == null) {
  162. // mf.printERR("Please connect to a ABINIT host before submitting a simulation !");
  163. // return false;
  164. // }
  165. this.createFileTree(mach, mf);
  166. String inputFolder = "input";
  167. String outputFolder = "output";
  168. String wholedataFolder = "wholedata";
  169. String pseudopotFolder = "pseudopot";
  170. String logfilesFolder = "logfiles";
  171. // *************************************************************
  172. String cwd = "";
  173. String CMD = "pwd";
  174. RetMSG retmsg = mach.sendCommand(CMD, mf);
  175. if(retmsg.getRetCode() == RetMSG.SUCCES)
  176. {
  177. cwd = Utils.removeEndl(retmsg.getRetMSG());
  178. }
  179. else
  180. {
  181. mf.printERR("Not able to get working directory !");
  182. return false;
  183. }
  184. // Exec remoteExec = mach.getExec();
  185. // if (remoteExec != null) {
  186. // retmsg = remoteExec.sendCommand(CMD);
  187. // if (retmsg.getRetCode() == RetMSG.SUCCES) {
  188. // mf.printOUT("PWD: " + retmsg.getRetMSG());
  189. // cwd = Utils.removeEndl(retmsg.getRetMSG());
  190. // } else {
  191. // //mf.printERR("Error (RetVal = " + retmsg.getRetCode() + "): " + retmsg.getRetMSG());
  192. // mf.printERR("Error: " + retmsg.getRetMSG() + " !");
  193. // }
  194. // } else {
  195. // mf.printERR("First connect to an abinit host please !");
  196. // }
  197. // ********************************************************************************************************************************
  198. String inputFN = "";
  199. String sep = Utils.fileSeparator();
  200. inputFN = Utils.getLastToken(inputFile.replace('\\', '/'), "/");
  201. // Test de l'existance de inputfile
  202. if (!Utils.exists(inputFile)) {
  203. mf.printERR("The file " + inputFile + " doesn't exist !");
  204. return false;
  205. }
  206. String simName = null;
  207. if (inputFN != null) {
  208. int idx = inputFN.indexOf('.');
  209. if (idx > 0 && idx < inputFN.length()) {
  210. simName = inputFN.substring(0, idx);
  211. } else {
  212. simName = inputFN;
  213. }
  214. }
  215. if (!inputFile.equals("")) {
  216. script.setAbinitPath(pathToAbinit + "abinit");
  217. script.setInputPath(cwd + "/" + rootPath.replaceFirst("./", "") + "/" + simName + ".files");
  218. script.setLogPath(cwd + "/"
  219. + rootPath.replaceFirst("./", "") + "/" + logfilesFolder + "/" + simName + ".log");
  220. if (script.getSystem().equals("SGE")) {
  221. String PBSfileName = rootPath + sep + simName + ".SGE.sh";
  222. script.writeToFile(PBSfileName);
  223. } else if (script.getSystem().equals("SLURM")) {
  224. String PBSfileName = rootPath + sep + simName + ".SLURM.sh";
  225. script.writeToFile(PBSfileName);
  226. } else if (script.getSystem().equals("Frontend")) {
  227. String SHfileName = rootPath + sep + simName + ".sh";
  228. script.writeToFile(SHfileName);
  229. }
  230. // Envoie (copie) du fichier d'input
  231. String inputFileR = rootPath + "/" + inputFolder + "/" + inputFN;
  232. mach.putFile(inputFile + " " + inputFileR, mf);
  233. if (isRemoteGatewayMachine
  234. || isRemoteAbinitMachine) {
  235. if (Utils.osName().startsWith("Windows")) {
  236. mach.sendCommand("dos2unix " + inputFileR, mf);
  237. // TODO Util.dos2unix(new File(inputFileR)); // Transformer avant d'envoyer le fichier
  238. }
  239. }
  240. // Création du contenu du fichier de configuration (*.files)
  241. String configFileContent = "";
  242. configFileContent += cwd + "/" + rootPath.replaceFirst("./", "")
  243. + "/" + inputFolder + "/" + inputFN + "\n";
  244. configFileContent += cwd + "/" + rootPath.replaceFirst("./", "")
  245. + "/" + outputFolder + "/" + simName + ".out\n";
  246. configFileContent += cwd + "/" + rootPath.replaceFirst("./", "")
  247. + "/" + wholedataFolder + "/" + simName + "/" + simName + "i\n";
  248. configFileContent += cwd + "/" + rootPath.replaceFirst("./", "")
  249. + "/" + wholedataFolder + "/" + simName + "/" + simName + "o\n";
  250. configFileContent += cwd + "/" + rootPath.replaceFirst("./", "")
  251. + "/" + wholedataFolder + "/" + simName + "/" + simName + "\n";
  252. if (usingExtInputFile) {
  253. for(Atom at : listPseudos)
  254. {
  255. mach.putFile(at.getPSPPath() + sep + at.getPSPFileName()
  256. + " " + rootPath + "/" + pseudopotFolder
  257. + "/" + at.getPSPFileName(), mf);
  258. configFileContent += cwd + "/" + rootPath.replaceFirst("./", "")
  259. + "/" + pseudopotFolder + "/" + at.getPSPFileName() + "\n";
  260. }
  261. } else {
  262. mf.printERR("Creating input files graphically not yet supported");
  263. return false;
  264. }
  265. // Création du fichier de configuration
  266. try {
  267. String FILESfileName = rootPath + sep + simName + ".files";
  268. OutputStreamWriter fw = new OutputStreamWriter(
  269. new FileOutputStream(FILESfileName), Utils.getCharset());
  270. //FileWriter fw = new FileWriter(FILESfileName);
  271. BufferedWriter bw = new BufferedWriter(fw);
  272. PrintWriter pw = new PrintWriter(bw);
  273. pw.print(configFileContent);
  274. pw.close();
  275. bw.close();
  276. fw.close();
  277. } catch (IOException e) {
  278. //mf.printERR(e.getMessage());
  279. mf.printERR("The configuration file (*.files) could not"
  280. + " be created !");
  281. return false;
  282. }
  283. if (isRemoteGatewayMachine
  284. || isRemoteAbinitMachine) {
  285. // Envoie du fichier de configuration
  286. String configFile = rootPath + sep + simName + ".files";
  287. String configFileR = rootPath + "/" + simName + ".files";
  288. if (Utils.osName().startsWith("Windows")) {
  289. Utils.dos2unix(new File(configFile));
  290. }
  291. mach.putFile(configFile + " " + configFileR, mf);
  292. //if (Utils.osName().startsWith("Windows")) {
  293. // sendCommand("dos2unix " + configFileR);
  294. //}
  295. }
  296. // Creation du dossier simName dans wholedataFolder
  297. Utils.mkdir(rootPath + "/" + wholedataFolder + "/" + simName);
  298. if (isRemoteGatewayMachine
  299. || isRemoteAbinitMachine) {
  300. // We do it only if machine is remote !
  301. mach.mkdir(rootPath + "/" + wholedataFolder + "/" + simName, mf);
  302. }
  303. if (script.getSystem().equals("SGE")) {
  304. String sgeSHFile = rootPath + sep + simName + ".SGE.sh";
  305. String sgeSHFileR = rootPath + "/" + simName + ".SGE.sh";
  306. if (isRemoteGatewayMachine
  307. || isRemoteAbinitMachine) {
  308. // Envoie du fichier SGE
  309. mach.putFile(sgeSHFile + " " + sgeSHFileR, mf);
  310. if (Utils.osName().startsWith("Windows")) {
  311. mach.sendCommand("dos2unix " + sgeSHFileR, mf);
  312. }
  313. }
  314. // lancement des commandes d'exécution de la simulation
  315. mach.sendCommand("qsub " + sgeSHFileR, mf);
  316. } else if (script.getSystem().equals("Frontend")) {
  317. String SHFile = rootPath + sep + simName + ".sh";
  318. String SHFileR = rootPath + "/" + simName + ".sh";
  319. if (isRemoteGatewayMachine
  320. || isRemoteAbinitMachine) {
  321. /*if (Utils.osName().startsWith("Windows")) {
  322. Utils.dos2unix(new File(SHFileR));
  323. }*/
  324. // Envoie du fichier BASH
  325. mach.putFile(SHFile + " " + SHFileR, mf);
  326. if (Utils.osName().startsWith("Windows")) {
  327. mach.sendCommand("dos2unix " + SHFileR, mf);
  328. }
  329. }
  330. // lancement des commandes d'exécution de la simulation
  331. mach.sendCommand("bash "+SHFileR, mf);
  332. } else if (script.getSystem().equals("SLURM")) {
  333. String slurmSHFile = rootPath + sep + simName + ".SLURM.sh";
  334. String slurmSHFileR = rootPath + "/" + simName + ".SLURM.sh";
  335. if (isRemoteGatewayMachine
  336. || isRemoteAbinitMachine) {
  337. // Envoie du fichier SGE
  338. mach.putFile(slurmSHFile + " " + slurmSHFileR, mf);
  339. if (Utils.osName().startsWith("Windows")) {
  340. mach.sendCommand("dos2unix " + slurmSHFileR, mf);
  341. }
  342. }
  343. // lancement des commandes d'exécution de la simulation
  344. mach.sendCommand("sbatch " + slurmSHFileR, mf);
  345. }
  346. } else {
  347. mf.printERR("Please setup the inputfile textfield !");
  348. return false;
  349. }
  350. if (isLocalMachine) {
  351. mf.printOUT("The simulation was submitted to the local Abinit machine.");
  352. } else {
  353. mf.printOUT("The simulation was submitted to the remote Abinit"
  354. + " machine "+mach.getName()+".");
  355. }
  356. mf.printOUT("The submission thread ended successfully! (Abinit)");
  357. return true;
  358. }
  359. }