BNU-FZH

fengzhenhua@outlook.com

将shell命令赋值给变量有两种不同的方法,这对于shell脚本编程特别有用,这两种形式为:

linux命令输出赋值给变量
1
2
3
4
5
变量名=$(命令)
变量名=$(命令 [命令选项...] 参数1 参数2)
或者
变量名=`命令`
变量名=`命令 [命令选项...] 参数1 参数2`

第二种方法中使用的是键盘左上角Esc键下面的反引号,多数键盘上和符号在同一个键上,并不是键盘右侧的单引号,这一点特别注意。

今天完成了引力理论专题的大作业,以朗道书中的一个复杂的计算的详细推导作为汇报内容。已经将作业发送给了高思杰老师(sijie@bnu.edu.cn), 同时将作业同步展示于此:

2023年 06月 21日 星期三 00:04:33 CST , 今天在核科院办公电脑上成功使用 Lua 脚本配置好了基本的 Neovim , 所以准备逐步完善 Neovim 配置教程。

Neovim 的依懒

在配置好 Neovim 后发现有的插件并不能正确运行,这可能是系统需要的依懒没有都装上。此时运行命令 :checkhealth ,Neovim 便可以进行健康检测,对于缺失的软件则对应报告安装即可。这里需要注意,有的软件并不是直接就是命令,而是某个软件提供的命令,例如javac 这条命令就无法使用pacman -Ss javac找到,经过百度发现这条命令是由软件jdk17-openjdk提供,此源于 archwiki ,于是安装软件问题解决,即:

1
sudo pacman -S jdk17-openjdk

Neovim配置vim-latex

在使用nvim编辑latex文件时,首先需要建立对应的模板,这里有两个插件一个是vim-latex本身就自带模板功能,一个是vim-templates ,但是后者提供的关于Latex的模板并不好用,所以决定使用vim-latex处理所有的latex文件。但是从vim转移到nvim后,发现模板功能并不能使用,于是将原来vim配置插件时的latex模板,完全复制到packer插件管理器中的对应位置即可,即

1
2
cp ~/.vim/bundle/vim-latex/ftplugin/latex-suite/templates/*
~/.local/share/nvim/site/pack/packer/start/vim-latex/ftplugin/latex-suite/templates

在 neovim 中使用 Lua

本文本最初来源是nvim-lua-guide,由于这段时间一直在学习使用Lua配置Neovim, 但是没有系统的教材,而这篇文章却恰好给了一个大致的框架,所以本文最初借用此文,待时机成熟再正式开始写作。

nvim-lua-guide 中文版简易教程

译者:Neovim Core Developer

:arrow_upper_left: (感觉太多太杂乱?使用 Github TOC 来浏览大纲!)

简介

Lua 作为 Neovim 中的一等语言的集成正在成为它的杀手级特性之一。然而,学习如何用 Lua 编写插件的教程数量并不像用 Vimscript 编写插件那样多。这是一种尝试,试图提供一些基本信息,让人们可以使用 Lua 编写 Neovim 插件。

阅读全文 »

2023年06月18日星期日多云北京市北京师范大学, 由于日常工作中需要面对大量 LaTeX 资料的编写工作,当数量大到一定量的时候在不同电脑上写作就会有不同步的问题,同时即使在同一台电脑上也会有找不到文件的可能性。同时为了不受限于网络,在很多时候 Github 并不能正确访问,所以一些需要的软件就需要通过 Gitlab 来间接的获取 ,但是每次都通过网页版操作太过麻烦,于是产生了通过脚本命令实现自动化的需要。于是决定规划编写两个主要的软件:

  1. labmgr.sh 管理本地的目录树:
    • 对于普通 LaTeX 文件,自动组织好目录并及时同步到 Gitlab
    • 一键列出所有同步的文件,自动调用 nvim 来编辑对应文档,最后保存并及时上传到对应仓库。
    • 运行时自动联网,检测 GitLab 仓库并检测更新情况,自动完成文件的更新
  2. plgmgr.sh 管理 nvim 插件:
    • 对于一些无法访问 Github 的情况,调用 GitLab api 将 github 上的源文件 fork 到 GitLab ,然后增加到 packer.vim 的插件控制目录,从 GitLab 上安装。
    • 同时考虑到插件的更新,本脚本增加 GitLab api 自动更新仓库的功能,这样就可以实现自动更新功能。
    • 再增加脚本自动备份功能,将本机配置好的 nvim 的lua 脚本及目录树一并同步到 GitLab 对应仓库, 这样就可以实现快速部署 nvim 到新电脑的目的。
  3. 参考文献:

2023年06月16日星期五晴北京市北京师范大学, 为了更好的编程和写Hexo日志,决定重新配置一下我的Neovim 。 之前由于课程紧张所以直接使用了vim的配置文件和插件,但是后来发现科技在进步,软件在升级,而Neovim 也越来越好用。于是决定使用其更加统一的接口来配置好我的Neovim, 同时本博客会逐步更新教程,力求达到让读都跟着一步一步的操作就可以完成操作。

限于时间关系,在本文的初步形态,借用一些网上的现成的文章,之后会逐步完善。近三天主要参考的网站分别为

  1. 菜鸟教程|Lua教程

  2. 从零开始配置vim(导读)

  3. 你需要知道的使用 lua 配置 neovim 的一切

  4. Snippets in Visual Studio Code

  5. Neovim主题

  6. NeoVim Builtin LSP的基本配置

  7. 行云流水般的NeoVim Builtin LSP操作

  8. Neovim|Diagnostics

  9. Neovim|Main

阅读全文 »

2023年06月12日星期一阴北京市北京师范大学, 高阶英语最先出了成绩,今将此次作业展示于此,里面的诗都是本人原创,翻译的诗歌是谢柏松老师的诗歌。

2023年06月08日星期四晴北京市北京师范大学, 发现今天上网的速度很慢,估计是DNS的问题。于是决定收录那些稳定快速的DNS服务器,逐步完善。未列出的服务器,请参考:免费公共 DNS 服务器大全

厂家 IPV4 IPV6
北京科技大学
清华大学TUNA
北京邮电大学
北京邮电大学
百度 180.76.76.76 2400:da00::6666
阿里 223.5.5.5 2400:3200::1
阿里 223.6.6.6 2400:3200:baba::1
腾讯 119.29.29.29 2402:4e00::
CNNIC 1.2.4.8 2001:dc7:1000::1
CNNIC 210.2.4.8 2001:dc7:1000::1
上交 2001:da8:8000:1:202:120:2:100
上交 2001:da8:8000:1:202:120:2:101
Yeti 中科院网络信息中心 2001:cc0:2fff:1::6666
Yeti 北京交通大学 2001:da8:205:2060::188
Yeti 清华大学 2001:da8:ff:305:20c:29ff:fe1f:a92a
1.1.1.1 2606:4700:4700::1111
1.0.0.1 2606:4700:4700::1001
101.101.101.101 2001:de4::101
101.102.103.104 2001:de4::102
4.2.2.1 4.2.2.2
4.2.2.3 4.2.2.4
4.2.2.5 4.2.2.6
9.9.9.9

注意:表中标注为绿色的DNS是我的当前环境下ping时响应速度在1ms以内的DNS, 所以这几个是在北京使用时极力推荐的, 其他的地址请自行ping后做出选择。

2023年 06月 19日 星期一 11:35:15 CST, 查询到英语成绩,顺利通过考试,感谢林老师,感谢外教,感谢北师大。本网站本是我准备复习考试之用,现作为Gist 组英语纪念.

林敦来老师正在给我们上课

2022-2023春季学期,英语考试考试时间:2023-06-10 13:30-16:10 考试地点:2023春期末考试,考场安排17周查询,具体考试安排尚未公布,请及时关注:北师大公共外语研究部

考场安排已经公布:

  1. 我本人: 教九楼/104/50

  2. 其他人考场安排: 公布版.xlsx

  3. 2023年06月10日 顺利完成考试

  4. 预计7月2日公布成绩,愿我们都顺利通过考试!

  5. 2023年 06月 19日 星期一 公布成绩,我们顺利通过了考试。

阅读全文 »

Project 15.32 Overcoming critical slowing down

Many of the original applications of Monte Carlo methods were done for systems of approximately one hundred particles and lattices of order 322 spins. It would be instructive to redo many of these applications with much better statistics and with larger system sizes. In the following, we discuss some additional recent developments, but we have omitted other important topics such as Brownian dynamics and umbrella sampling. More ideas for projects can be found in the references.

Energy-Monte Carlo Step The Spin Configuration The Spin Configuration The Spin Configuration The Spin Configuration

ocsd4.py
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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Author: 冯振华
# StudentNumber: 202221140015
# Verdion: V4.0
# Copyright © 2023 feng <feng@arch>
#
# Distributed under terms of the MIT license.

import numpy as np
import matplotlib.pyplot as plt

# Define the parameters
N = 20 # number of spins in each direction
J = -1 # coupling constant
beta = 1 # inverse temperature
nsteps = 1000 # number of Monte Carlo steps

# Initialize the spins randomly
spins = np.random.choice([-1, 1], size=(N, N))

# Define the energy function
def energy(spins):
return -J * (np.sum(spins[:-1,:] * spins[1:,:]) + np.sum(spins[:,:-1] * spins[:,1:])) \
- J * (np.sum(spins[0,:] * spins[-1,:]) + np.sum(spins[:,0] * spins[:,-1]))

# Define the Metropolis algorithm
def metropolis(spins, beta):
for i in range(N):
for j in range(N):
# Choose a random spin to flip
x, y = np.random.randint(0, N), np.random.randint(0, N)
# Calculate the energy difference
delta_E = 2 * J * spins[x, y] * \
(spins[(x+1)%N, y] + spins[(x-1)%N, y] + spins[x, (y+1)%N] + spins[x, (y-1)%N])
# Flip the spin with probability according to the Boltzmann factor
if np.random.uniform(0, 1) < np.exp(-beta * delta_E):
spins[x, y] = -spins[x, y]
return spins

# Run the Monte Carlo simulation
energies = []
for i in range(nsteps):
spins = metropolis(spins, beta)
energies.append(energy(spins))

# Calculate the average energy
E = np.mean(energies)

# Print the result
print(f"Average energy: {E:.4f}")

# Plot the energy as a function of Monte Carlo steps
plt.plot(energies)
plt.xlabel("Monte Carlo step")
plt.ylabel("Energy")
plt.show()

# Plot the spin configuration
plt.imshow(spins, cmap='binary')
plt.show()
ocsd3.py
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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2023 feng <feng@archlinux>
#
# Distributed under terms of the MIT license.
"""
作者:冯振华
学号:202221140015
版本:V4.1
日期:2023年05月20日
项目:Monte Carlo methods for Overcoming critical slowing down
"""
import numpy as np
import matplotlib.pyplot as plt

def initialize_spins(N):
"""
Initializes the spin lattice with random spins of -1 or +1
"""
return np.random.choice([-1, 1], size=(N, N))

def bond_probability(beta):
"""
Calculates probability of forming a bond between two neighboring spins
"""
return 1 - np.exp(-2*beta)

def update_spins(spins, beta):
"""
a single update of the spins using the Wolff algorithm
"""
N = spins.shape[0]
visited = np.zeros((N, N), dtype=bool)
seed = (np.random.randint(N), np.random.randint(N))
cluster_spins = [seed]
cluster_perimeter = [(seed[0], (seed[1]+1)%N), (seed[0], (seed[1]-1)%N),
((seed[0]+1)%N, seed[1]), ((seed[0]-1)%N, seed[1])]
while len(cluster_perimeter) > 0:
i, j = cluster_perimeter.pop()
if not visited[i,j] and np.random.random() < bond_probability(beta):
cluster_spins.append((i, j))
visited[i, j] = True
cluster_perimeter += [(i, (j+1)%N), (i, (j-1)%N),
((i+1)%N, j), ((i-1)%N, j)]
spins[tuple(zip(*cluster_spins))] *= -1
return spins

def calculate_energy(spins,J):
"""
Calculates the energy of the spin configuration
"""
N = spins.shape[0]
energy = 0
for i in range(N):
for j in range(N):
spin = spins[i, j]
neighbors = spins[(i+1)%N, j] + spins[i, (j+1)%N] + spins[(i-1)%N, j] + spins[i, (j-1)%N]
energy += 2*J*spin * neighbors
return energy

def run_simulation(N=20, J=-1,beta=1.0, num_steps=2000, num_measurements=100):
"""
Performs a full simulation of the Wolff algorithm on the spin lattice
"""
spins = initialize_spins(N)
energies = []
mags = []
for step in range(num_steps):
spins = update_spins(spins, beta)
if step % (num_steps // num_measurements) == 0:
energy = calculate_energy(spins,J)
mag = np.sum(spins)
energies.append(energy)
mags.append(mag)
return energies, mags

energies, mags = run_simulation(N=20,J=-1,beta=1.0, num_steps=100000, num_measurements=100)
#
# Calculate the average energy
E = np.mean(energies)
# Print the result
print(f"Average energy: {E:.4f}")
# Plot the energy as a function of Monte Carlo steps
plt.plot(energies)
plt.xlabel("Monte Carlo step")
plt.ylabel("Energy")
plt.show()

# Plot the spin configuration
plt.plot(mags)
plt.xlabel("Monte Carlo step")
plt.ylabel("Spin")
plt.show()
"""
To run the simulation, simply the `run_simulation` function with the desired parameters, such as `energies, mags = run_simulation(N=30,=1.0, num_steps=100000, num_measurements=100)`. This will simulate 100,000 Wolff updates on a 30x30 lattice at a temperature of 1. and take measurements of the energy and magnetization every 1,000 steps. The output is two arrays, `energies` and `mags`, that record the energy and magnetization at each measurement step. These arrays can be used to averages and variances to analyze the system properties.
"""

Project 15.32 Overcoming critical slowing down