Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,27 @@ version = "0.2.2"
[deps]
AtomsBase = "a963bdd2-2df7-4f54-a1ee-49d51e6be12a"
AtomsIO = "1692102d-eeb4-4df9-807b-c9517f998d44"
AtomsSystems = "bd665c09-60e6-46be-bbfe-8f6ec2b68ee8"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
PubChemCrawler = "30e472fa-2b12-4c0b-9705-07d174b7a4e1"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[compat]
AtomsBase = "0.5"
AtomsIO = "0.3"
AtomsSystems = "0.1.2"
JSON = "0.21.4"
LinearAlgebra = "1.9"
PubChemCrawler = "1.2"
Random = "1.9"
Reexport = "1"
StaticArrays = "1.9"
StatsBase = "0.34"
Unitful = "1.19"
julia = "1.10"

Expand Down
33 changes: 22 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@
This package provides utilities to build atomic structures. At the moment the functionality is limited - see examples below. The intention is that over time this package becomes the de facto standard for generating structures in the JuliaMolSim ecosystem. Contributions are very welcome.


## Preliminary Documentation
## Functionality List

Currently there are just two exported functions to build materials:
* `bulk`
* `rattle!`
In addition we overload
* `repeat` (with alias `*`)
The following functions are used to build and manipulate systems

* `bulk` - generate bulk solids
* `rattle_positions` and `rattle_positions!` - reexports from [AtomsSystems](https://github.com/JuliaMolSim/AtomsSystems.jl)
* `repeat` - repeating system - reexport from [AtomsSystems](https://github.com/JuliaMolSim/AtomsSystems.jl)
* `random_species!` - randomize species
* `load_from_pubchem` - download structures from [PubChem](https://pubchem.ncbi.nlm.nih.gov/)


## Example

```julia
using AtomsBuilder
using AtomsBuilder
using Unitful

# generate a diamond cubic bulk Si unit cell
at1 = bulk(:Si)
Expand All @@ -27,18 +33,20 @@ at2 = bulk(:Si, cubic=true)
@show length(at2)

# repeat the cell 3 times in each coordinate direction
at3 = at2 * 3
at3 = repeat(at2, 3)
@show length(at3)

# repeat the unit cell in only one direction
at4 = at2 * (3, 1, 1)
at4 = repeat(at2, (3, 1, 1) )
@show length(at3)

# create a bulk supercell and then rattle the atoms
at5 = rattle!( bulk(:Si, cubic=true) * 3 )
at5 = rattle_positions( repeat(bulk(:Si, cubic=true), 3), 0.3u"Å" )

random_species!(at5, [:Si, :P], Weights([0.9, 0.1]))
```

See `?bulk` and `?rattle!` for more information.
For more information look for docstrings.

## PubChem Interface

Expand All @@ -51,6 +59,9 @@ using AtomsBuilder
# using trivial name
load_from_pubchem( "water" )

# same with additional metadata
load_from_pubchem( "water"; metadata=true )

# using CID
load_from_pubchem( 887 )

Expand Down
16 changes: 15 additions & 1 deletion src/AtomsBuilder.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
module AtomsBuilder

import AtomsBase
using AtomsBase
using AtomsSystems
using LinearAlgebra: norm, I
using Reexport
using StaticArrays: SMatrix, SVector
using StatsBase: sample
import StatsBase
using Unitful

@reexport using AtomsSystems: rattle_positions!, rattle_positions
@reexport using StatsBase: FrequencyWeights, ProbabilityWeights, Weights

export bulk
export rattle!
export randz!
export random_species!

const Mat3{T} = SMatrix{3, 3, T}
const Vec3{T} = SVector{3, T}
Expand Down
9 changes: 6 additions & 3 deletions src/bulk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,16 @@ function bulk(sym::Symbol; cubic=false, pbc=(true, true, true),
cubic && throw(ArgumentError("cubic=true cannot be selected for $symm lattices"))
X, C = _bulk_hcp(sym; a, c, T)
else
throw(ArgumentError("Currently bulk not immplemented for symmetry $symm"))
throw(ArgumentError("Currently bulk not implemented for symmetry $symm"))
end
Z = Chemistry.atomic_number(sym)
#Z = Chemistry.atomic_number(sym)
nat = length(X)
at = _flexible_system( X, fill(Z, nat), C, _convert_pbc(pbc))
# at = _flexible_system( X, fill(Z, nat), C, _convert_pbc(pbc))
spc = AtomsBase.ChemicalSpecies(sym)
at = generic_system( fill(spc, nat), X; cell_vectors=collect(eachrow(C)), periodicity=pbc)
# I don't remember what this does so I'm commenting it out until I understand
# it again ...
# (x !== nothing || y !== nothing || z !== nothing) && rotate!(at, x=x, y=y, z=z)
#return at
return at
end
30 changes: 18 additions & 12 deletions src/pubchem.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
using AtomsBase: FastSystem
using AtomsSystems: generic_system
using AtomsIO: load_system
using PubChemCrawler: get_cid, get_for_cids
using PubChemCrawler: get_cid, get_for_cids
using Unitful

export load_from_pubchem

"""
load_from_pubchem(cid::Int)
load_from_pubchem(name::AbstractString)
load_from_pubchem(;smiles::AbstractString)
load_from_pubchem(cid::Int; metadata=false)
load_from_pubchem(name::AbstractString; metadata=false)
load_from_pubchem(;smiles::AbstractString; metadata=false)

Load a molecule from [PubChem](https://pubchem.ncbi.nlm.nih.gov/).

You can use [PubChemCrawler](https://github.com/JuliaHealth/PubChemCrawler.jl)
to search for `cid`.

If `metadata` is `true` then the returned system will include PubChem metadata.

# Example
```julia
using AtomsBuilder
Expand All @@ -31,23 +34,26 @@ load_from_pubchem( smiles="CC(=O)C" )
load_from_pubchem( "64-17-5" )
```
"""
function load_from_pubchem(cid::Int)
function load_from_pubchem(cid::Int; metadata=false)
tmp_name = tempname() * ".sdf"
open(tmp_name, "w") do io
write(io, get_for_cids(cid, output="SDF", record_type="3d"))
end
# parsing system does not work properly so we drop global features
sys = FastSystem( load_system(tmp_name) )
if metadata
sys = generic_system( load_system(tmp_name) )
else
sys = CellSystem( generic_system( load_system(tmp_name) ) )
end
rm(tmp_name)
return sys
end

function load_from_pubchem(name::AbstractString)
function load_from_pubchem(name::AbstractString; metadata=false)
cid = get_cid(name=name)
return load_from_pubchem(cid)
return load_from_pubchem(cid; metadata=metadata)
end

function load_from_pubchem(;smiles::AbstractString)
function load_from_pubchem(;smiles::AbstractString, metadata=false)
cid = get_cid(smiles=smiles)
return load_from_pubchem(cid)
return load_from_pubchem(cid; metadata=metadata)
end
Loading