fix: driven atoms now oscillate around their initial equilibrium position

Previously, apply_driving_force set absolute position to A*cos(2π f t + φ),
ignoring the atom's initial coordinates. For atoms not at the origin (e.g.,
atom 120 at x=119), this incorrectly forced them back toward the origin each
step, causing severe distortion and numerical explosion.

Fix: store each driven atom's initial position as eq_pos/eq_x/eq_y/eq_z at
load time; position is now eq + A*cos(2π f t + φ) in all four engines
(Python, C, C++, Fortran).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-13 07:23:25 +08:00
parent e62e536cee
commit d489222eaf
4 changed files with 98 additions and 14 deletions
+22 -5
View File
@@ -41,6 +41,7 @@ program dynamics_f90
double precision, allocatable :: drv_phi_x(:), drv_phi_y(:), drv_phi_z(:)
integer, allocatable :: drv_has_period(:)
double precision, allocatable :: drv_period_cycles(:)
double precision, allocatable :: drv_eq_x(:), drv_eq_y(:), drv_eq_z(:)
double precision, allocatable :: drv_freeze_x(:), drv_freeze_y(:), drv_freeze_z(:)
! 运行时位置/速度
@@ -82,11 +83,12 @@ program dynamics_f90
! 读取驱动力
n_drivers = 0
if (driving_force /= 0) then
call read_driver(input_dir, n_atoms, atom_ids, n_drivers, &
call read_driver(input_dir, n_atoms, atom_ids, pos_0, n_drivers, &
drv_atom_idx, drv_amp_x, drv_amp_y, drv_amp_z, &
drv_freq_x, drv_freq_y, drv_freq_z, &
drv_phi_x, drv_phi_y, drv_phi_z, &
drv_has_period, drv_period_cycles, &
drv_eq_x, drv_eq_y, drv_eq_z, &
drv_freeze_x, drv_freeze_y, drv_freeze_z)
end if
@@ -134,6 +136,7 @@ program dynamics_f90
drv_freq_x, drv_freq_y, drv_freq_z, &
drv_phi_x, drv_phi_y, drv_phi_z, &
drv_has_period, drv_period_cycles, &
drv_eq_x, drv_eq_y, drv_eq_z, &
drv_freeze_x, drv_freeze_y, drv_freeze_z)
end if
@@ -146,6 +149,7 @@ program dynamics_f90
drv_freq_x, drv_freq_y, drv_freq_z, &
drv_phi_x, drv_phi_y, drv_phi_z, &
drv_has_period, drv_period_cycles, &
drv_eq_x, drv_eq_y, drv_eq_z, &
drv_freeze_x, drv_freeze_y, drv_freeze_z)
end if
call apply_step(method, n, x, y, z, vx, vy, vz, masses, G, B, &
@@ -171,6 +175,7 @@ program dynamics_f90
drv_freq_x, drv_freq_y, drv_freq_z, &
drv_phi_x, drv_phi_y, drv_phi_z, &
drv_has_period, drv_period_cycles, &
drv_eq_x, drv_eq_y, drv_eq_z, &
drv_freeze_x, drv_freeze_y, drv_freeze_z)
end if
traj_x(s, :) = x; traj_y(s, :) = y; traj_z(s, :) = z
@@ -205,6 +210,7 @@ program dynamics_f90
deallocate(drv_freq_x, drv_freq_y, drv_freq_z)
deallocate(drv_phi_x, drv_phi_y, drv_phi_z)
deallocate(drv_has_period, drv_period_cycles)
deallocate(drv_eq_x, drv_eq_y, drv_eq_z)
deallocate(drv_freeze_x, drv_freeze_y, drv_freeze_z)
end if
@@ -804,14 +810,16 @@ end subroutine apply_step
! ========================================================================
! 读取驱动力 driver.txt
! ========================================================================
subroutine read_driver(input_dir, n_atoms, atom_ids, n_drivers, &
subroutine read_driver(input_dir, n_atoms, atom_ids, pos_0, n_drivers, &
drv_atom_idx, drv_amp_x, drv_amp_y, drv_amp_z, &
drv_freq_x, drv_freq_y, drv_freq_z, &
drv_phi_x, drv_phi_y, drv_phi_z, &
drv_has_period, drv_period_cycles, &
drv_eq_x, drv_eq_y, drv_eq_z, &
drv_freeze_x, drv_freeze_y, drv_freeze_z)
character(len=*), intent(in) :: input_dir
integer, intent(in) :: n_atoms, atom_ids(n_atoms)
double precision, intent(in) :: pos_0(n_atoms, 3)
integer, intent(out) :: n_drivers
integer, allocatable, intent(out) :: drv_atom_idx(:)
double precision, allocatable, intent(out) :: drv_amp_x(:), drv_amp_y(:), drv_amp_z(:)
@@ -819,6 +827,7 @@ subroutine read_driver(input_dir, n_atoms, atom_ids, n_drivers, &
double precision, allocatable, intent(out) :: drv_phi_x(:), drv_phi_y(:), drv_phi_z(:)
integer, allocatable, intent(out) :: drv_has_period(:)
double precision, allocatable, intent(out) :: drv_period_cycles(:)
double precision, allocatable, intent(out) :: drv_eq_x(:), drv_eq_y(:), drv_eq_z(:)
double precision, allocatable, intent(out) :: drv_freeze_x(:), drv_freeze_y(:), drv_freeze_z(:)
character(len=512) :: path, line, period_str
@@ -831,6 +840,7 @@ subroutine read_driver(input_dir, n_atoms, atom_ids, n_drivers, &
double precision :: pxx_tmp(MX), pyy_tmp(MX), pzz_tmp(MX)
double precision :: pc_tmp(MX)
double precision :: fzx_tmp(MX), fzy_tmp(MX), fzz_tmp2(MX)
double precision :: eqx_tmp(MX), eqy_tmp(MX), eqz_tmp(MX)
n_drivers = 0
path = trim(input_dir) // '/driver.txt'
@@ -859,6 +869,9 @@ subroutine read_driver(input_dir, n_atoms, atom_ids, n_drivers, &
if (idx < 0) cycle
idx_tmp(n_drivers) = idx - 1 ! 0-based index
eqx_tmp(n_drivers) = pos_0(idx, 1)
eqy_tmp(n_drivers) = pos_0(idx, 2)
eqz_tmp(n_drivers) = pos_0(idx, 3)
ax_tmp(n_drivers) = ax; ay_tmp(n_drivers) = ay; az_tmp(n_drivers) = az
fxx_tmp(n_drivers) = fx; fyy_tmp(n_drivers) = fy; fzz_tmp(n_drivers) = fz
! degrees to radians
@@ -886,6 +899,7 @@ subroutine read_driver(input_dir, n_atoms, atom_ids, n_drivers, &
allocate(drv_freq_x(n_drivers), drv_freq_y(n_drivers), drv_freq_z(n_drivers))
allocate(drv_phi_x(n_drivers), drv_phi_y(n_drivers), drv_phi_z(n_drivers))
allocate(drv_has_period(n_drivers), drv_period_cycles(n_drivers))
allocate(drv_eq_x(n_drivers), drv_eq_y(n_drivers), drv_eq_z(n_drivers))
allocate(drv_freeze_x(n_drivers), drv_freeze_y(n_drivers), drv_freeze_z(n_drivers))
drv_atom_idx = idx_tmp(1:n_drivers)
@@ -894,6 +908,7 @@ subroutine read_driver(input_dir, n_atoms, atom_ids, n_drivers, &
drv_phi_x = pxx_tmp(1:n_drivers); drv_phi_y = pyy_tmp(1:n_drivers); drv_phi_z = pzz_tmp(1:n_drivers)
drv_has_period = per_tmp(1:n_drivers)
drv_period_cycles = pc_tmp(1:n_drivers)
drv_eq_x = eqx_tmp(1:n_drivers); drv_eq_y = eqy_tmp(1:n_drivers); drv_eq_z = eqz_tmp(1:n_drivers)
drv_freeze_x = fzx_tmp(1:n_drivers); drv_freeze_y = fzy_tmp(1:n_drivers); drv_freeze_z = fzz_tmp2(1:n_drivers)
write(*, '("[Fortran-engine] 已加载驱动力: ", i0, " 条定义")') n_drivers
@@ -906,6 +921,7 @@ subroutine apply_driving(n, x, y, z, vx, vy, vz, t, step, dt, &
drv_freq_x, drv_freq_y, drv_freq_z, &
drv_phi_x, drv_phi_y, drv_phi_z, &
drv_has_period, drv_period_cycles, &
drv_eq_x, drv_eq_y, drv_eq_z, &
drv_freeze_x, drv_freeze_y, drv_freeze_z)
integer, intent(in) :: n, step, n_drivers
integer, intent(in) :: drv_atom_idx(n_drivers), drv_has_period(n_drivers)
@@ -915,6 +931,7 @@ subroutine apply_driving(n, x, y, z, vx, vy, vz, t, step, dt, &
double precision, intent(in) :: drv_freq_x(n_drivers), drv_freq_y(n_drivers), drv_freq_z(n_drivers)
double precision, intent(in) :: drv_phi_x(n_drivers), drv_phi_y(n_drivers), drv_phi_z(n_drivers)
double precision, intent(in) :: drv_period_cycles(n_drivers)
double precision, intent(in) :: drv_eq_x(n_drivers), drv_eq_y(n_drivers), drv_eq_z(n_drivers)
double precision, intent(inout) :: drv_freeze_x(n_drivers), drv_freeze_y(n_drivers), drv_freeze_z(n_drivers)
integer :: d, idx, period_steps
@@ -941,9 +958,9 @@ subroutine apply_driving(n, x, y, z, vx, vy, vz, t, step, dt, &
end if
end if
px = drv_amp_x(d) * cos(TWO_PI * drv_freq_x(d) * t + drv_phi_x(d))
py = drv_amp_y(d) * cos(TWO_PI * drv_freq_y(d) * t + drv_phi_y(d))
pz = drv_amp_z(d) * cos(TWO_PI * drv_freq_z(d) * t + drv_phi_z(d))
px = drv_eq_x(d) + drv_amp_x(d) * cos(TWO_PI * drv_freq_x(d) * t + drv_phi_x(d))
py = drv_eq_y(d) + drv_amp_y(d) * cos(TWO_PI * drv_freq_y(d) * t + drv_phi_y(d))
pz = drv_eq_z(d) + drv_amp_z(d) * cos(TWO_PI * drv_freq_z(d) * t + drv_phi_z(d))
vpx = -drv_amp_x(d) * TWO_PI * drv_freq_x(d) * sin(TWO_PI * drv_freq_x(d) * t + drv_phi_x(d))
vpy = -drv_amp_y(d) * TWO_PI * drv_freq_y(d) * sin(TWO_PI * drv_freq_y(d) * t + drv_phi_y(d))
vpz = -drv_amp_z(d) * TWO_PI * drv_freq_z(d) * sin(TWO_PI * drv_freq_z(d) * t + drv_phi_z(d))