(更新中)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
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@realXuan

请博主喝一杯茶吧~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值