File size: 29,530 Bytes
74e10df | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 | """
WRAPSA2, the python wrapper for RASPA2.
This is intended 1. to allow users to automate + simplify their workflows and
2. to enable scaling of simulations to millions of structures.
"""
from ctypes import cdll, c_void_p, c_char_p, c_bool, cast
import os
from textwrap import dedent
from multiprocessing import Process, Pipe
import argparse
from .__init__ import __version__
from RASPA2.output_parser import parse
raspa_dir = os.path.dirname(os.path.realpath(__file__))
libraspa_dir = os.path.join(raspa_dir, "simulations/lib")
libraspa_file = next(f for f in os.listdir(libraspa_dir) if "libraspa" in f)
try:
# Open Babel >= '3.0.0'
from openbabel import pybel
ob = pybel.ob
GetAtomicNum = ob.GetAtomicNum
GetSymbol = ob.GetSymbol
PYBEL_LOADED = True
except ImportError:
try:
# Open Babel <= '2.4.0'
import pybel
ob = pybel.ob
table = ob.OBElementTable()
GetAtomicNum = table.GetAtomicNum
GetSymbol = table.GetSymbol
PYBEL_LOADED = True
except ImportError:
PYBEL_LOADED = False
def run(structure, molecule_name, temperature=273.15, pressure=101325,
helium_void_fraction=1.0, unit_cells=(1, 1, 1),
framework_name="streamed", simulation_type="MonteCarlo", cycles=2000,
init_cycles="auto", forcefield="CrystalGenerator",
input_file_type="cif"):
"""Runs a simulation with the specified parameters.
Args:
structure: The structure to test for adsorption, as a string of type
`input_file_type` (default is "cif").
molecule_name: The molecule to test for adsorption. A file of the same
name must exist in `$RASPA_DIR/share/raspa/molecules/TraPPE`.
temperature: (Optional) The temperature of the simulation, in Kelvin.
pressure: (Optional) The pressure of the simulation, in Pascals.
helium_void_fraction: (Optional) The helium void fraction of the input
structure. Required for excess adsorption back-calculation.
unit_cells: (Optional) The number of unit cells to use, by dimension.
framework_name: (Optional) If not streaming, this will load the
structure at `$RASPA_DIR/share/raspa/structures`. Ignored if
streaming.
simulation_type: (Optional) The type of simulation to run, defaults
to "MonteCarlo".
cycles: (Optional) The number of simulation cycles to run.
init_cycles: (Optional) The number of initialization cycles to run.
Defaults to the minimum of cycles / 2 and 10,000.
forcefield: (Optional) The forcefield to use. Name must match a folder
in `$RASPA_DIR/share/raspa/forcefield`, which contains the properly
named `.def` files.
input_file_type: (Optional) The type of input structure. Assumes cif.
Returns:
A string representing the contents of a simulation input file.
The goal of this function is to mask the complexity of RASPA by limiting
parameters and assuming sensible defaults. This should streamline common-
case usage, but it means that this function won't work in all use cases.
In these cases, look into loading your own simulation input file and
passing it to `RASPA.run_script`.
"""
return parse(run_script(create_script(**locals()), structure))
def run_script(input_script, structure=None, stream=True):
"""Runs RASPA on the inputted structure, returning simulation results.
Args:
input_script: A RASPA simulation script, as a loaded string.
structure: (Optional) Data encoding a CIF, MOL, or CSSR file. If not
specified, the program will look for the file specified by
"FrameworkName" in `$RASPA_DIR/share/raspa/structures`.
stream: (Optional) Reads arguments from strings and writes simulation
results to stdout. NOTE: Only functional on Linux systems.
Returns:
A string representing the output data. (TODO This should be parsed
and returned as a mutable data structure)
If a structure is not specified, then nothing will be streamed and this
will output files and folders. Otherwise, this function will return the
output of RASPA, as a string.
"""
# This supports `pybel.Molecule` objects with charge data by converting
# them into RASPA-formatted cif strings
if PYBEL_LOADED and isinstance(structure, pybel.Molecule):
structure = pybel_to_raspa_cif(structure)
# This supports python objects grabbed from json files or databases
elif isinstance(structure, dict):
structure = pybel_to_raspa_cif(json_to_pybel(structure))
# RASPA leaks a lot of memory, which is an issue in high-throughput
# screening. Calling each simulation in a subprocess adds complexity and
# time, but allows still reachable memory to be garbage collected by the
# OS and also protects against segfaults destablizing an entire engine.
parent_conn, child_conn = Pipe()
p = Process(target=_script_subprocess, args=(input_script, structure or "",
raspa_dir, stream,
child_conn))
p.start()
if stream:
output = parent_conn.recv()
p.join()
p.terminate()
if stream:
return output
def _script_subprocess(input_script, structure, raspa_dir, stream, conn):
"""Loads and runs libraspa2. Called through multiprocessing.Process.
Intended as a workaround for RASPA's memory leaks. Once RASPA is running
valgrind pure, remove this approach and replace with a direct call.
"""
libraspa = cdll.LoadLibrary(os.path.join(libraspa_dir, libraspa_file))
libraspa.run.argtypes = (c_char_p, c_char_p, c_char_p, c_bool)
libraspa.run.restype = c_void_p
ptr = libraspa.run(input_script.encode("ascii"),
structure.encode("ascii"),
raspa_dir.encode("ascii"), stream)
if stream:
conn.send(cast(ptr, c_char_p).value[:].decode("utf-8"))
conn.close()
def create_script(molecule_name, temperature=273.15, pressure=101325,
helium_void_fraction=1.0, unit_cells=(1, 1, 1),
simulation_type="MonteCarlo", cycles=2000,
init_cycles="auto", forcefield="CrystalGenerator",
input_file_type="cif", **kwargs):
"""Creates a RASPA simulation input file from parameters.
Args:
molecule_name: The molecule to test for adsorption. A file of the same
name must exist in `$RASPA_DIR/share/raspa/molecules/TraPPE`.
temperature: (Optional) The temperature of the simulation, in Kelvin.
pressure: (Optional) The pressure of the simulation, in Pascals.
helium_void_fraction: (Optional) The helium void fraction of the input
structure. Required for excess adsorption back-calculation.
unit_cells: (Optional) The number of unit cells to use, by dimension.
simulation_type: (Optional) The type of simulation to run, defaults
to "MonteCarlo".
cycles: (Optional) The number of simulation cycles to run.
init_cycles: (Optional) The number of initialization cycles to run.
Defaults to the minimum of cycles / 2 and 10,000.
forcefield: (Optional) The forcefield to use. Name must match a folder
in `$RASPA_DIR/share/raspa/forcefield`, which contains the properly
named `.def` files.
input_file_type: (Optional) The type of input structure. Assumes cif.
charged: (Optional) A boolean indicating whether or not to use Ewald
charge parameters.
Returns:
A string representing the contents of a simulation input file.
The goal of this function is to mask the complexity of RASPA by limiting
parameters and assuming sensible defaults. This should streamline common-
case usage, but it means that this function won't work in all use cases.
In these cases, look into loading your own simulation input file and
passing it to `RASPA.run_script`.
"""
is_mol = "yes" if input_file_type.lower() == "mol" else "no"
print_every = cycles // 10
a, b, c = unit_cells
if init_cycles == "auto":
init_cycles = min(cycles // 2, 10000)
return dedent("""
SimulationType {simulation_type}
NumberOfCycles {cycles}
NumberOfInitializationCycles {init_cycles}
PrintEvery {print_every}
RestartFile no
Forcefield {forcefield}
CutOff 12.8
ChargeMethod Ewald
EwaldPrecision 1e-6
UseChargesFromMOLFile {is_mol}
Framework 0
FrameworkName streamed
InputFileType {input_file_type}
UnitCells {a} {b} {c}
HeliumVoidFraction {helium_void_fraction}
ExternalTemperature {temperature}
ExternalPressure {pressure}
Movies no
WriteMoviesEvery 100
Component 0 MoleculeName {molecule_name}
StartingBead 0
MoleculeDefinition TraPPE
IdealGasRosenbluthWeight 1.0
TranslationProbability 1.0
RotationProbability 1.0
ReinsertionProbability 1.0
SwapProbability 1.0
CreateNumberOfMolecules 0
""".format(**locals())).strip()
def run_mixture(structure, molecules, mol_fractions, temperature=273.15,
pressure=101325, helium_void_fraction=1.0,
unit_cells=(1, 1, 1), simulation_type="MonteCarlo",
cycles=2000, init_cycles="auto", forcefield="CrystalGenerator",
input_file_type="cif"):
"""Runs a simulation with mixture of gases.
Args:
structure: The structure to test for adsorption, as a string of type
`input_file_type` (default is "cif").
molecules: The molecules to test for adsorption. Files of the same
names must exist in `$RASPA_DIR/share/raspa/molecules/TraPPE`.
mol_fractions: The mol fractions of each gas that you want to separate.
Corresponds to the `molecules` list.
temperature: (Optional) The temperature of the simulation, in Kelvin.
pressure: (Optional) The pressure of the simulation, in Pascals.
helium_void_fraction: (Optional) The helium void fraction of the input
structure. Required for excess adsorption back-calculation.
unit_cells: (Optional) The number of unit cells to use, by dimension.
simulation_type: (Optional) The type of simulation to run, defaults
to "MonteCarlo".
cycles: (Optional) The number of simulation cycles to run.
init_cycles: (Optional) The number of initialization cycles to run.
Defaults to the minimum of cycles / 2 and 10,000.
forcefield: (Optional) The forcefield to use. Name must match a folder
in `$RASPA_DIR/share/raspa/forcefield`, which contains the properly
named `.def` files.
input_file_type: (Optional) The type of input structure. Assumes cif.
Returns:
A string representing the contents of a simulation input file.
The goal of this function is to mask the complexity of RASPA by limiting
parameters and assuming sensible defaults. This should streamline common-
case usage, but it means that this function won't work in all use cases.
In these cases, look into loading your own simulation input file and
passing it to `RASPA.run_script`.
"""
is_mol = "yes" if input_file_type.lower() == "mol" else "no"
print_every = cycles // 10
a, b, c = unit_cells
molecule_list = " ".join(str(n) for n in range(len(molecules)))
molecule_count = len(molecules)
if init_cycles == "auto":
init_cycles = min(cycles // 2, 10000)
script = dedent("""
SimulationType {simulation_type}
NumberOfCycles {cycles}
NumberOfInitializationCycles {init_cycles}
PrintEvery {print_every}
RestartFile no
Forcefield {forcefield}
CutOff 12.8
ChargeMethod Ewald
EwaldPrecision 1e-6
UseChargesFromMOLFile {is_mol}
Framework 0
FrameworkName streamed
InputFileType {input_file_type}
UnitCells {a} {b} {c}
HeliumVoidFraction {helium_void_fraction}
ExternalTemperature {temperature}
ExternalPressure {pressure}
Movies no
WriteMoviesEvery 100
""".format(**locals())).strip()
for i, (molecule, fraction) in enumerate(zip(molecules, mol_fractions)):
script += dedent("""
Component {i} MoleculeName {molecule}
StartingBead 0
MoleculeDefinition TraPPE
MolFraction {fraction}
IdentityChangeProbability 1.0
NumberOfIdentityChanges {molecule_count}
IdentityChangesList {molecule_list}
IdealGasRosenbluthWeight 1.0
TranslationProbability 1.0
RotationProbability 1.0
ReinsertionProbability 1.0
SwapProbability 1.0
CreateNumberOfMolecules 0
""".format(**locals()))
return parse(run_script(script, structure))
def get_geometric_surface_area(structure, unit_cells=(1, 1, 1), cycles=500,
input_file_type="cif", units="m^2/g",
forcefield="CrystalGenerator"):
"""Calculates the geometric surface area of an inputted structure.
Args:
structure: The structure to use, as a string of type
`input_file_type` (default is "cif").
input_file_type: (Optional) The type of input structure. Assumes cif.
unit_cells: (Optional) The number of unit cells to use, by dimension.
cycles: (Optional) The number of simulation cycles to run.
units: (Optional) The units in which to return the surface area. Can be
"m^2/g", "A^2", or "m^2/cm^3".
forcefield: (Optional) The forcefield to use. Name must match a folder
in `$RASPA_DIR/share/raspa/forcefield`, which contains the properly
named `.def` files.
Returns:
The geometric surface area, as a float.
"""
print_every = cycles // 10
a, b, c = unit_cells
script = dedent("""
SimulationType MonteCarlo
NumberOfCycles {cycles}
PrintEvery {print_every}
PrintPropertiesEvery {print_every}
Forcefield {forcefield}
CutOff 12.8
Framework 0
FrameworkName streamed
InputFileType {input_file_type}
UnitCells {a} {b} {c}
SurfaceAreaProbeDistance Sigma
Component 0 MoleculeName N2
StartingBead 0
MoleculeDefinition TraPPE
SurfaceAreaProbability 1.0
CreateNumberOfMolecules 0
""".format(**locals())).strip()
output = parse(run_script(script, structure))
return output["Average Surface Area"]["[{}]".format(units)][0]
def get_helium_void_fraction(structure, unit_cells=(1, 1, 1), cycles=2000,
input_file_type="cif",
forcefield="CrystalGenerator"):
"""Calculates the helium void fraction of the inputted structure.
Args:
structure: The structure to test for helium void fraction,
as a string of type 'input_file_type` (default is "cif").
unit_cells: (Optional) The number of unit cells to use, by dimension.
cycles: (Optional) The number of simulation cycles to run.
input_file_type: (Optional) The type of input structure. Assumes cif.
forcefield: (Optional) The forcefield to use. Name must match a folder
in `$RASPA_DIR/share/raspa/forcefield`, which contains the properly
named `.def` files.
Returns:
The helium void fraction of the structure, as a float.
"""
print_every = cycles // 10
a, b, c = unit_cells
script = dedent("""
SimulationType MonteCarlo
NumberOfCycles {cycles}
PrintEvery {print_every}
PrintPropertiesEvery {print_every}
Forcefield {forcefield}
CutOff 12.8
Framework 0
FrameworkName streamed
InputFileType {input_file_type}
UnitCells {a} {b} {c}
ExternalTemperature 298.0
Component 0 MoleculeName helium
MoleculeDefinition TraPPE
WidomProbability 1.0
CreateNumberOfMolecules 0
""".format(**locals())).strip()
output = parse(run_script(script, structure))
return output["Average Widom Rosenbluth factor"]["Widom"][0]
def get_pore_size_distribution(structure, unit_cells=(1, 1, 1), cycles=500,
input_file_type="cif",
forcefield="CrystalGenerator",
bins=50):
"""Calculates the pore size distribution of the inputted structure.
Args:
structure: The structure to test for helium void fraction,
as a string of type 'input_file_type` (default is "cif").
unit_cells: (Optional) The number of unit cells to use, by dimension.
cycles: (Optional) The number of simulation cycles to run.
input_file_type: (Optional) The type of input structure. Assumes cif.
forcefield: (Optional) The forcefield to use. Name must match a folder
in `$RASPA_DIR/share/raspa/forcefield`, which contains the properly
named `.def` files.
bins: (Optional) The number of bins to use in the output histogram.
Returns:
A list of the form [[x1, x2, ..., xn], [y1, y2, ..., yn]], where x is
the binned pore size (in Angstroms) and y is the partial pore volume
(in cm^3 / g).
"""
print_every = cycles // 10
a, b, c = unit_cells
script = dedent("""
SimulationType PSD
NumberOfCycles {cycles}
NumberOfInitializationCycles 0
PrintEvery {print_every}
PSDProbeDistance Sigma
WritePSDHistogramEvery {print_every}
PSDHistogramSize {bins}
PSDRange 12.5
Forcefield {forcefield}
CutOff 12.8
Framework 0
FrameworkName streamed
InputFileType {input_file_type}
UnitCells {a} {b} {c}
ExternalTemperature 100.0
""".format(**locals())).strip()
output = run_script(script, structure)
# PSD returns a different data structure which must be parsed separately.
# Luckily, it's an easy parse. Skip # commented lines, split by whitespace.
info = [[float(x) for x in r.split()] for r in output.strip().splitlines()
if r[0] != "#"]
return [[i[0] for i in info], [i[2] for i in info]]
def get_density(molecule_name, temperature=273.15, pressure=101325,
cycles=5000, init_cycles="auto",
forcefield="CrystalGenerator"):
"""Calculates the density of a gas through an NPT ensemble.
Args:
molecule_name: The molecule to test for adsorption. A file of the same
name must exist in `$RASPA_DIR/share/raspa/molecules/TraPPE`.
temperature: (Optional) The temperature of the simulation, in Kelvin.
pressure: (Optional) The pressure of the simulation, in Pascals.
cycles: (Optional) The number of simulation cycles to run.
init_cycles: (Optional) The number of initialization cycles to run.
Defaults to the minimum of cycles / 2 and 10,000.
forcefield: (Optional) The forcefield to use. Name must match a folder
in `$RASPA_DIR/share/raspa/forcefield`, which contains the properly
named `.def` files.
Returns:
The density, as a float, in kg/m^3.
"""
print_every = cycles // 10
if init_cycles == "auto":
init_cycles = min(cycles // 2, 10000)
script = dedent("""
SimulationType {simulation_type}
NumberOfCycles {cycles}
NumberOfInitializationCycles {init_cycles}
PrintEvery {print_every}
Forcefield {forcefield}
Box 0
BoxLengths 30 30 30
ExternalTemperature {temperature}
ExternalPressure {pressure}
VolumeChangeProbability 0.25
Component 0 MoleculeName {molecule_name}
MoleculeDefinition TraPPE
TranslationProbability 0.5
ReinsertionProbability 0.5
CreateNumberOfMolecules 256
""".format(**locals())).strip()
output = parse(run_script(script))
return output["Average Density"]["[kg/m^3]"][0]
def json_to_pybel(data):
"""Converts a python data structure to pybel.Molecule.
The data structure is a plain python object of the form:
```
{
"atoms": [{"location": [0, 0, 0], "element": "H", "label": "H1",
"charge": 0}, ...],
"bonds": [{"source": 0, "target": 0, "order": 1}, ...],
"unitcell": [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
}
```
It is referred to as "json" because this data structure is intended to
be read from and written to json files or databases.
As RASPA makes no use of bond information, this field is ignored.
The labels are stripped and replaced with "MOF_{element}", in accordance
with the CrystalGenerator forcefield notation. Therefore, labels are also
ignored.
Args:
data: the molecule, as a python object
Returns:
An instance of `pybel.Molecule`
"""
if not PYBEL_LOADED:
raise ImportError("Open Babel not installed.")
if "building_blocks" in data:
data["atoms"] = [a for bb in data["building_blocks"]
for a in bb["atoms"]]
obmol = pybel.ob.OBMol()
obmol.BeginModify()
for atom in data["atoms"]:
obatom = obmol.NewAtom()
obatom.SetAtomicNum(GetAtomicNum(str(atom["element"])))
obatom.SetVector(*atom["location"])
uc = pybel.ob.OBUnitCell()
uc.SetData(*(pybel.ob.vector3(*v) for v in data["unitcell"]))
uc.SetSpaceGroup("P1")
obmol.CloneData(uc)
obmol.EndModify()
mol = pybel.Molecule(obmol)
# Add partial charges
if "charge" in data["atoms"][0]:
mol.OBMol.SetPartialChargesPerceived()
for atom, pyatom in zip(data["atoms"], mol.atoms):
pyatom.OBAtom.SetPartialCharge(atom["charge"])
return mol
def pybel_to_raspa_cif(structure):
"""Converts instances of `pybel.Molecule` to a RASPA charged cif format."""
if not PYBEL_LOADED:
raise ImportError("Open Babel not installed.")
uc = structure.unitcell
a, b, c = uc.GetA(), uc.GetB(), uc.GetC()
alpha, beta, gamma = uc.GetAlpha(), uc.GetBeta(), uc.GetGamma()
cif = dedent("""
data_I
_chemical_name_common ''
_cell_length_a {a:.4f}
_cell_length_b {b:.4f}
_cell_length_c {c:.4f}
_cell_angle_alpha {alpha:.0f}
_cell_angle_beta {beta:.0f}
_cell_angle_gamma {gamma:.0f}
_space_group_name_H-M_alt 'P 1'
_space_group_name_Hall 'P 1'
loop_
_symmetry_equiv_pos_as_xyz
x,y,z
loop_
_atom_site_label
_atom_site_type_symbol
_atom_site_fract_x
_atom_site_fract_y
_atom_site_fract_z
_atom_site_charge
""".format(**locals())).strip()
for atom in structure:
element = GetSymbol(atom.atomicnum)
label = "Mof_" + element
charge = atom.partialcharge
c = uc.WrapFractionalCoordinate(uc.CartesianToFractional(atom.vector))
x, y, z = c.GetX(), c.GetY(), c.GetZ()
cif += ("\n {label:<7s} {element:<4s} {x:.5f} {y:9.5f} {z:9.5f} "
"{charge:7.3f}").format(**locals())
cif += "\n_end\n"
return cif
def run_command_line():
"""Called by the `simulate` command, enables CLI interface."""
parser = argparse.ArgumentParser(description="A general purpose classical "
"simulation package.")
parser.add_argument("-v", "--version", action="version",
version=__version__)
parser.add_argument("input", type=str, help="a RASPA simulation script")
parser.add_argument("-s", "--stream", action="store_true", help="Rather "
"than inputting and outputting files, this option "
"reads strings and outputs to stdout. Can be used to "
"simplify shell scripts. NOTE: Currently only "
"functional on linux.")
parser.add_argument("-c", "--crystal", type=str, default="", help="If "
"streaming is enabled, this can be used to specify an "
"input structure without changing the RASPA script.")
args = parser.parse_args()
# Support both filepaths and data-streamed strings
if args.stream:
try:
with open(args.input) as in_file:
args.input = in_file.read()
except IOError:
pass
try:
with open(args.crystal) as in_file:
args.crystal = in_file.read()
except IOError:
pass
if args.stream:
print(run_script(args.input, args.crystal, args.stream))
else:
run_script(args.input, args.crystal, args.stream)
def get_raspa_dir():
"""Called by the `raspa-dir` command, enables easy directory switching."""
parser = argparse.ArgumentParser(description="Returns the data directory "
"for the RASPA simulation program. Use "
"'raspa-dir' to view the path, or 'cd "
"`raspa-dir`' to change directories.")
parser.add_argument("-v", "--version", action="version",
version=__version__)
print(os.path.join(raspa_dir, "share/raspa"))
if __name__ == "__main__":
run_command_line()
|