Skip to content

Commit bcc57c1

Browse files
Worked on the large memory use (#38)
* Worked on memory use * Reduced memory use * Fixed wrong number of moelcules in memory * Keep looking for inconsistencies * Fixed test * Fixed error in insert_and_orient_molecule * Improved reader prescan * Improved test * Improved test prescan * Minor improvement of prescanned files * minor improvement * Added tests for inputs * fixed troncated error
1 parent 3936d5f commit bcc57c1

24 files changed

+422
-229
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ bin/*
1010
# Ignore produced files
1111
**/outputs/*
1212
**/*.dat
13-
**/log.maniac
13+
**/log*maniac
1414

1515
# Ignore backup and temporary files (optional but useful)
1616
*~

mc-topology

run.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22
set -e # Exit on error
33

4-
case="ZIF8-CH4O"
4+
case="WIDOM"
55

66
base_energy="mc-topology/testcase-energy"
77
base_adsorption="mc-topology/testcase-adsorption"
@@ -16,10 +16,12 @@ case "$case" in
1616

1717
"WIDOM")
1818
folder="$base_widom/ZIF8-MET"
19+
# reservoir="$base_reservoir/CH4O"
1920
;;
2021

2122
"CO2")
2223
folder="$base_reservoir/CO2"
24+
reservoir="$base_reservoir/CO2"
2325
;;
2426

2527
"SLIT")

src/data_parser.f90

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ subroutine read_lammps_bonds(infile, data_file_name, box)
692692

693693
! If no bonds are expected, skip the whole routine
694694
if (box%num%bonds == 0) then
695-
call InfoMessage("No bonds expected in data file: " // trim(data_file_name))
695+
call info_message("No bonds expected in data file: " // trim(data_file_name))
696696
return
697697
end if
698698

@@ -702,7 +702,7 @@ subroutine read_lammps_bonds(infile, data_file_name, box)
702702

703703
if (ios < 0) then
704704
write(formatted_msg, '(A, A)') "No bonds found in data file: ", trim(data_file_name)
705-
call InfoMessage(trim(formatted_msg))
705+
call info_message(trim(formatted_msg))
706706
end if
707707

708708
if (ios > 0) then
@@ -790,7 +790,7 @@ subroutine read_lammps_angles(infile, data_file_name, box)
790790
! If no angles are expected, skip the whole routine
791791
if (box%num%angles == 0) then
792792
write(formatted_msg, '(A, A)') "No angles expected in data file: ", trim(data_file_name)
793-
call InfoMessage(trim(formatted_msg))
793+
call info_message(trim(formatted_msg))
794794
return
795795
end if
796796

@@ -800,7 +800,7 @@ subroutine read_lammps_angles(infile, data_file_name, box)
800800

801801
if (ios < 0) then
802802
write(formatted_msg, '(A, A)') "No angles found in data file: ", trim(data_file_name)
803-
call InfoMessage(trim(formatted_msg))
803+
call info_message(trim(formatted_msg))
804804
end if
805805

806806
if (ios > 0) then
@@ -895,7 +895,7 @@ subroutine read_lammps_dihedrals(infile, data_file_name, box)
895895

896896
if (ios < 0) then
897897
write(formatted_msg, '(A, A)') "No dihedrals found in data file: ", trim(data_file_name)
898-
call InfoMessage(trim(formatted_msg))
898+
call info_message(trim(formatted_msg))
899899
end if
900900

901901
if (ios > 0) then
@@ -993,7 +993,7 @@ subroutine read_lammps_impropers(infile, data_file_name, box)
993993
read(infile, '(A)', IOSTAT=ios) line
994994
if (ios < 0) then
995995
write(formatted_msg, '(A, A)') "No impropers found in data file: ", trim(data_file_name)
996-
call InfoMessage(trim(formatted_msg))
996+
call info_message(trim(formatted_msg))
997997
end if
998998
if (ios > 0) then
999999
write(formatted_msg, '(A, A)') "I/O error reading data file: ", trim(data_file_name)
@@ -1286,7 +1286,7 @@ subroutine detect_molecules(box)
12861286
! Global check: maximum allowed molecules
12871287
if (detected_nb_max_molecule > NB_MAX_MOLECULE) then
12881288
write(formatted_msg, '(A, I0)') "The number of molecules exceeds the maximum allowed = ", NB_MAX_MOLECULE
1289-
call InfoMessage(trim(formatted_msg))
1289+
call info_message(trim(formatted_msg))
12901290
write(formatted_msg, '(A, I0)') "Number of molecules found = ", detected_nb_max_molecule
12911291
call abort_run(trim(formatted_msg) // " Please increase 'NB_MAX_MOLECULE' in src/parameters.f90 and recompile.", 11)
12921292
end if

src/ewald_energy.f90

Lines changed: 69 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ subroutine compute_recip_energy_single_mol(res_type, mol_index, u_recipCoulomb_n
8282
logical :: creation_flag
8383
logical :: deletion_flag
8484

85+
! Ensure that the routine deals with guest, not host
86+
if (res%role(res_type) == TYPE_HOST) call abort_run("Inconsistence in fourier routine")
87+
8588
creation_flag = present_or_false(is_creation)
8689
deletion_flag = present_or_false(is_deletion)
8790

@@ -105,9 +108,9 @@ subroutine compute_recip_energy_single_mol(res_type, mol_index, u_recipCoulomb_n
105108
kz_idx = ewald%kvectors(idx)%kz
106109

107110
! Compute phase factors for the current residue
108-
ewald%phase%new(1:natoms) = ewald%phase%factor(1, res_type, mol_index, 1:natoms, kx_idx) * &
109-
ewald%phase%factor(2, res_type, mol_index, 1:natoms, ky_idx) * &
110-
ewald%phase%factor(3, res_type, mol_index, 1:natoms, kz_idx)
111+
ewald%phase%new(1:natoms) = ewald%phase%factor_guest(1, res_type, mol_index, 1:natoms, kx_idx) * &
112+
ewald%phase%factor_guest(2, res_type, mol_index, 1:natoms, ky_idx) * &
113+
ewald%phase%factor_guest(3, res_type, mol_index, 1:natoms, kz_idx)
111114

112115
ewald%phase%old(1:natoms) = ewald%phase%factor_old(1, 1:natoms, kx_idx) * &
113116
ewald%phase%factor_old(2, 1:natoms, ky_idx) * &
@@ -265,23 +268,69 @@ pure function compute_recip_amplitude(kx_idx, ky_idx, kz_idx) result(Ak)
265268

266269
! Loop over all residue types
267270
do res_type = 1, res%number
268-
! Loop over all molecules of this residue type
269-
do mol_index = 1, primary%num%residues(res_type)
270-
! Loop over sites in molecule
271-
do atom_index = 1, res%atom(res_type)
272-
273-
! Extract charge and phase product
274-
charges = primary%atoms%charges(res_type, atom_index)
275-
product = ewald%phase%factor(1, res_type, mol_index, atom_index, kx_idx) * &
276-
ewald%phase%factor(2, res_type, mol_index, atom_index, ky_idx) * &
277-
ewald%phase%factor(3, res_type, mol_index, atom_index, kz_idx)
278-
279-
! Accumulate contribution from this atom:
280-
! charge * exp(i k · r) factor in x, y, z directions
281-
Ak = Ak + charges * product
282-
283-
end do
284-
end do
271+
272+
! For host
273+
if (res%role(res_type) == TYPE_HOST) then
274+
275+
! Loop over all molecules of this residue type
276+
do mol_index = 1, primary%num%residues(res_type)
277+
! Loop over sites in molecule
278+
do atom_index = 1, res%atom(res_type)
279+
280+
! Extract charge and phase product
281+
charges = primary%atoms%charges(res_type, atom_index)
282+
product = ewald%phase%factor_host(1, res_type, mol_index, atom_index, kx_idx) * &
283+
ewald%phase%factor_host(2, res_type, mol_index, atom_index, ky_idx) * &
284+
ewald%phase%factor_host(3, res_type, mol_index, atom_index, kz_idx)
285+
286+
! Accumulate contribution from this atom:
287+
! charge * exp(i k · r) factor in x, y, z directions
288+
Ak = Ak + charges * product
289+
290+
end do
291+
end do
292+
293+
! For guest
294+
else if (res%role(res_type) == TYPE_GUEST) then
295+
296+
! Loop over all molecules of this residue type
297+
do mol_index = 1, primary%num%residues(res_type)
298+
! Loop over sites in molecule
299+
do atom_index = 1, res%atom(res_type)
300+
301+
! Extract charge and phase product
302+
charges = primary%atoms%charges(res_type, atom_index)
303+
product = ewald%phase%factor_guest(1, res_type, mol_index, atom_index, kx_idx) * &
304+
ewald%phase%factor_guest(2, res_type, mol_index, atom_index, ky_idx) * &
305+
ewald%phase%factor_guest(3, res_type, mol_index, atom_index, kz_idx)
306+
307+
! Accumulate contribution from this atom:
308+
! charge * exp(i k · r) factor in x, y, z directions
309+
Ak = Ak + charges * product
310+
311+
end do
312+
end do
313+
314+
end if
315+
316+
! ! Loop over all molecules of this residue type
317+
! do mol_index = 1, primary%num%residues(res_type)
318+
! ! Loop over sites in molecule
319+
! do atom_index = 1, res%atom(res_type)
320+
321+
! ! Extract charge and phase product
322+
! charges = primary%atoms%charges(res_type, atom_index)
323+
! product = ewald%phase%factor(1, res_type, mol_index, atom_index, kx_idx) * &
324+
! ewald%phase%factor(2, res_type, mol_index, atom_index, ky_idx) * &
325+
! ewald%phase%factor(3, res_type, mol_index, atom_index, kz_idx)
326+
327+
! ! Accumulate contribution from this atom:
328+
! ! charge * exp(i k · r) factor in x, y, z directions
329+
! Ak = Ak + charges * product
330+
331+
! end do
332+
! end do
333+
285334
end do
286335

287336
end function compute_recip_amplitude

src/ewald_phase.f90

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ module ewald_phase
1414
! Saves the current Fourier-space phase factors and reciprocal amplitudes
1515
! for a specific molecule or residue, enabling rollback after a rejected move.
1616
!--------------------------------------------------------------------
17-
subroutine save_single_mol_fourier_terms(residue_type, molecule_index, symmetrize_x)
17+
subroutine save_single_mol_fourier_terms(res_type, mol_index, symmetrize_x)
1818

1919
! Input arguments
20-
integer, intent(in) :: residue_type ! Residue type identifier
21-
integer, intent(in) :: molecule_index ! Molecule index to save
20+
integer, intent(in) :: res_type ! Residue type identifier
21+
integer, intent(in) :: mol_index ! Molecule index to save
2222
logical, intent(in), optional :: symmetrize_x ! Whether to save negative kx
2323

2424
! Local variables
@@ -32,24 +32,27 @@ subroutine save_single_mol_fourier_terms(residue_type, molecule_index, symmetriz
3232
do_sym = .false.
3333
if (present(symmetrize_x)) do_sym = symmetrize_x
3434

35+
! Ensure that the routine deals with guest, not host
36+
if (res%role(res_type) == TYPE_HOST) call abort_run("Inconsistence in fourier routine")
37+
3538
!----------------------------------------------------------
3639
! Save per-atom Fourier phase factors (IKX, IKY, IKZ)
3740
!----------------------------------------------------------
38-
do atom_index = 1, res%atom(residue_type)
41+
do atom_index = 1, res%atom(res_type)
3942

4043
do dim = 1, 3
4144

4245
do k_idx = 0, ewald%param%kmax(dim)
4346

4447
! Positive k
4548
ewald%phase%factor_old(dim, atom_index, k_idx) = &
46-
ewald%phase%factor(dim, residue_type, molecule_index, atom_index, k_idx)
49+
ewald%phase%factor_guest(dim, res_type, mol_index, atom_index, k_idx)
4750

4851
! Negative k (only if non-zero)
4952
if (k_idx /= 0) then
5053
if (dim /= 1 .or. do_sym) then
5154
ewald%phase%factor_old(dim, atom_index, -k_idx) = &
52-
ewald%phase%factor(dim, residue_type, molecule_index, atom_index, -k_idx)
55+
ewald%phase%factor_guest(dim, res_type, mol_index, atom_index, -k_idx)
5356
end if
5457
end if
5558

@@ -72,12 +75,12 @@ end subroutine save_single_mol_fourier_terms
7275
! Restores previously saved Fourier-space phase factors and reciprocal
7376
! amplitudes for a molecule or residue after a rejected move.
7477
!--------------------------------------------------------------------
75-
subroutine restore_single_mol_fourier(residue_type, molecule_index, symmetrize_x)
78+
subroutine restore_single_mol_fourier(res_type, mol_index, symmetrize_x)
7679

7780
! Input arguments
78-
integer, intent(in) :: residue_type ! Residue type index
79-
integer, intent(in) :: molecule_index ! Molecule index to restore
80-
logical, intent(in), optional :: symmetrize_x ! Restore negative kx if true
81+
integer, intent(in) :: res_type ! Residue type index
82+
integer, intent(in) :: mol_index ! Molecule index to restore
83+
logical, intent(in), optional :: symmetrize_x ! Restore negative kx if true
8184

8285
! Local variables
8386
integer :: atom_index_1 ! Atom index
@@ -90,23 +93,24 @@ subroutine restore_single_mol_fourier(residue_type, molecule_index, symmetrize_x
9093
do_sym = .false.
9194
if (present(symmetrize_x)) do_sym = symmetrize_x
9295

93-
!----------------------------------------------------------
96+
! Ensure that the routine deals with guest, not host
97+
if (res%role(res_type) == TYPE_HOST) call abort_run("Inconsistence in fourier routine")
98+
9499
! Restore per-atom Fourier phase factors (IKX, IKY, IKZ)
95-
!----------------------------------------------------------
96-
do atom_index_1 = 1, res%atom(residue_type)
100+
do atom_index_1 = 1, res%atom(res_type)
97101

98102
do dim = 1, 3
99103

100104
do k_idx = 0, ewald%param%kmax(dim)
101105

102106
! Positive k
103-
ewald%phase%factor(dim, residue_type, molecule_index, atom_index_1, k_idx) = &
107+
ewald%phase%factor_guest(dim, res_type, mol_index, atom_index_1, k_idx) = &
104108
ewald%phase%factor_old(dim, atom_index_1, k_idx)
105109

106110
! Negative k
107111
if (k_idx /= 0) then
108112
if (dim /= 1 .or. do_sym) then
109-
ewald%phase%factor(dim, residue_type, molecule_index, atom_index_1, -k_idx) = &
113+
ewald%phase%factor_guest(dim, res_type, mol_index, atom_index_1, -k_idx) = &
110114
ewald%phase%factor_old(dim, atom_index_1, -k_idx)
111115
end if
112116
end if
@@ -128,10 +132,10 @@ end subroutine restore_single_mol_fourier
128132
! Replaces the Fourier-space phase factors of one molecule (`index_1`)
129133
! with those from another molecule (`index_2`) of the same residue type.
130134
!--------------------------------------------------------------------
131-
subroutine replace_fourier_terms_single_mol(residue_type, index_1, index_2, symmetrize_x)
135+
subroutine replace_fourier_terms_single_mol(res_type, index_1, index_2, symmetrize_x)
132136

133137
! Input arguments
134-
integer, intent(in) :: residue_type ! Residue type index
138+
integer, intent(in) :: res_type ! Residue type index
135139
integer, intent(in) :: index_1 ! Destination molecule index
136140
integer, intent(in) :: index_2 ! Source molecule index
137141
logical, intent(in), optional :: symmetrize_x ! Copy negative kx if true
@@ -146,22 +150,25 @@ subroutine replace_fourier_terms_single_mol(residue_type, index_1, index_2, symm
146150
do_sym = .false.
147151
if (present(symmetrize_x)) do_sym = symmetrize_x
148152

153+
! Ensure that the routine deals with guest, not host
154+
if (res%role(res_type) == TYPE_HOST) call abort_run("Inconsistence in fourier routine")
155+
149156
! Restore IKX, IKY, IKZ from backups
150-
do atom_index_1 = 1, res%atom(residue_type)
157+
do atom_index_1 = 1, res%atom(res_type)
151158

152159
do dim = 1, 3
153160

154161
do k_idx = 0, ewald%param%kmax(dim)
155162

156163
! Always copy positive (and zero) k
157-
ewald%phase%factor(dim, residue_type, index_1, atom_index_1, k_idx) = &
158-
ewald%phase%factor(dim, residue_type, index_2, atom_index_1, k_idx)
164+
ewald%phase%factor_guest(dim, res_type, index_1, atom_index_1, k_idx) = &
165+
ewald%phase%factor_guest(dim, res_type, index_2, atom_index_1, k_idx)
159166

160167
! Copy negative k only when allowed (when d=1, only if symmetrize_x is true, or when d=2,3)
161168
if (k_idx /= 0) then
162169
if (dim /= 1 .or. do_sym) then
163-
ewald%phase%factor(dim, residue_type, index_1, atom_index_1, -k_idx) = &
164-
ewald%phase%factor(dim, residue_type, index_2, atom_index_1, -k_idx)
170+
ewald%phase%factor_guest(dim, res_type, index_1, atom_index_1, -k_idx) = &
171+
ewald%phase%factor_guest(dim, res_type, index_2, atom_index_1, -k_idx)
165172
end if
166173
end if
167174

@@ -178,17 +185,17 @@ end subroutine replace_fourier_terms_single_mol
178185
subroutine compute_all_fourier_terms()
179186

180187
! Local variables
181-
integer :: residue_type
182-
integer :: molecule_index
188+
integer :: res_type
189+
integer :: mol_index
183190

184191
! Loop over all residue types
185-
do residue_type = 1, res%number
192+
do res_type = 1, res%number
186193

187194
! Loop over all molecules of this residue type
188-
do molecule_index = 1, primary%num%residues(residue_type)
195+
do mol_index = 1, primary%num%residues(res_type)
189196

190197
! Compute Fourier terms for a single molecule
191-
call single_mol_fourier_terms(residue_type, molecule_index)
198+
call single_mol_fourier_terms(res_type, mol_index)
192199

193200
end do
194201
end do
@@ -230,11 +237,18 @@ subroutine single_mol_fourier_terms(res_type, mol_index)
230237
! along each Cartesian direction. These factors will be used repeatedly
231238
! in the reciprocal-space sum for the Ewald energy.
232239
do idim = 1, 3
240+
233241
call compute_phase_factor(ewald%phase%axis(:), local_phase(idim), ewald%param%kmax(idim))
234-
ewald%phase%factor(idim, res_type, mol_index, atom_index_1, &
235-
-ewald%param%kmax(idim):ewald%param%kmax(idim)) = ewald%phase%axis(:)
236-
end do
237242

243+
! Differentiate between host and guest
244+
if (res%role(res_type) == TYPE_HOST) then
245+
ewald%phase%factor_host(idim, res_type, mol_index, atom_index_1, &
246+
-ewald%param%kmax(idim):ewald%param%kmax(idim)) = ewald%phase%axis(:)
247+
else if (res%role(res_type) == TYPE_GUEST) then
248+
ewald%phase%factor_guest(idim, res_type, mol_index, atom_index_1, &
249+
-ewald%param%kmax(idim):ewald%param%kmax(idim)) = ewald%phase%axis(:)
250+
end if
251+
end do
238252
end do
239253

240254
end subroutine single_mol_fourier_terms

0 commit comments

Comments
 (0)