天天看點

用飛天加速計劃資源學習 ABINIT 計算(二)下載下傳資料和輸入檔案準備計算利用 Abipy 實作簡單可視化Abipy 的另一個示例:輸出導出至 VESTA總結附加:網頁顯示

上一篇文章: https://developer.aliyun.com/article/833409

本節還是主要介紹一些伺服器的使用。如果能還有下一篇文章的話,我會嘗試把《Density Functional Theory: A Practical Introduction》這本書裡的一些計算例子用 ABINIT 實作。

還記得我們上次的内容嗎?特别是我們在伺服器裡套的娃。我們再來一次:

  1. 打開 PuTTY,連接配接上伺服器;
  2. 伺服器裡使用

    conda activate learn abinit

    ,進入 conda 的 abinit 環境中。

下載下傳資料和輸入檔案

我們上次是手動下載下傳計算氫原子所需的赝勢資料和輸入檔案,但這樣太麻煩啦!我們下面用

git

把 ABINIT 的源代碼(包含代碼以及我們所需的教程相關的檔案)下載下傳下來。

首先,安裝

git

apt install git           

簡單的來說,

git

是一個代碼版本管理工具。程式員在寫代碼時會有各種需求:分工合作,各人寫不同部分的代碼;修 bug,一個人去改另一個人的代碼等等。總之

git

這類軟體幫助程式員們記錄哪個人在什麼時候對哪個版本的代碼做了什麼改動,并且使改進代碼變得更簡單。

我們不在意代碼的曆史,是以我們可以讓

git

少下載下傳很多東西:(下面的指令裡

9.6.2

最好改為機器上

abinit --version

的相應版本)

git clone --depth 1 --branch 9.6.2 https://github.com.cnpmjs.org/abinit/abinit.git           

我猜意思大概是清楚的?我們讓

git

clone

(複制)來自

https://github.com.cnpmjs.org/abinit/abinit.git

的代碼,這個連結是 ABINIT 代碼的國内加速位址。因為我們不需要曆史是以曆史的深度

--depth

設為

1

(我們隻需要目前的“曆史”) 。我們需要的是版本

9.6.2

的代碼,

--branch

指定了我們需要的代碼版本。(

git

中代碼版本一般用

branch

(分支)或是

tag

(标簽)辨別,ABINIT 程式員已經把某個特定時間對應的曆史代碼标上了

9.6.2

的标簽。)

ls

指令看看我們下載下傳了什麼:

(learn-abinit) root@iZ0jl9j9dfuk4s5c3w60uhZ:~# ls
abinit  ABINIT-1  Miniconda3-py38_4.10.3-Linux-x86_64.sh           

(我們之後大多會使用這種形式同時展現我們的指令輸入和伺服器的輸出。)

我們這裡沒表現出來,你所看到的

abinit

ABINIT-1

應該是藍色的,這表示它們是目錄。(也有可能你看到的是

abinit/

ABINIT-1/

,這也是同樣意思。)

後兩個檔案是我們上一篇文章建立的,是以我們的

git

指令下載下傳的是

abinit/

目錄。我們看看

abinit

目錄裡有什麼:

(learn-abinit) root@iZ0jl9j9dfuk4s5c3w60uhZ:~# ls -p abinit
abichecks/               developers/           PACKAGING
abimkdocs/               doc/                  README
abimkdocs_plugin/        Doxyfile              README.md
abimkdocs_tests/         fallbacks/            RELNOTES
abinit_theme/            fkiss/                requirements.txt
abisrc.py                INSTALL               scripts/
abisrc_requirements.txt  INSTALL_CentOS.md     set_abienv.sh
AUTHORS                  INSTALL_EasyBuild.md  setup.cfg
autogen.sh               INSTALL_gpu.md        shared/
bindings/                INSTALL_MacOS.md      split/
ChangeLog                INSTALL.md            src/
change_source.py         INSTALL_Ubuntu.md     tasks.py
config/                  KNOWN_PROBLEMS        tests/
config.pc.in             mkdocs.yml            THANKS
config.sh.in             mkrobodoc.sh          TODO
configure.ac             mksite.py             TODO.buildsys
COPYING                  NEWS                  upgrade-build-config-file.sh
coverage/                packages/             wipeout.sh           

哇!好多檔案!

我們先來解釋一下

ls -p abinit

:參數

-p

表示我們希望把

目錄名稱

顯示為

目錄名稱/

,這樣我把輸出複制到我們這篇教程裡之後我們也能夠看出哪個是目錄。

ls

預設展示的是目前目錄,但當我們想讓它展示

abinti

目錄時,我們便用了

abinit

作為參數。

現在我們了解了我們部分檔案夾的“樹形”結構:

/root/
|
|---- abinit/
|     |
|     |----- abichecks/
|     |----- abimkdocs/
|     |----- ...
|
|---- ABINIT-1/
|
|---- Miniconda3-py38_4.10.3-Linux-x86_64.sh           

試着用

ls

看看

ABINIT-1

以及

abinit

目錄裡的

abichecks

tests

目錄的内容?

如果還不熟悉目錄操作,可以到

https://gitee.com/thuyesh/abinit/tree/9.6.2

來看看

abinit

目錄的結構,再配合

pwd

,

ls

cd

來各種試驗一下。

目前目錄

abinit

下的

abimkdocs

的路徑是

abinit/abimkdocs

,如果我們想要指定上級目錄呢?

abinit/abimkdocs/..

其實也是

abinit

的路徑,是以

..

是什麼意思?

abinit/././././././abimkdocs

其實就是

abinit/abimkdocs

,是以 “

.

” 是什麼目錄?

另外,你永遠可以用

/root

指代我們的“家”目錄。檢視

pwd

的值,

/root/abinit

root/abinit

有無差別?

/root

/root/

沒有說明的情況下是沒有差別的。

準備計算

我猜你現在已經可以看懂下面的指令了:(

cp

Copy

複制的意思)

(learn-abinit) root@iZ0jl9j9dfuk4s5c3w60uhZ:~# cd /root
(learn-abinit) root@iZ0jl9j9dfuk4s5c3w60uhZ:~# cd abinit/tests/tutorial/Input/
(learn-abinit) root@iZ0jl9j9dfuk4s5c3w60uhZ:~/abinit/tests/tutorial/Input# mkdir Work
(learn-abinit) root@iZ0jl9j9dfuk4s5c3w60uhZ:~/abinit/tests/tutorial/Input# cd Work/
(learn-abinit) root@iZ0jl9j9dfuk4s5c3w60uhZ:~/abinit/tests/tutorial/Input/Work# cp ../tbase1_1.abi .           

我們再來重複一次上一篇文章的工作:

ABI_PSPDIR=/root/abinit/tests/Psps_for_tests/ abinit tbase1_1.abi &> log           

我們現在來解釋一下這行指令,

ABI_PSPDIR

在上篇中已經提到,因為 ABINIT 需要赝勢資料,我們下載下傳下來的赝勢資料就在

/root/abinit/tests/Psps_for_tests/

目錄下,是以我們在正式

abinit

指令前,以

ABI_PSPDIR=...

的格式指定赝勢所在目錄。抛去前面這個,我們剩下

abinit tbase1_1.abi &> log

abinit tbase1_1.abi

是主要指令,它使

abinit

程式去找

tbase1_1.abi

檔案并從中讀取計算指令。

&> log

的話:我們看到

apt

指令的輸出有很多,但其中值得注意的資訊暫時不太多;不加

&> log

的話,

abinit

也會刷刷輸出很多東西(不妨試試?),其中有很多有用的、我們想要記錄下來的東西。如果我們留它刷刷刷輸出的話我們可能需要手動複制粘貼才能好好地記錄其中的資訊,而

&> log

的功能便是把

abinit

程式的正常輸出以及可能的異常輸出導出到

log

檔案中去(

log

這個檔案名可以随便改)。于是我們的

ls

輸出裡便有了

log

這個檔案。

&>

>&

是一樣的意思,是以網上看到另一種也不要吃驚?)

(現在你可以試一下

ls &> ls_output

指令并用

nano

檢視

ls_output

檔案中的輸出。)

tbase1_1.abi

的這個輸入檔案内容的各種關鍵詞在官網上有一個清單:

Base1 - abinit

(需要向下翻一點點的)。我這裡就不獻醜了。

利用 Abipy 實作簡單可視化

ABINIT 的輸出檔案看起來密密麻麻。事實上,ABINIT 團隊提供了

Abipy

來對各種輸出檔案進行處理。我們下面示範使用 Abipy 來将其中一些輸出檔案用 Jupyter Lab/Notebook 遠端可視化。

阿裡雲 ECS 的安全防護措施預設阻止了各種網絡“端口”的通路——也就是我們必須手動開啟一些設定後才能通過“端口”對外通信,才能夠遠端通路我們的可視化 Notebook。

如圖進行操作,将

80

端口開啟即可。

  1. 進入安全組配置:
    用飛天加速計劃資源學習 ABINIT 計算(二)下載下傳資料和輸入檔案準備計算利用 Abipy 實作簡單可視化Abipy 的另一個示例:輸出導出至 VESTA總結附加:網頁顯示
  2. 點選配置規則:
    用飛天加速計劃資源學習 ABINIT 計算(二)下載下傳資料和輸入檔案準備計算利用 Abipy 實作簡單可視化Abipy 的另一個示例:輸出導出至 VESTA總結附加:網頁顯示
  3. 快速添加:
    用飛天加速計劃資源學習 ABINIT 計算(二)下載下傳資料和輸入檔案準備計算利用 Abipy 實作簡單可視化Abipy 的另一個示例:輸出導出至 VESTA總結附加:網頁顯示
  4. 選中

    HTTP (80)

    确認即可:
    用飛天加速計劃資源學習 ABINIT 計算(二)下載下傳資料和輸入檔案準備計算利用 Abipy 實作簡單可視化Abipy 的另一個示例:輸出導出至 VESTA總結附加:網頁顯示

下面的操作在 conda 環境(即教程中的

learn-abinit

環境)中進行:

(learn-abinit) root@iZ0jl9j9dfuk4s5c3w60uhZ:~# conda config --set channel_priority flexible
(learn-abinit) root@iZ0jl9j9dfuk4s5c3w60uhZ:~# conda install abipy nbformat jupyterlab ipympl nodejs           

我們通過 conda 安裝了

abipy

以及遠端可視化所需的其它軟體,下面是對 Abipy 的極簡初始配置:

mv

指令代表 move,也即我們常說的重命名操作)

(learn-abinit) root@iZ0jl9j9dfuk4s5c3w60uhZ:~# abicheck.py -c # 生成模闆配置檔案,出錯是正常的
(learn-abinit) root@iZ0jl9j9dfuk4s5c3w60uhZ:~# mv /root/.abinit/abipy/manager.yaml /root/.abinit/abipy/manager.yml # 将模闆配置檔案直接重命名為配置檔案名
(learn-abinit) root@iZ0jl9j9dfuk4s5c3w60uhZ:~# mv /root/.abinit/abipy/scheduler.yaml /root/.abinit/abipy/scheduler.yml # 将模闆配置檔案直接重命名為配置檔案名
(learn-abinit) root@iZ0jl9j9dfuk4s5c3w60uhZ:~# abicheck.py # 檢查配置正确與否           

下面是對 Jupyter Lab 進行初始化:

(learn-abinit) root@iZ0jl9j9dfuk4s5c3w60uhZ:~# jupyter notebook --generate-config
Writing default config to: /root/.jupyter/jupyter_notebook_config.py
(learn-abinit) root@iZ0jl9j9dfuk4s5c3w60uhZ:~# nano /root/.jupyter/jupyter_notebook_config.py # 這裡編輯的檔案名請複制上一個指令裡的           

我們現在進入了

nano

的編輯界面,對 Jupyter Notebook 的配置檔案進行編輯,我們要加入的配置是以下三行,一般可以跑到檔案的最後去加入:

c.NotebookApp.allow_root = True
c.NotebookApp.ip = '*'
c.NotebookApp.port = 80           

(其實

allow_root

并不是一個好的安全習慣,但篇幅限制,這裡不介紹我們 Linux 伺服器上的各種權限以及使用者操作。)

我們繼續 Jupyter Lab 的配置:

(learn-abinit) root@iZ0jl9j9dfuk4s5c3w60uhZ:~# jlpm config set registry https://registry.npmmirror.com
(learn-abinit) root@iZ0jl9j9dfuk4s5c3w60uhZ:~# jupyter lab build           

經過一陣等待後,配置完成!

如果現在你的目前目錄(

pwd

)不是

/root/abinit/tests/tutorial/Input/Work

的話,請回到這個目錄。

下面我們通過 Abipy 的

abiopen.py

程式開啟 Jupyter Lab:

(learn-abinit) root@iZ0jl9j9dfuk4s5c3w60uhZ:~/abinit/tests/tutorial/Input/Work# abiopen.py tbase1_1.abo --notebook --foreground
[I 2021-12-17 23:56:57.305 ServerApp] jupyterlab | extension was successfully linked.
[W 2021-12-17 23:56:57.308 NotebookApp] 'allow_root' has moved from NotebookApp to ServerApp. This config will be passed to ServerApp. Be sure to update your config before our next release.
[W 2021-12-17 23:56:57.308 NotebookApp] 'ip' has moved from NotebookApp to ServerApp. This config will be passed to ServerApp. Be sure to update your config before our next release.
[W 2021-12-17 23:56:57.308 NotebookApp] 'port' has moved from NotebookApp to ServerApp. This config will be passed to ServerApp. Be sure to update your config before our next release.
[I 2021-12-17 23:56:57.466 ServerApp] nbclassic | extension was successfully linked.
[W 2021-12-17 23:56:57.485 ServerApp] WARNING: The Jupyter server is listening on all IP addresses and not using encryption. This is not recommended.
[I 2021-12-17 23:56:57.490 ServerApp] nbclassic | extension was successfully loaded.
[I 2021-12-17 23:56:57.491 LabApp] JupyterLab extension loaded from /opt/miniconda3/envs/learn-abinit/lib/python3.9/site-packages/jupyterlab
[I 2021-12-17 23:56:57.492 LabApp] JupyterLab application directory is /opt/miniconda3/envs/learn-abinit/share/jupyter/lab
[I 2021-12-17 23:56:57.495 ServerApp] jupyterlab | extension was successfully loaded.
[I 2021-12-17 23:56:57.496 ServerApp] Serving notebooks from local directory: /root/abinit/tests/tutorial/Input/Work
[I 2021-12-17 23:56:57.496 ServerApp] Jupyter Server 1.13.1 is running at:
[I 2021-12-17 23:56:57.496 ServerApp] http://iZ0jl9j9dfuk4s5c3w60uhZ:80/lab?token=e8345a692eea32e41211ff7934c96cd4a75b3c7b0cc71531
[I 2021-12-17 23:56:57.496 ServerApp]  or http://127.0.0.1:80/lab?token=e8345a692eea32e41211ff7934c96cd4a75b3c7b0cc71531
[I 2021-12-17 23:56:57.496 ServerApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 2021-12-17 23:56:57.500 ServerApp] No web browser found: could not locate runnable browser.
[C 2021-12-17 23:56:57.500 ServerApp]

    To access the server, open this file in a browser:
        file:///root/.local/share/jupyter/runtime/jpserver-43759-open.html
    Or copy and paste one of these URLs:
        http://iZ0jl9j9dfuk4s5c3w60uhZ:80/lab?token=e8345a692eea32e41211ff7934c96cd4a75b3c7b0cc71531
     or http://127.0.0.1:80/lab?token=e8345a692eea32e41211ff7934c96cd4a75b3c7b0cc71531           

程式會有一大堆輸出。我們見不到

(learn-abinit) root@**** #

這樣的一行——這意味着程式還沒結束,我們一般叫做“程式還未傳回”。這時其實 Jupyter Lab 程式打開了一個網頁伺服器,其網頁位址在程式最後那一部分的輸出給出:

http://127.0.0.1:80/lab?token=e8345a692eea32e41211ff7934c96cd4a75b3c7b0cc71531           

但是,這個位址我們并不能通路,我們需要把

127.0.0.1

改為伺服器的 IP 位址(PuTTY 上填的那個),再到浏覽器中去通路。例如,我的例子裡就是替換後變為了:

http://8.130.17.77:80/lab?token=e8345a692eea32e41211ff7934c96cd4a75b3c7b0cc71531           

請把

127.0.0.1

對應替換為自己的 IP 位址,并在浏覽器中通路對應連結。

需要等待一段時間。界面如圖:

用飛天加速計劃資源學習 ABINIT 計算(二)下載下傳資料和輸入檔案準備計算利用 Abipy 實作簡單可視化Abipy 的另一個示例:輸出導出至 VESTA總結附加:網頁顯示

輕按兩下打開左邊的

abinb_***

的檔案(如果你運作了多次上面的指令,可以看時間選擇最新的一個)。選擇預設 kernel:

用飛天加速計劃資源學習 ABINIT 計算(二)下載下傳資料和輸入檔案準備計算利用 Abipy 實作簡單可視化Abipy 的另一個示例:輸出導出至 VESTA總結附加:網頁顯示

可以看到,我們的網頁裡打開了一個 Jupyter Notebook 檔案,這個檔案裡包含了可視化輸出的 Python 代碼。

然而在直接運作代碼之前,我們要進行一些修改,請按下面的提示找到相應的代碼進行修改:

# 修改前
%matplotlib notebook
# 修改後
%matplotlib widget

# 修改前
abo.plot()
# 修改後
for i in abo.yield_figs():
    pass           

修改完成後,運作整個檔案最友善的是直接使用下面的按鈕:

用飛天加速計劃資源學習 ABINIT 計算(二)下載下傳資料和輸入檔案準備計算利用 Abipy 實作簡單可視化Abipy 的另一個示例:輸出導出至 VESTA總結附加:網頁顯示

好的,現在你就擁有一系列好看的圖檔了:

用飛天加速計劃資源學習 ABINIT 計算(二)下載下傳資料和輸入檔案準備計算利用 Abipy 實作簡單可視化Abipy 的另一個示例:輸出導出至 VESTA總結附加:網頁顯示

如果想要退出 Jupyter Lab,在 PuTTY 終端視窗裡按下 Ctrl+C,并輸入

y

回車确認。

Abipy 的另一個示例:輸出導出至 VESTA

沒有軟體可能做到顧及所有功能,例如我們的 ABINIT 和 Abipy 難以直接做到 3D 可視化的效果。但是我們知道一個常用的 3D 晶體可視化的軟體

VESTA

。請先在自己的電腦上安裝好

(這裡不贅述過程),然後我們來用 VESTA 可視化 ABINIT 的電子密度結果。

我們下面會進入 Python 的互動界面,其中

In [1]:

等的也是指令提示符,隻是指令的接受者為 Python 程式自己。

tbase1_1o_DEN

儲存了電子密度資訊。下面的 Python 代碼就不做解釋了,請考慮一下學習 Python :-)

(learn-abinit) root@iZ0jl9j9dfuk4s5c3w60uhZ:~/abinit/tests/tutorial/Input/Work# abiopen.py tbase1_1o_DEN
Python 3.9.7 | packaged by conda-forge | (default, Sep 29 2021, 19:23:11)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.30.1 -- An enhanced Interactive Python. Type '?' for help.



The Abinit file object is associated to the `abifile` python variable.
Use `abifile.<TAB>` to list available methods.
Use e.g. `abifile.plot?` to access docstring and `abifile.plot??` to visualize source.
Use `print(abifile)` to print the object.


In [1]: abifile.get_density().export("tbase1_1.xsf") # <- 我們的輸入
tokens ['tbase1_1', 'xsf'] ext xsf
/opt/miniconda3/envs/learn-abinit/lib/python3.9/site-packages/abipy/iotools/xsf.py:126: FutureWarning: lattice_vectors is deprecated
lattice_vectors is deprecated and will be removed in abipy 1.0
  cell = structure.lattice_vectors(space="r")
Out[1]: <abipy.iotools.visualizer.Xcrysden at 0x7f0bcd8331f0>

In [2]: exit # <- 我們的輸入           

ls

一下你會發現目錄下多出了一個

tbase1_1.xsf

的檔案,我們需要把這個檔案下載下傳回我們的電腦。因為篇幅限制,這裡簡單地用 Python 的

http.server

來傳檔案:

(learn-abinit) root@iZ0jl9j9dfuk4s5c3w60uhZ:~/abinit/tests/tutorial/Input/Work# python3 -m http.server 80
Serving HTTP on 0.0.0.0 port 80 (http://0.0.0.0:80/) ...           

好的,還是把 IP 替換調,打開網頁,找到我們的

tbase1_1.xsf

檔案,點選下載下傳。

用 VESTA 打開:

用飛天加速計劃資源學習 ABINIT 計算(二)下載下傳資料和輸入檔案準備計算利用 Abipy 實作簡單可視化Abipy 的另一個示例:輸出導出至 VESTA總結附加:網頁顯示

略微調一調設定(例如在

Objects->Boundary...

裡調整

x, y, z

的範圍:

例如:

用飛天加速計劃資源學習 ABINIT 計算(二)下載下傳資料和輸入檔案準備計算利用 Abipy 實作簡單可視化Abipy 的另一個示例:輸出導出至 VESTA總結附加:網頁顯示
用飛天加速計劃資源學習 ABINIT 計算(二)下載下傳資料和輸入檔案準備計算利用 Abipy 實作簡單可視化Abipy 的另一個示例:輸出導出至 VESTA總結附加:網頁顯示

(Python

http.server

程式還是需要 Ctrl+C 退出。)

總結

  1. Linux 的目錄概念:

    .

    表示目前目錄,

    ..

    表示上一級目錄,絕對路徑

    /root

    與相對路徑

    root

    的辨析;
  2. ABI_PSPDIR=/root/abinit/tests/Psps_for_tests/ abinit tbase1_1.abi &> log

    指令的分塊了解;
  3. 用 Jupyter Lab 遠端編寫 Python 代碼調用 Abipy 來可視化結果;
  4. python3 -m http.server 80

    來簡單地把檔案從伺服器下載下傳到電腦;
  5. 用 Python 代碼調用 Abipy 導出電子密度結果到 VESTA。

附加:網頁顯示

聽說有個展示網頁好些?本文不涉及

3Dmol.js

的教程,但是通過使用

abifile.get_density().export_to_cube("tbase1_1.cube")

調用 Abipy 将電子密度導出至 CUBE 檔案,再使用 3Dmol.js 讀取檔案,我們的确可以在網頁上顯示出我們的分子或是晶胞模型。

這裡是我做的一個例子,了解網頁開發的同學可以通過網頁控制台直接看源碼,這裡不多講。

一些電子密度的可視化(3Dmol.js)