Loading

(更新中)gprMax项目代码分解:gprMax.constants.py、gprMax.exceptions

1. 引言

本文对gprMax项目中的“gprMax.constants.py”、“gprMax.exceptions.py”文件代码进行了逻辑梳理和分解。

2. gprMax.constants.py

gprMax.constants.py内容主要用于定义一些常量和数据类型。其中的常量cm0e0从scipy库导入的,可详见Constants (scipy.constants) — SciPy v1.10.0 Manual;常量z0是通过简单的平方根运算得到的。数据类型的定义也很直观,均是通过直接赋值的方式指定的。

import numpy as np
from scipy.constants import c
from scipy.constants import mu_0 as m0
from scipy.constants import epsilon_0 as e0

#定义变量z0
# Impedance of free space (Ohms)
z0 = np.sqrt(m0 / e0)

#以下为定义数据类型
# Single precision
# For numpy arrays
floattype = np.float32
complextype = np.complex64

# For C (CUDA) arrays
cudafloattype = 'float'
cudacomplextype = 'pycuda::complex<float>'

# Double precision
# For numpy arrays
# floattype = np.float64
# complextype = np.complex128

# For C (CUDA) arrays
# cudafloattype = 'double'
# cudacomplextype = 'pycuda::complex<double>'

3. gprMax.exceptions.py

colorama库用于自定义命令行文本显示颜色。Generalerror类和CmdinputErrorl类都继承自ValueError类,GeneralError类用于处理一般错误,CmdinputErrorl用于处理用户输入命令错误

#导入函数库
import sys
from colorama import init
from colorama import Fore

init()

sys.tracebacklimit = None


class GeneralError(ValueError):
	""""此类用于处理一般错误,继承自ValueError。"""
    """Handles general errors. Subclasses the ValueError class."""

    def __init__(self, message, *args):

        self.message = message
        super(GeneralError, self).__init__(message, *args)
        print(Fore.RED)


class CmdInputError(ValueError):
	""""此类用于处理用户输入命令错误,继承自ValueError。"""
    """Handles errors in user specified commands. Subclasses the ValueError class."""

    def __init__(self, message, *args):

        self.message = message
        super(CmdInputError, self).__init__(message, *args)
        print(Fore.RED)

4. 总结

本文分解了gprMax项目中的gprMax.constants.py、gprMax.exceptions.py文件代码。

Reference

  1. GitHub - gprMax/gprMax: gprMax is open source software that simulates electromagnetic wave propagation using the Finite-Difference Time-Domain (FDTD) method for numerical modelling of Ground Penetrating Radar (GPR)
  2. SciPy documentation — SciPy v1.10.0 Manual
posted @ 2023-01-12 11:56  realXuan  阅读(54)  评论(0编辑  收藏  举报  来源