SudoDEM系列教程:从准备到安装

SudoDEM基于Linux平台开发,提供 Ubuntu发行版(14.04~18.04)的二进制安装(压缩)包,解压即可使用,无需root权限,不向系统核心目录写入文件,完全“绿色”。项目后期会提供Singularity容器的二进制包,用于任何Linux发行版。

if 你是编程新手 or Linux新手
  建议先尝试使用编译的二进制包
  请继续阅读本文
else:
  可直接下载源代码进行编译
  你可以快速跳过本文

如果你能非常容易地看懂上面代码块表达的意思,那么你可以继续往下看,否则,请直接拖到文章末尾点赞。

使用SudoDEM,你只需要掌握最基本的Linux系统知识和Python编程技巧。本文将介绍这些必备知识。首先,请准备Linux系统,推荐Ubuntu 14~18;你可以通过以下方案获得Linux系统:

  • Windows上安装虚拟机(Windows重度用户,体验Linux)
  • Windows 10开启Linux子系统(Windows重度用户,体验Linux)
  • 安装与Windows并存的多系统(需要重启电脑切换系统)
  • 安装单一系统,如深度Deepin(Windows轻度用户)
  • 安装单一系统,如Ubuntu(Linux重度用户)

[Linux最基础命令]

下面以Ubuntu系统为例(不同Linux发行版(如CentOS)命令可能稍有不同),介绍几个最为基本的命令和快捷方式:

  • 打开终端(terminal,即命令行窗口):CTRL+ALT+T
  • 安装软件到系统:sudo apt-get install package 或者sudo apt install package
  • 显示当前路径(工作目录):pwd
  • 改变路径(工作目录):cd directory

路径/目录directory可以是相对路径或绝对路径。

其中,绝对路径是以Linux根目录(/)开始,比如:/bin,/home/sway等。为了方便访问用户目录(如/home/sway/),系统给出了用户目录的缩写目录,表示为~;~就等价为前述/home/sway。注意, Linux系统下可以存在不同用户的用户目录,但~符号只表示当前登录用户(账号)的用户目录;并且,当前用户对其他用户的用户目录不具有访问权限。值得一提的是,Linux是天然多用户系统,即允许多个用户同时登录同一系统(但有各自的运行环境,这些配置保存在各自的用户目录下;注:“同时登录”不是访问同一桌面)。比如用户sway已经登录系统进行工作,用户SC则可以通过网络(如ssh远程登录命令)进入系统工作;不同用户互不干扰(除了抢占硬件资源,比如大家都在用SudoDEM跑计算)。 相对路径就是基于某一特定目录。严格意义上讲,基于根目录的路径也可以叫做相对路径。因为,永远没有绝对()。除了根目录标记(/)和用户目录标记(~)外,有两个特定的符号用于相对路径(一个点.和两个点..)。一个点(.)代表当前工作路径,也就是用pwd执行后看到的路径;两个点(..)则代表相对当前路径的上一级目录。 比如 cd ../sub表示切换路径到上一目录的子目录 sub下;cd ../../表示切换到上一目录的上一目录;cd ./sub切换到当前目录下的子目录 sub,当然一般直接敲cd sub即可。 运行可执行文件(程序)prog:prog or ./path/prog。假如prog可被终端识别,即prog已被加入到了系统搜索路径(一般是PATH环境变量),只需要敲可执行文件名即可(比如前述cd就是系统命令,已在系统默认搜索路径里)。否则,需要加prog的路径(这里./表示执行该文件)。

强制结束终端程序运行:CTRL+C

$> sudo apt−get install python−numpy
$> pwd
$> cd /home/
$> sudodem3d

[Python 2.7]

Python与C++混合编程是目前计算软件的主流趋势。其中,Python主要扮演接口调用作用,实现快速建模;而繁重的核心计算部分则由底层C++编写的计算模块实现。SudoDEM目前仍然使用Python 2.7版本,后续项目开发升级会使用Python 3.0+版本。需要指出的是,Python 3是Python 2的全面升级,并不向下兼容,但在SudoDEM中,我们只需要最基本的功能(基本语法依然一致)。以下Python脚本展示了最为基本的入门知识。

 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
import math # import the module math which includes some basic math functions

# we have numbers
a = 2 # integer number
b = 5
c = 2.0 # float number
d = a/b # the value is an integer
e = c/b # float number
print a, d, e # print the values of a, d and e to the terminal

#we have strings
a = 'abc' # we can assign any type of values to any type of variables
b = "this is a 'Python' script."
print a, b

#we have list, tuple and dict to store and manage the above basic primitives
c = [1,2,3,'ss'] # a list
d = (1,2,3,'ss') # a tuple
c[0] = 5 # change the first item in the list c, and the index starts from 0.
d[0] = 5 # failed! a tuple is constant, which is the distinguishing property from a list
d={1:22,2:33,4:'ssss','w':123} #a dict: 1, 2, 4 and 'w' are keys of the dict, and 22, 33, 'ssss' and 123 are the corresponding values.
print d[1], d[4], d['w']

# we initialize two objects c and d for storing data at a for loop later
c = list() # an empty list
d = dict() # an empty dict
#test expression
if 1>2:
    print "1 is greater than 2, really?"
else:
    if 2 in [1, 2, 3]: # if 2 is in the list [1, 2, 3]
        #ok, let's start a for loop
        for i in range(5): # equal to for i in [0, 1, 2, 3, 4]
            print i # you can see what's going on in the for loop
            # we append some data to a list c and a dict d
            c.append(math.cos(i)*10 + 2.0**5) # we append the value of 10*cos(i) + 2.0^5 to the list c. We use the math function cos from the module math.
            if i not in d.keys(): # if i is not a key or index of the dict d. 
                d[i] = str(i)

#we can capsule our commands by a function
def GoodJob(input_number, keyword1 = 1, keyword2 = True):
    if keyword2: # that is if keyword2 == True
        print "the input number is", input
    input_number += keyword1 # i.e., the value of input_number is updated to input_number + keyword1
    return input_number # we can return the value
    
# call the function
a = GoodJob(2) # a = 3, with print info 'the input number is 3'
b = GoodJob(3, keyword1 = 5, keyword2 = False) # b = 8, without print info.

安装SudoDEM

[二进制安装包]

在SudoDEM项目网站(https://sudodem.github.io)的Download页面下载最新的二进制安装包。或者直接到SudoDEM的Github仓库(https://github.com/SudoDEM/SudoDEM/releases)下载编译的二进制包。

将二进制包解压到本地目录(如用户目录/home/xxx/),得到以下文件目录(SudoDEM3D为例):

通过用户配置文件.bashrc(/home/xxx/.bashrc),将可执行文件sudodem3d的路径(/home/xxx/SudoDEM/bin)加入到环境变量PATH:

PATH=${PATH}:/home/xxx/SudoDEM/bin

[脚本自动编译安装]

Here is a video that may guide you to compile SudoDEM. You can copy the command lines directly from this video.

非root权限编译安装是SudoDEM推荐安装方式。与Windows显著不同的是,Linux下向系统安装软件,会把软件的各个部分安装到系统的对应目录。比如编译的二进制会安装到bin目录(如/bin),动态链接库(为了开发等原因会把独立功能的模块编译成单独的二进制文件库,主程序根据需要动态调用该库文件)安装到lib(如 /lib)等。安装简便,但卸载可能出现问题。用sudo权限向系统安装软件(一般通过sudo apt install package),往往会附加安装一些依赖库(已存在略过);在卸载软件时,如果选择同时卸载依赖库,那么可能导致其他软件不能正常工作(需要的依赖库被卸载)。选择非root权限安装,可以有许多好处:避免上述卸载问题、可以编译和使用特定版本的依赖库(不使用系统的)、系统更加轻量。 最新SudoDEM代码中提供了一键编译安装脚本(shell脚本),直接在终端执行即可自动下载第三方依赖库的源代码进行编译安装。首先,从SudoDEM代码仓库下载源代码,文件结构如下:

其中,文件INSTALL提供了详细安装说明,包括一键编译安装和分布手动编译安装。然后,新建第三方依赖库编译目录 3rdlib、SudoDEM2D和SudoDEM3D的编译目录build2d和build3d(按需)、SudoDEM的本地安装目录sudodeminstall,最终文件目录结构如下:

编译安装操作在scripts目录下进行,该目录包含了安装脚本如下:

第一步,安装第三方库:

./install3rdlibs.sh

如果当前系统没有安装必要的编译工具,该脚本会自动安装,并需要输入管理员密码。第三方库包括Boost、Eigen、libQGLViewer和Minieigen,SudoDEM2D和3D共用,只需编译安装一次。 第二步,安装SudoDEM程序(以SudoDEM3D为例):

./firstCompile_SudoDEM3D.sh

注意:该脚本只用于首次编译安装;如果更改了SudoDEM的代码,只需到编译目录(build2d或build3d)执行make install即可重新编译安装。如果在SudoDEM源代码文件目录中增加了cpp文件,则需要重新cmake (即cmake ../SudoDEM3D)。