├── .github └── workflows │ └── build_and_run.yaml ├── LICENSE ├── README.md ├── example ├── data.BaTiO3 └── in.BaTiO3 └── patch ├── orb_driver.py ├── pair_orb.cpp ├── pair_orb.h └── patch.sh /.github/workflows/build_and_run.yaml: -------------------------------------------------------------------------------- 1 | name: Build and Run LAMMPS with ML-ORB 2 | 3 | on: 4 | workflow_dispatch: 5 | 6 | jobs: 7 | build-lammps: 8 | runs-on: ubuntu-latest 9 | 10 | steps: 11 | - name: Checkout code 12 | uses: actions/checkout@v3 13 | 14 | - name: Install dependencies 15 | run: | 16 | sudo apt-get update 17 | sudo apt-get install -y cmake g++ make python3-venv libjpeg-dev libomp-dev libomp5 18 | 19 | - name: Setup Python virtual environment 20 | run: | 21 | python3 -m venv orb-models 22 | source orb-models/bin/activate 23 | pip install --upgrade pip 24 | pip install orb-models 25 | pip install "pynanoflann@git+https://github.com/dwastberg/pynanoflann#egg=af434039ae14bedcbb838a7808924d6689274168" 26 | pip install ase 27 | 28 | - name: Download LAMMPS source code 29 | run: | 30 | git clone https://github.com/lammps/lammps.git 31 | cd lammps 32 | 33 | - name: Apply ML-ORB patch 34 | run: | 35 | bash patch/patch.sh $(pwd)/lammps 36 | 37 | - name: Build LAMMPS with ML-ORB 38 | run: | 39 | cd lammps 40 | mkdir build 41 | cd build 42 | cmake -C ../cmake/presets/all_off.cmake -D BUILD_MPI=OFF -D BUILD_OMP=ON -D PKG_OMP=ON -D PKG_PYTHON=ON -D PKG_ML-ORB=ON ../cmake 43 | make -j$(nproc) 44 | 45 | - name: Run a simple LAMMPS input script (for testing) 46 | run: | 47 | source orb-models/bin/activate 48 | cd example 49 | cp ../lammps/src/ML-ORB/orb_driver.py . 50 | ../lammps/build/lmp -in in.BaTiO3 51 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Orbital Materials Pretrained Model LAMMPS Wrapper 2 | [![Build and Run LAMMPS with ML-ORB](https://github.com/stefanbringuier/ORB-LAMMPS-PATCH/actions/workflows/build_and_run.yaml/badge.svg)](https://github.com/stefanbringuier/ORB-LAMMPS-PATCH/actions/workflows/build_and_run.yaml) 3 | 4 | > 🚩 4 Apr. 2025: The [Orbital Materials models](https://github.com/orbital-materials/orb-models) along with other GNNs have been implmented in , therefore this repo is being archived. 🚩 5 | > 6 | This patch is inspired by the approach used in the [AdvancedSoftCorp](https://github.com/advancesoftcorp/lammps) [M3GNet](https://github.com/advancesoftcorp/lammps/tree/based-on-lammps_2Aug2023/src/ML-M3GNET) implementation, but it applies to the [Orbital Materials pretrained models](https://github.com/orbital-materials/orb-models). Essentially, this is a C++ wrapper that calls the Python implementation for the [Orbital Materials pretrained atomic potentials](https://github.com/orbital-materials/orb-models). As a result, a Python driver script must be invoked by LAMMPS, which needs to be compiled with Python support, and the Orbital Materials pretrained potential package must be installed in the Python environment. 7 | 8 | 9 | ## Why 10 | There might not be too much upside in performance (i.e., runs as fast as ASE MD) as this approach is just a wrapper and does not leverage the full parallelization aspects of LAMMPS (i.e., MPI). However, there may be some added functionality in the available `fix` and `compute` commands that are availabe in LAMMPS which are not typically implemented in [ASE](https://wiki.fysik.dtu.dk/ase). For this reason there will be scenarios where it will be useful to test and benchmark the Orbital Materials pretrained models similar to how some are doing for M3GNet or CHGNet. 11 | 12 | ## How to install 13 | 1. Setup a python virtual environment and activate: 14 | - `python -m venv orb-models` 15 | - `source orb-models/bin/activate` 16 | 2. Install the [Orbital Materials pretrained atomic potentials](https://github.com/orbital-materials/orb-models) 17 | 3. Download [LAMMPS](https://lammps.org). 18 | 4. Clone this repo. 19 | 5. Run the `patch.sh` script and provide path to the cloned LAMMPS folder. 20 | 6. Compile LAMMPS with at minimum (you can add more) the following: `cmake -D BUILD_MPI=OFF -D BUILD_OMP=ON -D PKG_OMP=ON -D PKG_PYTHON=ON -D PKG_ML-ORB=ON ../cmake` 21 | > NOTE: You can compile with MPI but it will not work for this `pair_style`, in other words you can only use a single MPI task. 22 | 7. Copy the [orb_driver.py](patch/orb_driver.py) script to the folder with your LAMMPS input script or provide the full path to it in your input script. 23 | 24 | ## How to use 25 | 26 | In your LAMMPS script you need to use the following syntax: 27 | 28 | ``` 29 | pair_style orb 30 | pair_coeff * * orb-v1 ... 31 | ``` 32 | 33 | The species should be the atomic symbol and ordered such that they follow LAMMPS type id sequence. If you provide the keyword `gpu` it should try to run on the GPU if one is available, otherwise the inference will be done on the CPU. The path to the `orb_driver.py` file is needed. You can copy this from either the [patch](patch) or the [example](example) folders. You do not need to modify `orb_driver.py` unless you are need custimization or finding the GPU flag isn't being passed properly. 34 | > I was seeing the GPU utilized when using the `gpu` flag, but then when I ran on another system I had to manually edit the `orb_driver.py` to just use the `gpu`. 35 | 36 | 37 | ### Model options 38 | 39 | - `orb-v1` - trained on [MPTraj](https://figshare.com/articles/dataset/Materials_Project_Trjectory_MPtrj_Dataset/23713842?file=41619375) + [Alexandria](https://alexandria.icams.rub.de/). 40 | - `orb-mptraj-only-v1` - trained on the MPTraj dataset only to reproduce our second Matbench Discovery result. We do not recommend using this model for general use. 41 | - `orb-d3-v1` - trained on MPTraj + Alexandria with integrated D3 corrections. In general, we recommend using this model, particularly for systems where dispersion interactions are important. This model was trained to predict D3-corrected targets and hence is the same speed as `orb-v1`. Incorporating D3 into the model like this is substantially faster than using analytical D3 corrections. 42 | - `orb-d3-sm-v1` or `orb-d3-sm-v1` - Smaller versions of `orb-d3-v1`. The `sm` model has 10 layers, whilst the `xs` model has 5 layers. 43 | 44 | ## Contributing 45 | Feel free to create a pull request if you find some bugs or want to suggest improvements. One potential change is to move away from a python driver script and just have C++ code that instantiates and calls the python objects and methods that are in the driver script. 46 | 47 | ## Acknowledgements 48 | Thanks to the team at [AdvancedSoftCorp](https://www.advancesoft.jp/) for providing a framework for leveraging the [ASE Calculators Class](https://wiki.fysik.dtu.dk/ase/ase/calculators/calculators.html#calculators). Also appreciate the team at Orbitals Materials making their pretrained models available for testing. 49 | -------------------------------------------------------------------------------- /example/data.BaTiO3: -------------------------------------------------------------------------------- 1 | # LAMMPS data file written by OVITO Basic 3.10.3 2 | 3 | 320 atoms 4 | 3 atom types 5 | 6 | 0.0 16.04 xlo xhi 7 | 0.0 16.04 ylo yhi 8 | 0.0 16.04 zlo zhi 9 | 10 | Atom Type Labels 11 | 12 | 1 Ti 13 | 2 Ba 14 | 3 O 15 | 16 | Masses 17 | 18 | 1 47.867 # Ti 19 | 2 137.327 # Ba 20 | 3 15.9994 # O 21 | 22 | Atoms # atomic 23 | 24 | 1 1 0.0 0.0 0.0 25 | 2 2 2.005 2.005 2.005 26 | 3 3 2.005 0.0 0.0 27 | 4 3 0.0 2.005 0.0 28 | 5 3 0.0 0.0 2.005 29 | 6 1 0.0 0.0 4.01 30 | 7 2 2.005 2.005 6.015 31 | 8 3 2.005 0.0 4.01 32 | 9 3 0.0 2.005 4.01 33 | 10 3 0.0 0.0 6.015 34 | 11 1 0.0 0.0 8.02 35 | 12 2 2.005 2.005 10.025 36 | 13 3 2.005 0.0 8.02 37 | 14 3 0.0 2.005 8.02 38 | 15 3 0.0 0.0 10.025 39 | 16 1 0.0 0.0 12.03 40 | 17 2 2.005 2.005 14.035 41 | 18 3 2.005 0.0 12.03 42 | 19 3 0.0 2.005 12.03 43 | 20 3 0.0 0.0 14.035 44 | 21 1 0.0 4.01 0.0 45 | 22 2 2.005 6.015 2.005 46 | 23 3 2.005 4.01 0.0 47 | 24 3 0.0 6.015 0.0 48 | 25 3 0.0 4.01 2.005 49 | 26 1 0.0 4.01 4.01 50 | 27 2 2.005 6.015 6.015 51 | 28 3 2.005 4.01 4.01 52 | 29 3 0.0 6.015 4.01 53 | 30 3 0.0 4.01 6.015 54 | 31 1 0.0 4.01 8.02 55 | 32 2 2.005 6.015 10.025 56 | 33 3 2.005 4.01 8.02 57 | 34 3 0.0 6.015 8.02 58 | 35 3 0.0 4.01 10.025 59 | 36 1 0.0 4.01 12.03 60 | 37 2 2.005 6.015 14.035 61 | 38 3 2.005 4.01 12.03 62 | 39 3 0.0 6.015 12.03 63 | 40 3 0.0 4.01 14.035 64 | 41 1 0.0 8.02 0.0 65 | 42 2 2.005 10.025 2.005 66 | 43 3 2.005 8.02 0.0 67 | 44 3 0.0 10.025 0.0 68 | 45 3 0.0 8.02 2.005 69 | 46 1 0.0 8.02 4.01 70 | 47 2 2.005 10.025 6.015 71 | 48 3 2.005 8.02 4.01 72 | 49 3 0.0 10.025 4.01 73 | 50 3 0.0 8.02 6.015 74 | 51 1 0.0 8.02 8.02 75 | 52 2 2.005 10.025 10.025 76 | 53 3 2.005 8.02 8.02 77 | 54 3 0.0 10.025 8.02 78 | 55 3 0.0 8.02 10.025 79 | 56 1 0.0 8.02 12.03 80 | 57 2 2.005 10.025 14.035 81 | 58 3 2.005 8.02 12.03 82 | 59 3 0.0 10.025 12.03 83 | 60 3 0.0 8.02 14.035 84 | 61 1 0.0 12.03 0.0 85 | 62 2 2.005 14.035 2.005 86 | 63 3 2.005 12.03 0.0 87 | 64 3 0.0 14.035 0.0 88 | 65 3 0.0 12.03 2.005 89 | 66 1 0.0 12.03 4.01 90 | 67 2 2.005 14.035 6.015 91 | 68 3 2.005 12.03 4.01 92 | 69 3 0.0 14.035 4.01 93 | 70 3 0.0 12.03 6.015 94 | 71 1 0.0 12.03 8.02 95 | 72 2 2.005 14.035 10.025 96 | 73 3 2.005 12.03 8.02 97 | 74 3 0.0 14.035 8.02 98 | 75 3 0.0 12.03 10.025 99 | 76 1 0.0 12.03 12.03 100 | 77 2 2.005 14.035 14.035 101 | 78 3 2.005 12.03 12.03 102 | 79 3 0.0 14.035 12.03 103 | 80 3 0.0 12.03 14.035 104 | 81 1 4.01 0.0 0.0 105 | 82 2 6.015 2.005 2.005 106 | 83 3 6.015 0.0 0.0 107 | 84 3 4.01 2.005 0.0 108 | 85 3 4.01 0.0 2.005 109 | 86 1 4.01 0.0 4.01 110 | 87 2 6.015 2.005 6.015 111 | 88 3 6.015 0.0 4.01 112 | 89 3 4.01 2.005 4.01 113 | 90 3 4.01 0.0 6.015 114 | 91 1 4.01 0.0 8.02 115 | 92 2 6.015 2.005 10.025 116 | 93 3 6.015 0.0 8.02 117 | 94 3 4.01 2.005 8.02 118 | 95 3 4.01 0.0 10.025 119 | 96 1 4.01 0.0 12.03 120 | 97 2 6.015 2.005 14.035 121 | 98 3 6.015 0.0 12.03 122 | 99 3 4.01 2.005 12.03 123 | 100 3 4.01 0.0 14.035 124 | 101 1 4.01 4.01 0.0 125 | 102 2 6.015 6.015 2.005 126 | 103 3 6.015 4.01 0.0 127 | 104 3 4.01 6.015 0.0 128 | 105 3 4.01 4.01 2.005 129 | 106 1 4.01 4.01 4.01 130 | 107 2 6.015 6.015 6.015 131 | 108 3 6.015 4.01 4.01 132 | 109 3 4.01 6.015 4.01 133 | 110 3 4.01 4.01 6.015 134 | 111 1 4.01 4.01 8.02 135 | 112 2 6.015 6.015 10.025 136 | 113 3 6.015 4.01 8.02 137 | 114 3 4.01 6.015 8.02 138 | 115 3 4.01 4.01 10.025 139 | 116 1 4.01 4.01 12.03 140 | 117 2 6.015 6.015 14.035 141 | 118 3 6.015 4.01 12.03 142 | 119 3 4.01 6.015 12.03 143 | 120 3 4.01 4.01 14.035 144 | 121 1 4.01 8.02 0.0 145 | 122 2 6.015 10.025 2.005 146 | 123 3 6.015 8.02 0.0 147 | 124 3 4.01 10.025 0.0 148 | 125 3 4.01 8.02 2.005 149 | 126 1 4.01 8.02 4.01 150 | 127 2 6.015 10.025 6.015 151 | 128 3 6.015 8.02 4.01 152 | 129 3 4.01 10.025 4.01 153 | 130 3 4.01 8.02 6.015 154 | 131 1 4.01 8.02 8.02 155 | 132 2 6.015 10.025 10.025 156 | 133 3 6.015 8.02 8.02 157 | 134 3 4.01 10.025 8.02 158 | 135 3 4.01 8.02 10.025 159 | 136 1 4.01 8.02 12.03 160 | 137 2 6.015 10.025 14.035 161 | 138 3 6.015 8.02 12.03 162 | 139 3 4.01 10.025 12.03 163 | 140 3 4.01 8.02 14.035 164 | 141 1 4.01 12.03 0.0 165 | 142 2 6.015 14.035 2.005 166 | 143 3 6.015 12.03 0.0 167 | 144 3 4.01 14.035 0.0 168 | 145 3 4.01 12.03 2.005 169 | 146 1 4.01 12.03 4.01 170 | 147 2 6.015 14.035 6.015 171 | 148 3 6.015 12.03 4.01 172 | 149 3 4.01 14.035 4.01 173 | 150 3 4.01 12.03 6.015 174 | 151 1 4.01 12.03 8.02 175 | 152 2 6.015 14.035 10.025 176 | 153 3 6.015 12.03 8.02 177 | 154 3 4.01 14.035 8.02 178 | 155 3 4.01 12.03 10.025 179 | 156 1 4.01 12.03 12.03 180 | 157 2 6.015 14.035 14.035 181 | 158 3 6.015 12.03 12.03 182 | 159 3 4.01 14.035 12.03 183 | 160 3 4.01 12.03 14.035 184 | 161 1 8.02 0.0 0.0 185 | 162 2 10.025 2.005 2.005 186 | 163 3 10.025 0.0 0.0 187 | 164 3 8.02 2.005 0.0 188 | 165 3 8.02 0.0 2.005 189 | 166 1 8.02 0.0 4.01 190 | 167 2 10.025 2.005 6.015 191 | 168 3 10.025 0.0 4.01 192 | 169 3 8.02 2.005 4.01 193 | 170 3 8.02 0.0 6.015 194 | 171 1 8.02 0.0 8.02 195 | 172 2 10.025 2.005 10.025 196 | 173 3 10.025 0.0 8.02 197 | 174 3 8.02 2.005 8.02 198 | 175 3 8.02 0.0 10.025 199 | 176 1 8.02 0.0 12.03 200 | 177 2 10.025 2.005 14.035 201 | 178 3 10.025 0.0 12.03 202 | 179 3 8.02 2.005 12.03 203 | 180 3 8.02 0.0 14.035 204 | 181 1 8.02 4.01 0.0 205 | 182 2 10.025 6.015 2.005 206 | 183 3 10.025 4.01 0.0 207 | 184 3 8.02 6.015 0.0 208 | 185 3 8.02 4.01 2.005 209 | 186 1 8.02 4.01 4.01 210 | 187 2 10.025 6.015 6.015 211 | 188 3 10.025 4.01 4.01 212 | 189 3 8.02 6.015 4.01 213 | 190 3 8.02 4.01 6.015 214 | 191 1 8.02 4.01 8.02 215 | 192 2 10.025 6.015 10.025 216 | 193 3 10.025 4.01 8.02 217 | 194 3 8.02 6.015 8.02 218 | 195 3 8.02 4.01 10.025 219 | 196 1 8.02 4.01 12.03 220 | 197 2 10.025 6.015 14.035 221 | 198 3 10.025 4.01 12.03 222 | 199 3 8.02 6.015 12.03 223 | 200 3 8.02 4.01 14.035 224 | 201 1 8.02 8.02 0.0 225 | 202 2 10.025 10.025 2.005 226 | 203 3 10.025 8.02 0.0 227 | 204 3 8.02 10.025 0.0 228 | 205 3 8.02 8.02 2.005 229 | 206 1 8.02 8.02 4.01 230 | 207 2 10.025 10.025 6.015 231 | 208 3 10.025 8.02 4.01 232 | 209 3 8.02 10.025 4.01 233 | 210 3 8.02 8.02 6.015 234 | 211 1 8.02 8.02 8.02 235 | 212 2 10.025 10.025 10.025 236 | 213 3 10.025 8.02 8.02 237 | 214 3 8.02 10.025 8.02 238 | 215 3 8.02 8.02 10.025 239 | 216 1 8.02 8.02 12.03 240 | 217 2 10.025 10.025 14.035 241 | 218 3 10.025 8.02 12.03 242 | 219 3 8.02 10.025 12.03 243 | 220 3 8.02 8.02 14.035 244 | 221 1 8.02 12.03 0.0 245 | 222 2 10.025 14.035 2.005 246 | 223 3 10.025 12.03 0.0 247 | 224 3 8.02 14.035 0.0 248 | 225 3 8.02 12.03 2.005 249 | 226 1 8.02 12.03 4.01 250 | 227 2 10.025 14.035 6.015 251 | 228 3 10.025 12.03 4.01 252 | 229 3 8.02 14.035 4.01 253 | 230 3 8.02 12.03 6.015 254 | 231 1 8.02 12.03 8.02 255 | 232 2 10.025 14.035 10.025 256 | 233 3 10.025 12.03 8.02 257 | 234 3 8.02 14.035 8.02 258 | 235 3 8.02 12.03 10.025 259 | 236 1 8.02 12.03 12.03 260 | 237 2 10.025 14.035 14.035 261 | 238 3 10.025 12.03 12.03 262 | 239 3 8.02 14.035 12.03 263 | 240 3 8.02 12.03 14.035 264 | 241 1 12.03 0.0 0.0 265 | 242 2 14.035 2.005 2.005 266 | 243 3 14.035 0.0 0.0 267 | 244 3 12.03 2.005 0.0 268 | 245 3 12.03 0.0 2.005 269 | 246 1 12.03 0.0 4.01 270 | 247 2 14.035 2.005 6.015 271 | 248 3 14.035 0.0 4.01 272 | 249 3 12.03 2.005 4.01 273 | 250 3 12.03 0.0 6.015 274 | 251 1 12.03 0.0 8.02 275 | 252 2 14.035 2.005 10.025 276 | 253 3 14.035 0.0 8.02 277 | 254 3 12.03 2.005 8.02 278 | 255 3 12.03 0.0 10.025 279 | 256 1 12.03 0.0 12.03 280 | 257 2 14.035 2.005 14.035 281 | 258 3 14.035 0.0 12.03 282 | 259 3 12.03 2.005 12.03 283 | 260 3 12.03 0.0 14.035 284 | 261 1 12.03 4.01 0.0 285 | 262 2 14.035 6.015 2.005 286 | 263 3 14.035 4.01 0.0 287 | 264 3 12.03 6.015 0.0 288 | 265 3 12.03 4.01 2.005 289 | 266 1 12.03 4.01 4.01 290 | 267 2 14.035 6.015 6.015 291 | 268 3 14.035 4.01 4.01 292 | 269 3 12.03 6.015 4.01 293 | 270 3 12.03 4.01 6.015 294 | 271 1 12.03 4.01 8.02 295 | 272 2 14.035 6.015 10.025 296 | 273 3 14.035 4.01 8.02 297 | 274 3 12.03 6.015 8.02 298 | 275 3 12.03 4.01 10.025 299 | 276 1 12.03 4.01 12.03 300 | 277 2 14.035 6.015 14.035 301 | 278 3 14.035 4.01 12.03 302 | 279 3 12.03 6.015 12.03 303 | 280 3 12.03 4.01 14.035 304 | 281 1 12.03 8.02 0.0 305 | 282 2 14.035 10.025 2.005 306 | 283 3 14.035 8.02 0.0 307 | 284 3 12.03 10.025 0.0 308 | 285 3 12.03 8.02 2.005 309 | 286 1 12.03 8.02 4.01 310 | 287 2 14.035 10.025 6.015 311 | 288 3 14.035 8.02 4.01 312 | 289 3 12.03 10.025 4.01 313 | 290 3 12.03 8.02 6.015 314 | 291 1 12.03 8.02 8.02 315 | 292 2 14.035 10.025 10.025 316 | 293 3 14.035 8.02 8.02 317 | 294 3 12.03 10.025 8.02 318 | 295 3 12.03 8.02 10.025 319 | 296 1 12.03 8.02 12.03 320 | 297 2 14.035 10.025 14.035 321 | 298 3 14.035 8.02 12.03 322 | 299 3 12.03 10.025 12.03 323 | 300 3 12.03 8.02 14.035 324 | 301 1 12.03 12.03 0.0 325 | 302 2 14.035 14.035 2.005 326 | 303 3 14.035 12.03 0.0 327 | 304 3 12.03 14.035 0.0 328 | 305 3 12.03 12.03 2.005 329 | 306 1 12.03 12.03 4.01 330 | 307 2 14.035 14.035 6.015 331 | 308 3 14.035 12.03 4.01 332 | 309 3 12.03 14.035 4.01 333 | 310 3 12.03 12.03 6.015 334 | 311 1 12.03 12.03 8.02 335 | 312 2 14.035 14.035 10.025 336 | 313 3 14.035 12.03 8.02 337 | 314 3 12.03 14.035 8.02 338 | 315 3 12.03 12.03 10.025 339 | 316 1 12.03 12.03 12.03 340 | 317 2 14.035 14.035 14.035 341 | 318 3 14.035 12.03 12.03 342 | 319 3 12.03 14.035 12.03 343 | 320 3 12.03 12.03 14.035 344 | -------------------------------------------------------------------------------- /example/in.BaTiO3: -------------------------------------------------------------------------------- 1 | # Test of ML-ORB potential for BaTiO3 system 2 | 3 | units metal 4 | boundary p p p 5 | atom_style atomic 6 | 7 | read_data data.BaTiO3 8 | 9 | pair_style orb ./orb_driver.py 10 | pair_coeff * * orb-v2 Ti Ba O 11 | 12 | neighbor 0.3 bin 13 | neigh_modify delay 10 14 | 15 | thermo 10 16 | timestep 0.001 17 | velocity all create 300.0 4928459 rot yes dist gaussian 18 | fix 1 all nvt temp 300.0 300.0 1.0 19 | 20 | dump 1 all atom 10 dump.BaTiO3-ML-ORB 21 | run 500 22 | -------------------------------------------------------------------------------- /patch/orb_driver.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (c) 2023, AdvanceSoft Corp. 3 | Copyright (c) 2024, Stefan Bringuier 4 | 5 | This source code is licensed under the GNU General Public License Version 2 6 | found in the LICENSE file in the root directory of this source tree. 7 | """ 8 | 9 | from ase import Atoms 10 | from orb_models.forcefield import pretrained 11 | from orb_models.forcefield.calculator import ORBCalculator 12 | 13 | 14 | import torch 15 | 16 | def orb_initialize(model_name = None, gpu = False, cutoff=4.0): 17 | """ 18 | Initialize Orb models: https://github.com/orbital-materials/orb-models 19 | Args: 20 | model_name (str): name of model for GNNP. 21 | gpu (bool): using GPU, if possible. 22 | Returns: 23 | cutoff: cutoff radius. 24 | 25 | Note: 26 | The cutoff from ORBCalculator is not available. 27 | """ 28 | 29 | # Get Orb model 30 | if model_name is not None: 31 | orbff = pretrained.ORB_PRETRAINED_MODELS[model_name]() 32 | else: 33 | orbff = pretrained.orb_v2() 34 | 35 | # Assign Calculator and Device Type 36 | global calculator 37 | if gpu and torch.cuda.is_available(): 38 | calculator = ORBCalculator(orbff,device="cuda") 39 | else: 40 | calculator = ORBCalculator(orbff,device="cpu") 41 | 42 | global aseAtoms 43 | 44 | aseAtoms = None 45 | 46 | # Orb Model isn't providing cutoff 47 | #cutoff = calculator.model.cutoff 48 | return cutoff 49 | 50 | def orb_get_energy_forces_stress(cell, atomic_numbers, positions): 51 | """ 52 | Predict total energy, atomic forces and stress w/ pre-trained Orb Model 53 | Args: 54 | cell: lattice vectors in angstroms. 55 | atomic_numbers: atomic numbers for all atoms. 56 | positions: xyz coordinates for all atoms in angstroms. 57 | Returns: 58 | energy: total energy. 59 | forcces: atomic forces. 60 | stress: stress tensor (Voigt order). 61 | 62 | NOTES: 63 | - Assumes PBC is always True. This should eventually be changed so that this function takes LAMMPS cell and BC. 64 | """ 65 | 66 | # Initialize Atoms 67 | global aseAtoms 68 | global calculator 69 | 70 | if aseAtoms is not None and len(aseAtoms.numbers) != len(atomic_numbers): 71 | aseAtoms = None 72 | 73 | if aseAtoms is None: 74 | aseAtoms = Atoms( 75 | numbers = atomic_numbers, 76 | positions = positions, 77 | cell = cell, 78 | pbc = [True, True, True] 79 | ) 80 | 81 | aseAtoms.calc = calculator 82 | 83 | else: 84 | aseAtoms.set_cell(cell) 85 | aseAtoms.set_atomic_numbers(atomic_numbers) 86 | aseAtoms.set_positions(positions) 87 | 88 | energy = aseAtoms.get_potential_energy() 89 | if not isinstance(energy, float): 90 | energy = energy.item() 91 | 92 | forces = aseAtoms.get_forces().tolist() 93 | 94 | stress = aseAtoms.get_stress().tolist() 95 | 96 | return energy, forces, stress 97 | -------------------------------------------------------------------------------- /patch/pair_orb.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 AdvanceSoft Corporation 3 | * Copyright (C) 2024 Stefan Bringuier 4 | 5 | * This source code is licensed under the GNU General Public License Version 2 6 | * found in the LICENSE file in the root directory of this source tree. 7 | */ 8 | 9 | #include "pair_orb.h" 10 | 11 | using namespace LAMMPS_NS; 12 | 13 | #define GPA_TO_EVA3 160.21766208 14 | 15 | PairOrb::PairOrb(LAMMPS *lmp) : Pair(lmp) 16 | { 17 | single_enable = 0; 18 | restartinfo = 0; 19 | one_coeff = 1; 20 | manybody_flag = 1; 21 | no_virial_fdotr_compute = 1; 22 | centroidstressflag = CENTROID_NOTAVAIL; 23 | 24 | this->atomNumMap = nullptr; 25 | this->maxinum = 10; 26 | this->initializedPython = 0; 27 | this->cutoff = 0.0; 28 | this->npythonPath = 0; 29 | this->pythonPaths = nullptr; 30 | this->pyModule = nullptr; 31 | this->pyFunc = nullptr; 32 | 33 | if (!PairOrb::finalized) 34 | { 35 | PairOrb::finalized = 1; 36 | std::atexit(PairOrb::finalize); 37 | } 38 | } 39 | 40 | PairOrb::~PairOrb() 41 | { 42 | if (copymode) 43 | { 44 | return; 45 | } 46 | 47 | if (this->atomNumMap != nullptr) 48 | { 49 | delete[] this->atomNumMap; 50 | } 51 | 52 | if (allocated) 53 | { 54 | memory->destroy(cutsq); 55 | memory->destroy(setflag); 56 | memory->destroy(this->cell); 57 | memory->destroy(this->atomNums); 58 | memory->destroy(this->positions); 59 | memory->destroy(this->forces); 60 | memory->destroy(this->stress); 61 | } 62 | 63 | if (this->pythonPaths != nullptr) 64 | { 65 | for (int i = 0; i < this->npythonPath; ++i) 66 | { 67 | delete[] this->pythonPaths[i]; 68 | } 69 | 70 | delete[] this->pythonPaths; 71 | } 72 | 73 | if (this->initializedPython) 74 | { 75 | this->finalizePython(); 76 | } 77 | } 78 | 79 | int PairOrb::finalized = 0; 80 | 81 | void PairOrb::finalize() 82 | { 83 | if(Py_IsInitialized()) Py_Finalize(); 84 | } 85 | 86 | void PairOrb::allocate() 87 | { 88 | allocated = 1; 89 | 90 | const int ntypes = atom->ntypes; 91 | 92 | memory->create(cutsq, ntypes + 1, ntypes + 1, "pair:cutsq"); 93 | memory->create(setflag, ntypes + 1, ntypes + 1, "pair:setflag"); 94 | 95 | memory->create(this->cell, 3, 3, "pair:cell"); 96 | memory->create(this->atomNums, this->maxinum, "pair:atomNums"); 97 | memory->create(this->positions, this->maxinum, 3, "pair:positions"); 98 | memory->create(this->forces, this->maxinum, 3, "pair:forces"); 99 | memory->create(this->stress, 6, "pair:stress"); 100 | } 101 | 102 | void PairOrb::compute(int eflag, int vflag) 103 | { 104 | ev_init(eflag, vflag); 105 | 106 | if (eflag_atom) 107 | { 108 | error->all(FLERR, "Pair style Orb Model does not support atomic energy"); 109 | } 110 | 111 | if (vflag_atom) 112 | { 113 | error->all(FLERR, "Pair style Orb Model does not support atomic virial pressure"); 114 | } 115 | 116 | this->prepareGNN(); 117 | 118 | this->performGNN(); 119 | } 120 | 121 | void PairOrb::prepareGNN() 122 | { 123 | int i; 124 | int iatom; 125 | 126 | int* type = atom->type; 127 | double** x = atom->x; 128 | 129 | int inum = list->inum; 130 | int* ilist = list->ilist; 131 | 132 | double* boxlo = domain->boxlo; 133 | 134 | // grow with inum and nneigh 135 | if (inum > this->maxinum) 136 | { 137 | this->maxinum = inum + this->maxinum / 2; 138 | 139 | memory->grow(this->atomNums, this->maxinum, "pair:atomNums"); 140 | memory->grow(this->positions, this->maxinum, 3, "pair:positions"); 141 | memory->grow(this->forces, this->maxinum, 3, "pair:forces"); 142 | } 143 | 144 | // set cell 145 | this->cell[0][0] = domain->h[0]; // xx 146 | this->cell[1][1] = domain->h[1]; // yy 147 | this->cell[2][2] = domain->h[2]; // zz 148 | this->cell[2][1] = domain->h[3]; // yz 149 | this->cell[2][0] = domain->h[4]; // xz 150 | this->cell[1][0] = domain->h[5]; // xy 151 | this->cell[0][1] = 0.0; 152 | this->cell[0][2] = 0.0; 153 | this->cell[1][2] = 0.0; 154 | 155 | // set atomNums and positions 156 | #pragma omp parallel for private(iatom, i) 157 | for (iatom = 0; iatom < inum; ++iatom) 158 | { 159 | i = ilist[iatom]; 160 | 161 | this->atomNums[iatom] = this->atomNumMap[type[i]]; 162 | 163 | this->positions[iatom][0] = x[i][0] - boxlo[0]; 164 | this->positions[iatom][1] = x[i][1] - boxlo[1]; 165 | this->positions[iatom][2] = x[i][2] - boxlo[2]; 166 | } 167 | } 168 | 169 | void PairOrb::performGNN() 170 | { 171 | int i; 172 | int iatom; 173 | 174 | double** f = atom->f; 175 | 176 | int inum = list->inum; 177 | int* ilist = list->ilist; 178 | 179 | double volume; 180 | double factor; 181 | double evdwl = 0.0; 182 | 183 | // perform Graph Neural Network Potential of Orb Model 184 | evdwl = this->calculatePython(); 185 | 186 | // set total energy 187 | if (eflag_global) 188 | { 189 | eng_vdwl += evdwl; 190 | } 191 | 192 | // set atomic forces 193 | for (iatom = 0; iatom < inum; ++iatom) 194 | { 195 | i = ilist[iatom]; 196 | 197 | f[i][0] += this->forces[iatom][0]; 198 | f[i][1] += this->forces[iatom][1]; 199 | f[i][2] += this->forces[iatom][2]; 200 | } 201 | 202 | // set virial pressure 203 | if (vflag_global) 204 | { 205 | // GPa -> eV/A^3 206 | volume = domain->xprd * domain->yprd * domain->zprd; 207 | factor = volume / GPA_TO_EVA3; 208 | 209 | virial[0] -= factor * this->stress[0]; // xx 210 | virial[1] -= factor * this->stress[1]; // yy 211 | virial[2] -= factor * this->stress[2]; // zz 212 | virial[3] -= factor * this->stress[3]; // yz 213 | virial[4] -= factor * this->stress[4]; // xz 214 | virial[5] -= factor * this->stress[5]; // xy 215 | } 216 | } 217 | 218 | void PairOrb::settings(int narg, char **arg) 219 | { 220 | if (comm->nprocs > 1) 221 | { 222 | error->all(FLERR, "Pair style Orb Model does not support MPI parallelization"); 223 | } 224 | 225 | if (narg < 1) 226 | { 227 | return; 228 | } 229 | 230 | this->npythonPath = narg; 231 | this->pythonPaths = new char*[this->npythonPath]; 232 | 233 | for (int i = 0; i < this->npythonPath; ++i) 234 | { 235 | this->pythonPaths[i] = new char[512]; 236 | strcpy(this->pythonPaths[i], arg[i]); 237 | } 238 | } 239 | 240 | void PairOrb::coeff(int narg, char **arg) 241 | { 242 | int i, j; 243 | int count; 244 | 245 | int ntypes = atom->ntypes; 246 | int ntypesEff; 247 | 248 | int gpu = withGPU(); 249 | 250 | if (narg != (3 + ntypes)) 251 | { 252 | error->all(FLERR, "Incorrect number of arguments for pair_coeff."); 253 | } 254 | 255 | if (strcmp(arg[0], "*") != 0 || strcmp(arg[1], "*") != 0) 256 | { 257 | error->all(FLERR, "Only wildcard asterisk is allowed in place of atom types for pair_coeff."); 258 | } 259 | 260 | if (this->atomNumMap != nullptr) 261 | { 262 | delete this->atomNumMap; 263 | } 264 | 265 | this->atomNumMap = new int[ntypes + 1]; 266 | 267 | ntypesEff = 0; 268 | for (i = 0; i < ntypes; ++i) 269 | { 270 | if (strcmp(arg[i + 3], "NULL") == 0) 271 | { 272 | this->atomNumMap[i + 1] = 0; 273 | } 274 | else 275 | { 276 | this->atomNumMap[i + 1] = this->elementToAtomNum(arg[i + 3]); 277 | ntypesEff++; 278 | } 279 | } 280 | 281 | if (ntypesEff < 1) 282 | { 283 | error->all(FLERR, "There are no elements for pair_coeff of Orb Model."); 284 | } 285 | 286 | if (!allocated) 287 | { 288 | allocate(); 289 | } 290 | 291 | if (this->initializedPython) 292 | { 293 | this->finalizePython(); 294 | } 295 | 296 | this->cutoff = this->initializePython(arg[2], gpu); 297 | 298 | if (this->cutoff <= 0.0) 299 | { 300 | error->all(FLERR, "Cutoff is not positive for pair_coeff of Orb Model."); 301 | } 302 | 303 | count = 0; 304 | 305 | for (i = 1; i <= ntypes; ++i) 306 | { 307 | for (j = i; j <= ntypes; ++j) 308 | { 309 | if (this->atomNumMap[i] > 0 && this->atomNumMap[j] > 0) 310 | { 311 | setflag[i][j] = 1; 312 | count++; 313 | } 314 | else 315 | { 316 | setflag[i][j] = 0; 317 | } 318 | } 319 | } 320 | 321 | if (count == 0) 322 | { 323 | error->all(FLERR, "Incorrect args for pair coefficients"); 324 | } 325 | } 326 | 327 | double PairOrb::init_one(int i, int j) 328 | { 329 | if (setflag[i][j] == 0) 330 | { 331 | error->all(FLERR, "All pair coeffs are not set"); 332 | } 333 | 334 | double r, rr; 335 | 336 | r = this->cutoff; 337 | rr = r * r; 338 | 339 | cutsq[i][j] = rr; 340 | cutsq[j][i] = rr; 341 | 342 | return r; 343 | } 344 | 345 | void PairOrb::init_style() 346 | { 347 | if (strcmp(update->unit_style, "metal") != 0) 348 | { 349 | error->all(FLERR, "Pair style Orb Model requires 'units metal'"); 350 | } 351 | 352 | int* periodicity = domain->periodicity; 353 | 354 | if (!(periodicity[0] && periodicity[1] && periodicity[2])) 355 | { 356 | error->all(FLERR, "Pair style Orb Model requires periodic boundary condition"); 357 | } 358 | 359 | neighbor->add_request(this, NeighConst::REQ_FULL); 360 | } 361 | 362 | int PairOrb::withGPU() 363 | { 364 | return 0; 365 | } 366 | 367 | void PairOrb::finalizePython() 368 | { 369 | if (this->initializedPython == 0) 370 | { 371 | return; 372 | } 373 | 374 | this->initializedPython = 0; 375 | 376 | Py_XDECREF(this->pyFunc); 377 | Py_XDECREF(this->pyModule); 378 | 379 | // Py_Finalize() does not work well, because of the following bug: 380 | // https://python.readthedocs.io/fr/latest/c-api/init.html#c.Py_FinalizeEx 381 | //if(Py_IsInitialized()) Py_Finalize(); 382 | } 383 | 384 | double PairOrb::initializePython(const char *name, int gpu) 385 | { 386 | if (this->initializedPython != 0) 387 | { 388 | return this->cutoff; 389 | } 390 | 391 | 392 | double cutoff = -1.0; 393 | 394 | PyObject* pySys = nullptr; 395 | PyObject* pyPath = nullptr; 396 | PyObject* pyName = nullptr; 397 | PyObject* pyModule = nullptr; 398 | PyObject* pyFunc = nullptr; 399 | PyObject* pyArgs = nullptr; 400 | PyObject* pyArg1 = nullptr; 401 | PyObject* pyArg2 = nullptr; 402 | PyObject* pyArg3 = nullptr; 403 | PyObject* pyValue = nullptr; 404 | 405 | if (!Py_IsInitialized()) Py_Initialize(); 406 | 407 | pySys = PyImport_ImportModule("sys"); 408 | pyPath = PyObject_GetAttrString(pySys, "path"); 409 | 410 | pyName = PyUnicode_DecodeFSDefault("."); 411 | if (pyName != nullptr) 412 | { 413 | PyList_Append(pyPath, pyName); 414 | Py_DECREF(pyName); 415 | } 416 | 417 | if (this->pythonPaths != nullptr) 418 | { 419 | for (int i = 0; i < this->npythonPath; ++i) 420 | { 421 | pyName = PyUnicode_DecodeFSDefault(this->pythonPaths[i]); 422 | if (pyName != nullptr) 423 | { 424 | PyList_Append(pyPath, pyName); 425 | Py_DECREF(pyName); 426 | } 427 | } 428 | } 429 | 430 | pyName = PyUnicode_DecodeFSDefault("orb_driver"); 431 | 432 | 433 | if (pyName != nullptr) 434 | { 435 | pyModule = PyImport_Import(pyName); 436 | Py_DECREF(pyName); 437 | } 438 | 439 | if (pyModule != nullptr) 440 | { 441 | pyFunc = PyObject_GetAttrString(pyModule, "orb_initialize"); 442 | 443 | if (pyFunc != nullptr && PyCallable_Check(pyFunc)) 444 | { 445 | pyArg1 = PyUnicode_FromString(name); 446 | pyArg2 = PyBool_FromLong(gpu); 447 | 448 | pyArgs = PyTuple_New(2); 449 | PyTuple_SetItem(pyArgs, 0, pyArg1); 450 | PyTuple_SetItem(pyArgs, 1, pyArg2); 451 | 452 | 453 | pyValue = PyObject_CallObject(pyFunc, pyArgs); 454 | 455 | Py_DECREF(pyArgs); 456 | 457 | if (pyValue != nullptr && PyFloat_Check(pyValue)) 458 | { 459 | this->initializedPython = 1; 460 | cutoff = PyFloat_AsDouble(pyValue); 461 | } 462 | else 463 | { 464 | if (PyErr_Occurred()) PyErr_Print(); 465 | } 466 | 467 | Py_XDECREF(pyValue); 468 | } 469 | 470 | else 471 | { 472 | if (PyErr_Occurred()) PyErr_Print(); 473 | } 474 | 475 | Py_XDECREF(pyFunc); 476 | 477 | pyFunc = PyObject_GetAttrString(pyModule, "orb_get_energy_forces_stress"); 478 | 479 | if (pyFunc != nullptr && PyCallable_Check(pyFunc)) 480 | { 481 | // NOP 482 | } 483 | else 484 | { 485 | this->initializedPython = 0; 486 | if (PyErr_Occurred()) PyErr_Print(); 487 | } 488 | 489 | //Py_XDECREF(pyFunc); 490 | //Py_DECREF(pyModule); 491 | } 492 | 493 | else 494 | { 495 | if (PyErr_Occurred()) PyErr_Print(); 496 | } 497 | 498 | if (this->initializedPython == 0) 499 | { 500 | Py_XDECREF(pyFunc); 501 | Py_XDECREF(pyModule); 502 | 503 | if(Py_IsInitialized()) Py_Finalize(); 504 | 505 | error->all(FLERR, "Cannot initialize python for pair_coeff of Orb Model."); 506 | } 507 | 508 | this->pyModule = pyModule; 509 | this->pyFunc = pyFunc; 510 | 511 | return cutoff; 512 | } 513 | 514 | double PairOrb::calculatePython() 515 | { 516 | int i; 517 | int iatom; 518 | int natom = list->inum; 519 | 520 | double energy = 0.0; 521 | int hasEnergy = 0; 522 | int hasForces = 0; 523 | int hasStress = 0; 524 | 525 | PyObject* pyFunc = this->pyFunc; 526 | PyObject* pyArgs = nullptr; 527 | PyObject* pyArg1 = nullptr; 528 | PyObject* pyArg2 = nullptr; 529 | PyObject* pyArg3 = nullptr; 530 | PyObject* pyAsub = nullptr; 531 | PyObject* pyValue = nullptr; 532 | PyObject* pyVal1 = nullptr; 533 | PyObject* pyVal2 = nullptr; 534 | PyObject* pyVal3 = nullptr; 535 | PyObject* pyVsub = nullptr; 536 | PyObject* pyVobj = nullptr; 537 | 538 | // set cell -> pyArgs1 539 | pyArg1 = PyList_New(3); 540 | 541 | for (i = 0; i < 3; ++i) 542 | { 543 | pyAsub = PyList_New(3); 544 | PyList_SetItem(pyAsub, 0, PyFloat_FromDouble(this->cell[i][0])); 545 | PyList_SetItem(pyAsub, 1, PyFloat_FromDouble(this->cell[i][1])); 546 | PyList_SetItem(pyAsub, 2, PyFloat_FromDouble(this->cell[i][2])); 547 | PyList_SetItem(pyArg1, i, pyAsub); 548 | } 549 | 550 | // set atomNums -> pyArgs2 551 | pyArg2 = PyList_New(natom); 552 | 553 | for (iatom = 0; iatom < natom; ++iatom) 554 | { 555 | PyList_SetItem(pyArg2, iatom, PyLong_FromLong(this->atomNums[iatom])); 556 | } 557 | 558 | // set positions -> pyArgs3 559 | pyArg3 = PyList_New(natom); 560 | 561 | for (iatom = 0; iatom < natom; ++iatom) 562 | { 563 | pyAsub = PyList_New(3); 564 | PyList_SetItem(pyAsub, 0, PyFloat_FromDouble(this->positions[iatom][0])); 565 | PyList_SetItem(pyAsub, 1, PyFloat_FromDouble(this->positions[iatom][1])); 566 | PyList_SetItem(pyAsub, 2, PyFloat_FromDouble(this->positions[iatom][2])); 567 | PyList_SetItem(pyArg3, iatom, pyAsub); 568 | } 569 | 570 | // call function 571 | pyArgs = PyTuple_New(3); 572 | PyTuple_SetItem(pyArgs, 0, pyArg1); 573 | PyTuple_SetItem(pyArgs, 1, pyArg2); 574 | PyTuple_SetItem(pyArgs, 2, pyArg3); 575 | 576 | pyValue = PyObject_CallObject(pyFunc, pyArgs); 577 | 578 | Py_DECREF(pyArgs); 579 | 580 | if (pyValue != nullptr && PyTuple_Check(pyValue) && PyTuple_Size(pyValue) >= 3) 581 | { 582 | // get energy <- pyValue 583 | pyVal1 = PyTuple_GetItem(pyValue, 0); 584 | if (pyVal1 != nullptr && PyFloat_Check(pyVal1)) 585 | { 586 | hasEnergy = 1; 587 | energy = PyFloat_AsDouble(pyVal1); 588 | } 589 | else 590 | { 591 | if (PyErr_Occurred()) PyErr_Print(); 592 | } 593 | 594 | // get forces <- pyValue 595 | pyVal2 = PyTuple_GetItem(pyValue, 1); 596 | if (pyVal2 != nullptr && PyList_Check(pyVal2) && PyList_Size(pyVal2) >= natom) 597 | { 598 | hasForces = 1; 599 | 600 | for (iatom = 0; iatom < natom; ++iatom) 601 | { 602 | pyVsub = PyList_GetItem(pyVal2, iatom); 603 | if (pyVsub != nullptr && PyList_Check(pyVsub) && PyList_Size(pyVsub) >= 3) 604 | { 605 | for (i = 0; i < 3; ++i) 606 | { 607 | pyVobj = PyList_GetItem(pyVsub, i); 608 | if (pyVobj != nullptr && PyFloat_Check(pyVobj)) 609 | { 610 | this->forces[iatom][i] = PyFloat_AsDouble(pyVobj); 611 | } 612 | else 613 | { 614 | if (PyErr_Occurred()) PyErr_Print(); 615 | hasForces = 0; 616 | break; 617 | } 618 | } 619 | } 620 | else 621 | { 622 | if (PyErr_Occurred()) PyErr_Print(); 623 | hasForces = 0; 624 | break; 625 | } 626 | 627 | if (hasForces == 0) 628 | { 629 | break; 630 | } 631 | } 632 | } 633 | else 634 | { 635 | if (PyErr_Occurred()) PyErr_Print(); 636 | } 637 | 638 | // get stress <- pyValue 639 | pyVal3 = PyTuple_GetItem(pyValue, 2); 640 | if (pyVal3 != nullptr && PyList_Check(pyVal3) && PyList_Size(pyVal3) >= 6) 641 | { 642 | hasStress = 1; 643 | 644 | for (i = 0; i < 6; ++i) 645 | { 646 | pyVobj = PyList_GetItem(pyVal3, i); 647 | if (pyVobj != nullptr && PyFloat_Check(pyVobj)) 648 | { 649 | this->stress[i] = PyFloat_AsDouble(pyVobj); 650 | } 651 | else 652 | { 653 | if (PyErr_Occurred()) PyErr_Print(); 654 | hasStress = 0; 655 | break; 656 | } 657 | } 658 | } 659 | else 660 | { 661 | if (PyErr_Occurred()) PyErr_Print(); 662 | } 663 | } 664 | 665 | else 666 | { 667 | if (PyErr_Occurred()) PyErr_Print(); 668 | } 669 | 670 | Py_XDECREF(pyValue); 671 | 672 | if (hasEnergy == 0 || hasForces == 0 || hasStress == 0) 673 | { 674 | error->all(FLERR, "Cannot calculate energy, forces and stress by python of Orb Model."); 675 | } 676 | 677 | return energy; 678 | } 679 | 680 | static const int NUM_ELEMENTS = 118; 681 | 682 | static const char* ALL_ELEMENTS[] = { 683 | "H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne", "Na", "Mg", "Al", "Si", "P", "S", 684 | "Cl", "Ar", "K", "Ca", "Sc", "Ti", "V", "Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn", "Ga", "Ge", 685 | "As", "Se", "Br", "Kr", "Rb", "Sr", "Y", "Zr", "Nb", "Mo", "Tc", "Ru", "Rh", "Pd", "Ag", "Cd", 686 | "In", "Sn", "Sb", "Te", "I", "Xe", "Cs", "Ba", "La", "Ce", "Pr", "Nd", "Pm", "Sm", "Eu", "Gd", 687 | "Tb", "Dy", "Ho", "Er", "Tm", "Yb", "Lu", "Hf", "Ta", "W", "Re", "Os", "Ir", "Pt", "Au", "Hg", 688 | "Tl", "Pb", "Bi", "Po", "At", "Rn", "Fr", "Ra", "Ac", "Th", "Pa", "U", "Np", "Pu", "Am", "Cm", 689 | "Bk", "Cf", "Es", "Fm", "Md", "No", "Lr", "Rf", "Db", "Sg", "Bh", "Hs", "Mt", "Ds", "Rg", "Cn", 690 | "Nh", "Fl", "Mc", "Lv", "Ts", "Og" 691 | }; 692 | 693 | int PairOrb::elementToAtomNum(const char *elem) 694 | { 695 | char elem1[16]; 696 | 697 | strcpy(elem1, elem); 698 | 699 | this->toRealElement(elem1); 700 | 701 | if (strlen(elem1) > 0) 702 | { 703 | for (int i = 0; i < NUM_ELEMENTS; ++i) 704 | { 705 | if (strcasecmp(elem1, ALL_ELEMENTS[i]) == 0) 706 | { 707 | return (i + 1); 708 | } 709 | } 710 | } 711 | 712 | char estr[256]; 713 | sprintf(estr, "Incorrect name of element: %s", elem); 714 | error->all(FLERR, estr); 715 | 716 | return 0; 717 | } 718 | 719 | void PairOrb::toRealElement(char *elem) 720 | { 721 | int n = strlen(elem); 722 | n = n > 2 ? 2 : n; 723 | 724 | int m = n; 725 | 726 | for (int i = 0; i < n; ++i) 727 | { 728 | char c = elem[i]; 729 | if (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || 730 | c == '5' || c == '6' || c == '7' || c == '8' || c == '9' || c == ' ' || 731 | c == '_' || c == '-' || c == '+' || c == '*' || c == '~' || c == ':' || c == '#') 732 | { 733 | m = i; 734 | break; 735 | } 736 | 737 | elem[i] = c; 738 | } 739 | 740 | elem[m] = '\0'; 741 | } 742 | -------------------------------------------------------------------------------- /patch/pair_orb.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 AdvanceSoft Corporation 3 | * Copyright (C) 2024 Stefan Bringuier 4 | * 5 | * This source code is licensed under the GNU General Public License Version 2 6 | * found in the LICENSE file in the root directory of this source tree. 7 | */ 8 | 9 | #ifdef PAIR_CLASS 10 | 11 | PairStyle(orb, PairOrb) 12 | 13 | #else 14 | 15 | #ifndef LMP_PAIR_ORB_H_ 16 | #define LMP_PAIR_ORB_H_ 17 | 18 | #include 19 | #include 20 | #include "atom.h" 21 | #include "comm.h" 22 | #include "error.h" 23 | #include "pair.h" 24 | #include "force.h" 25 | #include "update.h" 26 | #include "memory.h" 27 | #include "neigh_list.h" 28 | #include "neigh_request.h" 29 | #include "neighbor.h" 30 | #include "domain.h" 31 | 32 | namespace LAMMPS_NS 33 | { 34 | 35 | class PairOrb : public Pair 36 | { 37 | public: 38 | PairOrb(class LAMMPS*); 39 | 40 | virtual ~PairOrb() override; 41 | 42 | void compute(int, int) override; 43 | 44 | void settings(int, char **) override; 45 | 46 | void coeff(int, char **) override; 47 | 48 | double init_one(int, int) override; 49 | 50 | void init_style() override; 51 | 52 | protected: 53 | virtual int withGPU(); 54 | 55 | private: 56 | int* atomNumMap; 57 | int* atomNums; 58 | double** cell; 59 | double** positions; 60 | double** forces; 61 | double* stress; 62 | 63 | int maxinum; 64 | int initializedPython; 65 | double cutoff; 66 | 67 | int npythonPath; 68 | char** pythonPaths; 69 | 70 | PyObject* pyModule; 71 | PyObject* pyFunc; 72 | 73 | void allocate(); 74 | 75 | void prepareGNN(); 76 | 77 | void performGNN(); 78 | 79 | void finalizePython(); 80 | 81 | double initializePython(const char *name, int gpu); 82 | 83 | double calculatePython(); 84 | 85 | int elementToAtomNum(const char *elem); 86 | 87 | void toRealElement(char *elem); 88 | 89 | static int finalized; 90 | static void finalize(); 91 | }; 92 | 93 | } // namespace LAMMPS_NS 94 | 95 | #endif /* LMP_PAIR_ORB_H_ */ 96 | #endif 97 | -------------------------------------------------------------------------------- /patch/patch.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ "$#" -ne 1 ]; then 4 | echo "Usage: $0 " 5 | exit 1 6 | fi 7 | 8 | LAMMPS_PATH=$1 9 | 10 | # Verify the provided path is a valid directory 11 | if [ ! -d "$LAMMPS_PATH" ]; then 12 | echo "Error: Provided path is not a valid directory." 13 | exit 1 14 | fi 15 | 16 | ML_ORB_DIR="$LAMMPS_PATH/src/ML-ORB" 17 | mkdir -p "$ML_ORB_DIR" 18 | 19 | cp -r patch/* "$ML_ORB_DIR" 20 | 21 | # Modify CMakeLists.txt to include ML-ORB package 22 | CMAKE_LIST="$LAMMPS_PATH/cmake/CMakeLists.txt" 23 | if grep -q "ML-ORB" "$CMAKE_LIST"; then 24 | echo "ML-ORB package already exists in CMakeLists.txt." 25 | else 26 | sed -i '/set(STANDARD_PACKAGES/ a\ ML-ORB' "$CMAKE_LIST" 27 | echo "ML-ORB package added to CMakeLists.txt." 28 | fi 29 | 30 | echo "Patch applied successfully!" 31 | --------------------------------------------------------------------------------