【问题标题】:1D Heat Equation using DFT produces incorrect results (FFTW)使用 DFT 的一维热方程产生不正确的结果 (FFTW)
【发布时间】:2021-01-17 04:19:53
【问题描述】:

我正在尝试使用复数到复数 IDFT 求解一维热方程。问题是单个时间步后的输出似乎不正确。我在下面提供了一个简单的示例来说明问题。

我初始化温度状态如下:

频域的初始模式为:
k[ 0] = 12.5 + 0i
k[ 1] = 12.5 + 0i
k[ 2] = 12.5 + 0i
k[ 3] = 12.5 + 0i
k[ 4] = 12.5 + 0i
k[-3] = 12.5 + 0i
k[-2] = 12.5 + 0i
k[-1] = 12.5 + 0i

然后我使用标准一维热方程将频域状态推进到t=0.02:

double alpha = 0.2; // Thermal conductivity constant
double timestep = 0.02;

for (int i = 0; i < N; i++) {
    int k = (i <= N / 2) ? i : i - N;

    F[i][REAL] *= exp(-alpha * k * k * timestep); // Decay the real part
    F[i][IMAG] *= exp(-alpha * k * k * timestep); // Decay the imaginary part
}

t=0.02处的频率模式变为:
k[ 0] = 12.5 + 0i
k[ 1] = 12.45 + 0i
k[ 2] = 12.3 + 0i
k[ 3] = 12.06 + 0i
k[ 4] = 11.73 + 0i
k[-3] = 12.06 + 0i
k[-2] = 12.3 + 0i
k[-1] = 12.45 + 0i

在执行 IDFT 以获取 t=0.02 处的温度域状态后,我得到:

空间域和频域似乎都是正确的周期性。然而,热量(空间域中的值)似乎没有按照高斯曲线消散。更令人惊讶的是,一些温度低于其初始值(它们变为负值!)。

能量守恒似乎是正确的:将所有温度加在一起仍然是 100。

这是我的完整热方程代码:

double alpha = 0.2;     // Thermal conductivity constant
double timestep = 0.02; // Physical heat equation timestep
int N = 8;              // Number of data points

fftw_complex* T = (fftw_complex*)fftw_alloc_complex(N); // Temperature domain
fftw_complex* F = (fftw_complex*)fftw_alloc_complex(N); // Frequency domain

fftw_plan plan = fftw_plan_dft_1d(N, F, T, FFTW_BACKWARD, FFTW_MEASURE); // IDFT from frequency to temperature domain

// Initialize all frequency modes such that there is a peak of 100 at x=0 in the temperature domain
// All other other points in the temperature domain are 0
for (int i = 0; i < N; i++) {
    F[i][REAL] = 100.0 / N;
    F[i][IMAG] = 0.0;
}

// Perform the IDFT to obtain the initial state in the temperature domain
fftw_execute(plan);
printTime1d(T, N);
printFrequencies1d(F, N);

// Perform a single timestep of the heat equation to obtain the frequency domain state at t=0.02
for (int i = 0; i < N; i++) {
    int k = (i <= N / 2) ? i : i - N;

    F[i][REAL] *= exp(-alpha * k * k * timestep); // Decay the real part
    F[i][IMAG] *= exp(-alpha * k * k * timestep); // Decay the imaginary part
}

// Perform the IDFT to obtain the temperature domain state at t=0.02
fftw_execute(plan);
printTime1d(T, N);
printFrequencies1d(F, N);

printTime(...)和printFrequencies(...)的定义是:

void printTime1d(fftw_complex* data, int N) {
    int rounding_factor = pow(10, 2);

    for (int i = 0; i < N; i++) {
        std::cout << std::setw(8) << round(data[i][REAL] * rounding_factor) / rounding_factor;
    }

    std::cout << std::endl;
}

void printFrequencies1d(fftw_complex* data, int N) {
    int rounding_factor = pow(10, 2);

    for (int i = 0; i < N; i++) {
        int k = (i <= N / 2) ? i : i - N;

        double R = round(data[i][REAL] * rounding_factor) / rounding_factor;
        double I = round(data[i][IMAG] * rounding_factor) / rounding_factor;

        std::cout << "k[" << std::setw(2) << k << "]: " << std::setw(2) << R << ((I < 0) ? " - " : " + ") << std::setw(1) << abs(I) << "i" << std::endl;
    }

    std::cout << std::endl;
}

也许值得注意的是,我还使用复杂到真实的 IDFT(使用 fftw 的 fftw_plan_dft_c2r_1d())进行了这个实验,结果完全相同。

【问题讨论】:

  • 在你的代码中,我没有看到你做过 forward 转换。只是一个落后的。这是故意的吗?
  • 这是故意的。在实际程序中,我希望能够进行正向变换(因此是 c2c 计划),但对于重现问题,这不是必需的。
  • 在您的第一个 for 循环中,您似乎正在初始化 spatial(温度)域中的值。然而上面的评论却另有说明。
  • 哦,我的错。对不起。
  • 这确实是一个奇怪的结果。我认为这与在频域中切割高斯有关,但我现在不确定。我得做一些实验。

标签: c++ fftw dft


【解决方案1】:

您的问题是您没有解决所需的频率,而是在乘以衰减系数后得到以下函数的傅立叶图像:

上面的结果与你应该得到的结果相差甚远——一个高斯——至少是这样的(使用 80 点而不是 8 点):

请注意,上面第一张图表中的幅度甚至没有任何机会接近零,而是撞上了奈奎斯特频率。很明显,您会得到类似于吉布斯现象的伪影:这是傅立叶部分和的通常行为。

80点数据版本的傅里叶逆变换如下:

这个结果仍然有负分量(因为我们使用了有限数量的谐波),但它们的幅度远比只有 8 个谐波得到的要小。

请注意,这确实意味着,如果您增加您感兴趣的时间值,您可以减少考虑的谐波数量。起初这可能出乎意料,但这仅仅是因为高次谐波比低次谐波衰减得快得多,而且它们永远不会再增加。

【讨论】:

  • 非常感谢您的回答!如果我理解正确,这种现象是由于数据点数量非常有限,而不是由于代码错误?在实践中,我的程序将使用大量数据点,然后应该对此进行补救?
  • @Naemesis 是的,这是由于数据点数量不足。代码逻辑本身的正确性似乎还可以,因为我已经成功地在完全不同的环境中重现了这些值:Wolfram Mathematica。
  • 非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-14
  • 2015-10-12
  • 2015-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-30
相关资源
最近更新 更多