论文研究手记(2):创建单独的时间控制程序

,

·

论文研究手记

配置文件时间控制模块调用示例 三部分,基于 Python + NumPy 实现,代码完整可直接运行。


一、第一步:创建 config_time.json 配置文件

在项目同级目录新建 config_time.json,存储仿真时间参数:

{
  "total_simulation_time": 100,
  "time_step": 0.5
}
  • total_simulation_time:仿真总时长(整型)
  • time_step:仿真步长(浮点型)

二、第二步:创建 time_control.py 时间控制模块

实现读取配置、仿真时间记录、时间序列生成、对外接口,代码注释详细:

import json
import numpy as np
import os


class SimTimeControl:
    """仿真时间控制类"""
    def __init__(self):
        # 1. 加载json配置文件
        self._load_config()

        # 2. 从配置读取参数
        self.total_time: int = self.config["total_simulation_time"]  # 总时长 int
        self.time_step: float = self.config["time_step"]             # 步长 float

        # 3. 当前仿真时间(初始化为0,用于实时记录)
        self.current_time: float = 0.0

        # 4. 生成完整时间序列 t = np.arange(0, 总时长, 步长)
        self.time_array = np.arange(0, self.total_time, self.time_step)

    def _load_config(self):
        """私有方法:加载config_time.json配置"""
        config_path = "config_time.json"
        if not os.path.exists(config_path):
            raise FileNotFoundError("未找到 config_time.json 配置文件!")

        with open(config_path, "r", encoding="utf-8") as f:
            self.config = json.load(f)

    # ========== 对外接口 ==========
    def get_current_time(self) -> float:
        """接口1:获取当前仿真时间"""
        return self.current_time

    def update_current_time(self, delta: float):
        """接口2:更新当前仿真时间(单步推进)"""
        self.current_time += delta

    def get_total_time(self) -> int:
        """接口3:获取仿真总时长"""
        return self.total_time

    def get_time_step(self) -> float:
        """接口4:获取仿真步长"""
        return self.time_step

    def get_time_array(self) -> np.ndarray:
        """接口5:获取完整时间序列数组"""
        return self.time_array


# 全局实例(方便外部直接导入使用)
time_controller = SimTimeControl()

模块功能说明

  1. 配置读取:自动加载 config_time.json,文件不存在则抛出异常;
  2. 参数存储:总时长(int)、步长(float)严格匹配类型要求;
  3. 当前仿真时间current_time 变量记录,支持手动更新;
  4. 时间序列:通过 np.arange(0, 总时长, 步长) 生成时间轴;
  5. 对外接口:提供读取/更新时间、获取参数、获取时间数组的公开方法。

三、第三步:外部调用示例(test_demo.py)

演示如何在其他代码中使用 time_control.py 的接口:

# 导入时间控制全局实例
from time_control import time_controller

if __name__ == "__main__":
    # 1. 读取基础参数
    print("仿真总时长:", time_controller.get_total_time())
    print("仿真步长:", time_controller.get_time_step())

    # 2. 获取完整时间序列
    t_arr = time_controller.get_time_array()
    print("时间序列前5个值:", t_arr[:5])

    # 3. 实时更新并查看当前仿真时间
    print("初始当前时间:", time_controller.get_current_time())
    # 单步推进时间
    time_controller.update_current_time(time_controller.get_time_step())
    print("推进一步后当前时间:", time_controller.get_current_time())

四、项目目录结构

保证文件在同一文件夹下,结构如下:

项目文件夹/
├─ config_time.json   # 时间配置文件
├─ time_control.py    # 时间控制核心模块
└─ test_demo.py       # 测试调用文件

五、关键补充说明

  1. 类型保证
    config_time.jsontotal_simulation_time 写整数,读取后强制为 inttime_step 写小数,读取后为 float,符合需求。
  2. 时间推进逻辑
    仿真循环中可反复调用 update_current_time(步长) 实现仿真时间逐帧运行。
  3. 路径问题(进阶)
    如果配置文件和代码不在同级目录,可修改 _load_config 中的 config_path绝对路径
  4. 依赖安装
    需提前安装 numpy:
   pip install numpy

六、运行效果

执行 test_demo.py 输出示例:

仿真总时长: 100
仿真步长: 0.5
时间序列前5个值: [0.  0.5 1.  1.5 2. ]
初始当前时间: 0.0
推进一步后当前时间: 0.5

Comments

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注