From 12943549b258c8561fd700037df8a62b9ff86378 Mon Sep 17 00:00:00 2001 From: Sarath Date: Mon, 13 May 2024 11:53:21 +0200 Subject: [PATCH 1/9] change to fix id --- atomrdf/structure.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/atomrdf/structure.py b/atomrdf/structure.py index 4a76c18..1cfcfdf 100644 --- a/atomrdf/structure.py +++ b/atomrdf/structure.py @@ -1083,6 +1083,17 @@ def __delitem__(self, val): # now the graph has to be updated accordingly self.delete(indices=list(val)) + def write_poscar_id(self, outfile): + lines = [] + with open(outfile, "r") as fin: + for line in fin: + lines.append(line) + lines[0] = self.sample.toPython() + "\n" + with open(outfile, "w") as fout: + for line in lines: + fout.write(line) + + def to_file(self, outfile, format='lammps-dump', customkeys=None, customvals=None, compressed=False, timestep=0, species=None, add_sample_id=True): @@ -1090,14 +1101,7 @@ def to_file(self, outfile, format='lammps-dump', customkeys=None, customvals=Non compressed=compressed, timestep=timestep, species=species) if format == 'poscar': if add_sample_id and (self.sample is not None): - lines = [] - with open(outfile, "r") as fin: - for line in fin: - lines.append(line) - lines[0] = self.sample.toPython() + "\n" - with open(outfile, "w") as fout: - for line in lines: - fout.write(line) + self.write_poscar_id(outfile) def to_graph(self): From b109b6bbb603689327c192201969236573141de2 Mon Sep 17 00:00:00 2001 From: Sarath Date: Mon, 13 May 2024 11:55:22 +0200 Subject: [PATCH 2/9] change write call --- atomrdf/graph.py | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/atomrdf/graph.py b/atomrdf/graph.py index d579c24..1789fd3 100644 --- a/atomrdf/graph.py +++ b/atomrdf/graph.py @@ -1239,24 +1239,7 @@ def to_file(self, sample, filename=None, format="poscar", add_sample_id=True): filename = os.path.join(os.getcwd(), "out") sys = self.get_system_from_sample(sample) - - if format == "ase": - return sys.write.ase() - elif format == "poscar": - asesys = sys.write.ase() - write(filename, asesys, format="vasp") - if add_sample_id: - lines = [] - with open(filename, "r") as fin: - for line in fin: - lines.append(line) - lines[0] = sample.toPython() + "\n" - with open(filename, "w") as fout: - for line in lines: - fout.write(line) - else: - asesys = sys.write.ase() - write(filename, asesys, format=format) + sys.to_file(filename, format=format, add_sample_id=add_sample_id) def enable_workflow(self, workflow_object, workflow_environment=None, workflow_module=None): self.workflow.inform_graph(workflow_object, From 3eb5e839208143e9a4c51dddaddc046c9a8585ec Mon Sep 17 00:00:00 2001 From: Sarath Date: Mon, 13 May 2024 15:23:46 +0200 Subject: [PATCH 3/9] rewrite write method --- atomrdf/structure.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/atomrdf/structure.py b/atomrdf/structure.py index 1cfcfdf..455d15c 100644 --- a/atomrdf/structure.py +++ b/atomrdf/structure.py @@ -16,7 +16,7 @@ import shutil import tarfile import warnings - +from ase.io import write import pyscal3.structure_creator as pcs from pyscal3.grain_boundary import GrainBoundary @@ -1097,12 +1097,22 @@ def write_poscar_id(self, outfile): def to_file(self, outfile, format='lammps-dump', customkeys=None, customvals=None, compressed=False, timestep=0, species=None, add_sample_id=True): - inputmethods.to_file(self, outfile, format=format, customkeys=customkeys, customvals=customvals, - compressed=compressed, timestep=timestep, species=species) - if format == 'poscar': + if format == "ase": + return self.write.ase() + elif format == "poscar": + asesys = self.write.ase() + write(outfile, asesys, format="vasp") if add_sample_id and (self.sample is not None): self.write_poscar_id(outfile) - + elif format == "lammps-dump": + inputmethods.to_file(self, outfile, format='lammps-dump', customkeys=customkeys, customvals=customvals, + compressed=compressed, timestep=timestep, species=species) + elif format == "lammps-data": + asesys = self.write.ase() + write(outfile, asesys, format='lammps-data', atom_style='atomic') + else: + asesys = self.write.ase() + write(outfile, asesys, format=format) def to_graph(self): """ From 3e5a7226cc644c97620d2a6c554fde3d667d0e26 Mon Sep 17 00:00:00 2001 From: Sarath Date: Mon, 13 May 2024 16:26:42 +0200 Subject: [PATCH 4/9] add ids for QE input files --- atomrdf/structure.py | 72 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/atomrdf/structure.py b/atomrdf/structure.py index 455d15c..14acf08 100644 --- a/atomrdf/structure.py +++ b/atomrdf/structure.py @@ -1092,11 +1092,72 @@ def write_poscar_id(self, outfile): with open(outfile, "w") as fout: for line in lines: fout.write(line) - + + def write_quatum_espresso_id(self, outfile): + lines = [] + lines.append(f"! {self.sample.toPython()}\n") + with open(outfile, "r") as fin: + for line in fin: + lines.append(line) + with open(outfile, "w") as fout: + for line in lines: + fout.write(line) def to_file(self, outfile, format='lammps-dump', customkeys=None, customvals=None, - compressed=False, timestep=0, species=None, add_sample_id=True): + compressed=False, timestep=0, species=None, add_sample_id=True, + input_data=None, pseudopotentials=None, + kspacing=None, kpts=None, + koffset=(0, 0, 0), + crystal_coordinates=False): + """ + Write the structure to a file in the specified format. + Parameters + ---------- + outfile : str + The path to the output file. + format : str, optional + The format of the output file. Defaults to 'lammps-dump'. + customkeys : list, optional + A list of custom keys to include in the output file. Defaults to None. + Only valid if format is 'lammps-dump'. + customvals : list, optional + A list of custom values corresponding to the custom keys. Defaults to None. + Only valid if format is 'lammps-dump'. + compressed : bool, optional + Whether to compress the output file. Defaults to False. + timestep : int, optional + The timestep value to include in the output file. Defaults to 0. + Only valid if format is 'lammps-dump'. + species : list, optional + A list of species to include in the output file. Defaults to None. + Only valid for ASE, if species is not specified. + add_sample_id : bool, optional + Whether to add a sample ID to the output file. Defaults to True. + Only valid for poscar and quantum-espresso formats. + input_data : str, optional + Additional input data to include in the output file. Defaults to None. + Only valid for quantum-espresso format. See ASE write docs for more information. + pseudopotentials : str, optional + The path to the pseudopotentials file. Defaults to None. + Only valid for quantum-espresso format. See ASE write docs for more information. + kspacing : float, optional + The k-spacing value to include in the output file. Defaults to None. + Only valid for quantum-espresso format. See ASE write docs for more information. + kpts : list, optional + A list of k-points to include in the output file. Defaults to None. + Only valid for quantum-espresso format. See ASE write docs for more information. + koffset : tuple, optional + The k-offset values to include in the output file. Defaults to (0, 0, 0). + Only valid for quantum-espresso format. See ASE write docs for more information. + crystal_coordinates : bool, optional + Whether to include crystal coordinates in the output file. Defaults to False. + Only valid for quantum-espresso format. See ASE write docs for more information. + + Returns + ------- + None + """ if format == "ase": return self.write.ase() elif format == "poscar": @@ -1110,6 +1171,13 @@ def to_file(self, outfile, format='lammps-dump', customkeys=None, customvals=Non elif format == "lammps-data": asesys = self.write.ase() write(outfile, asesys, format='lammps-data', atom_style='atomic') + elif format == "quantum-espresso": + asesys = self.write.ase() + write(outfile, asesys, format='espresso-in', input_data=input_data, + pseudopotentials=pseudopotentials, kspacing=kspacing, + kpts=kpts, koffset=koffset, crystal_coordinates=crystal_coordinates) + if add_sample_id and (self.sample is not None): + self.write_quatum_espresso_id(outfile) else: asesys = self.write.ase() write(outfile, asesys, format=format) From 4af8cf55850a1b14120b8bbed672ad0ef94d55b9 Mon Sep 17 00:00:00 2001 From: Sarath Date: Mon, 13 May 2024 16:29:00 +0200 Subject: [PATCH 5/9] fix write function in graph --- atomrdf/graph.py | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/atomrdf/graph.py b/atomrdf/graph.py index 1789fd3..8a27a8f 100644 --- a/atomrdf/graph.py +++ b/atomrdf/graph.py @@ -1215,7 +1215,11 @@ def get_system_from_sample(self, sample): sys.graph = self return sys - def to_file(self, sample, filename=None, format="poscar", add_sample_id=True): + def to_file(self, sample, filename=None, format="poscar", add_sample_id=True, + input_data=None, pseudopotentials=None, + kspacing=None, kpts=None, + koffset=(0, 0, 0), + crystal_coordinates=False): """ Save a given sample to a file @@ -1227,9 +1231,28 @@ def to_file(self, sample, filename=None, format="poscar", add_sample_id=True): filename: string name of output file - format: string, {"lammps-dump","lammps-data", "poscar"} + format: string, {"lammps-dump","lammps-data", "poscar", 'cif', 'quantum-espresso'} or any format supported by ase + input_data : str, optional + Additional input data to include in the output file. Defaults to None. + Only valid for quantum-espresso format. See ASE write docs for more information. + pseudopotentials : str, optional + The path to the pseudopotentials file. Defaults to None. + Only valid for quantum-espresso format. See ASE write docs for more information. + kspacing : float, optional + The k-spacing value to include in the output file. Defaults to None. + Only valid for quantum-espresso format. See ASE write docs for more information. + kpts : list, optional + A list of k-points to include in the output file. Defaults to None. + Only valid for quantum-espresso format. See ASE write docs for more information. + koffset : tuple, optional + The k-offset values to include in the output file. Defaults to (0, 0, 0). + Only valid for quantum-espresso format. See ASE write docs for more information. + crystal_coordinates : bool, optional + Whether to include crystal coordinates in the output file. Defaults to False. + Only valid for quantum-espresso format. See ASE write docs for more information. + Returns ------- None @@ -1239,7 +1262,9 @@ def to_file(self, sample, filename=None, format="poscar", add_sample_id=True): filename = os.path.join(os.getcwd(), "out") sys = self.get_system_from_sample(sample) - sys.to_file(filename, format=format, add_sample_id=add_sample_id) + sys.to_file(filename, format=format, add_sample_id=add_sample_id, input_data=input_data, + pseudopotentials=pseudopotentials, kspacing=kspacing, + kpts=kpts, koffset=koffset, crystal_coordinates=crystal_coordinates) def enable_workflow(self, workflow_object, workflow_environment=None, workflow_module=None): self.workflow.inform_graph(workflow_object, From f35b4b8be2bb3dd0ec3d0e911f27ec0658736ebb Mon Sep 17 00:00:00 2001 From: Sarath Date: Wed, 15 May 2024 09:27:00 +0200 Subject: [PATCH 6/9] add method to dump --- atomrdf/graph.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/atomrdf/graph.py b/atomrdf/graph.py index 8a27a8f..23a5efe 100644 --- a/atomrdf/graph.py +++ b/atomrdf/graph.py @@ -803,6 +803,28 @@ def write(self, filename, format="json-ld"): with open(filename, "w") as fout: fout.write(self.graph.serialize(format=format)) + def close(self, filename, format="json-ld"): + """ + Close the graph and write to a file + + Parameters + ---------- + filename: string + name of output file + + Returns + ------- + None + """ + self.write(filename, format=format) + + def dump(self, filename): + """ + Pickle the KnowledgeGraph as a python object + """ + with open(filename, "wb") as fout: + pickle.dumps(self, fout) + def archive(self, package_name, format="turtle", compress=True, add_simulations=False): """ Publish a dataset from graph including per atom quantities. @@ -1272,7 +1294,7 @@ def enable_workflow(self, workflow_object, workflow_environment=None, workflow_m workflow_module=workflow_module) def add_workflow(self, job, workflow_environment=None, workflow_module=None, job_dicts=None, - add_intermediate_jobs=False): + add_intermediate_jobs=False): self.workflow.to_graph(job, workflow_environment=workflow_environment, workflow_module=workflow_module, job_dicts=job_dicts, From 1a92eac857a25064d98825d1f886c5cf6c7ede38 Mon Sep 17 00:00:00 2001 From: Sarath Date: Wed, 15 May 2024 09:30:24 +0200 Subject: [PATCH 7/9] remove dumping and have closing --- atomrdf/graph.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/atomrdf/graph.py b/atomrdf/graph.py index 23a5efe..66b9b2f 100644 --- a/atomrdf/graph.py +++ b/atomrdf/graph.py @@ -34,6 +34,7 @@ import logging import warnings import re +import pickle # from pyscal3.core import System from pyscal3.atoms import Atoms @@ -817,13 +818,6 @@ def close(self, filename, format="json-ld"): None """ self.write(filename, format=format) - - def dump(self, filename): - """ - Pickle the KnowledgeGraph as a python object - """ - with open(filename, "wb") as fout: - pickle.dumps(self, fout) def archive(self, package_name, format="turtle", compress=True, add_simulations=False): """ From 745111c545724b3842c7df40f6a781f467e54acd Mon Sep 17 00:00:00 2001 From: Sarath Date: Wed, 15 May 2024 09:34:07 +0200 Subject: [PATCH 8/9] add parser for qe --- examples/08_write_qe.ipynb | 108 +++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 examples/08_write_qe.ipynb diff --git a/examples/08_write_qe.ipynb b/examples/08_write_qe.ipynb new file mode 100644 index 0000000..e8c2dc4 --- /dev/null +++ b/examples/08_write_qe.ipynb @@ -0,0 +1,108 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "from atomrdf import KnowledgeGraph, System" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "kg = KnowledgeGraph()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "s = System.create.element.Fe(graph = kg)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "s.write.file('qe_input', format='quantum-espresso')" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "! sample:f2b5122e-7458-40e2-a197-4275dde7ed77\n", + "&CONTROL\n", + "/\n", + "&SYSTEM\n", + " ntyp = 1\n", + " nat = 2\n", + " ibrav = 0\n", + "/\n", + "&ELECTRONS\n", + "/\n", + "&IONS\n", + "/\n", + "&CELL\n", + "/\n", + "\n", + "ATOMIC_SPECIES\n", + "Fe 55.845 None\n", + "\n", + "K_POINTS gamma\n", + "\n", + "CELL_PARAMETERS angstrom\n", + "2.87000000000000 0.00000000000000 0.00000000000000\n", + "0.00000000000000 2.87000000000000 0.00000000000000\n", + "\u001b[Km--More--(67%)\u001b[m" + ] + } + ], + "source": [ + "! more qe_input" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "workflow-rdf", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.8" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From ac6097250801b814512213cebacad85a8f0519d3 Mon Sep 17 00:00:00 2001 From: Sarath Date: Wed, 15 May 2024 09:34:18 +0200 Subject: [PATCH 9/9] =?UTF-8?q?Bump=20version:=200.6.13=20=E2=86=92=200.6.?= =?UTF-8?q?14?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .bumpversion.cfg | 2 +- CITATION.cff | 2 +- setup.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index eeb7a32..577b356 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.6.13 +current_version = 0.6.14 commit = True tag = False diff --git a/CITATION.cff b/CITATION.cff index d0555ac..7d089c5 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -20,4 +20,4 @@ url: 'https://atomrdf.pyscal.org' license: "MIT" repository-code: https://github.com/pyscal/atomRDF type: software -version: 0.6.13 +version: 0.6.14 diff --git a/setup.py b/setup.py index 306e7da..a8c3428 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ setup( name='atomrdf', - version='0.6.13', + version='0.6.14', author='Abril Azocar Guzman, Sarath Menon', author_email='sarath.menon@pyscal.org', description='Ontology based structural manipulation and quering',