Re: [lammps-users] Compile Extra Modules

Hi All
I have one extra module for "region_triangle.cpp",
"region_triangle.h" as written by Steve very long ago. I am trying to
integrate this region triangle by compiling. I have tried with both
make and cmake. But it's not being added up to the executable. Any
help will be appreciated. Thank you in advance.

Thank you.

Regards
Raj.

All you should have to do for traditional make is to add the *.cpp and *.h file to the src dir
and type “make serial” or “make mpi” or whatever target you normally use.

Have you tried that - does it give any compile/link errors?
Since these are older files, do 3 lines like this appear at the top or the region_tri.h file?

#ifdef REGION_CLASS
RegionStyle(block,RegBlock);
#else

Steve

Hi Steve,

I am having similar issues (with a new pair style I implemented) where I add the new files (.cpp and .h) to the src folder, rebuild LAMMPS with cmake following the instructions in the documentation, and the new style is also not recognized by LAMMPS when I try to run a simple test. I do have those three lines in my header file, in my case:

#ifdef PAIR_CLASS
PairStyle(mystyle,PairMYSTYLE)
#else

Any other tips you may have would be greatly appreciated.

Thank you so much for your time and help,
Al

With CMake you have rerun cmake. once in the build folder to update the list of files to compile. Of course you also have to carefully watch out for compilation and linker errors and use the correct executable.

Thanks for your prompt reply Dr. Kohlmeyer. I really appreciate it!

Unfortunately, I believe I am doing all of that (getting no errors) and it is still not working. I’ll keep trying. Raj, if you find a solution to your issue, please let me know. I’ll keep you posted as well.

Stay safe, and the kindest regards,
Al

Al,

you must be doing something wrong that you are not reporting. here is a quick demonstration.

I am attaching two files which are the lj/cut pair style from LAMMPS but renamed to be a new pair style “mything” implemented as a class PairMyThing so you can try for yourself.
I have a (CMake based) build folder where I have a successfully built LAMMPS compile, so that when I type “make” I get the following:

...
[ 10%] Generating includes/lammps/kspace.h
[ 10%] Built target kspace.h
[ 10%] Generating includes/lammps/lattice.h
[ 10%] Built target lattice.h
[ 10%] Generating C++ sources with cythonize...
[ 10%] Generating C++ sources with cythonize...
[100%] Built target lammps
[100%] Built target lmp

Now I copy the new files to the src folder and rerun cmake and then type “make” again:

...
[ 8%] Generating includes/lammps/kspace.h
[ 8%] Built target kspace.h
[ 8%] Generating includes/lammps/lattice.h
[ 8%] Built target lattice.h
[ 9%] Generating C++ sources with cythonize...
Consolidate compiler generated dependencies of target lammps
[ 9%] Building CXX object CMakeFiles/lammps.dir/home/akohlmey/compile/lammps/src/force.cpp.o
[ 9%] Building CXX object CMakeFiles/lammps.dir/home/akohlmey/compile/lammps/src/lammps.cpp.o
[ 9%] Building CXX object CMakeFiles/lammps.dir/home/akohlmey/compile/lammps/src/pair_mything.cpp.o
[ 9%] Generating C++ sources with cythonize...
[ 9%] Linking CXX shared library liblammps.so
[100%] Built target lammps
Consolidate compiler generated dependencies of target lmp
[100%] Linking CXX executable lmp
[100%] Built target lmp

Clearly the newly added source code was compiled and looking at the output of “./lmp -h” confirms that there is a pair style mything:

...
* Pair styles:

adp adp/omp agni agni/omp airebo
airebo/intel airebo/morse airebo/morse/intel airebo/morse/omp
airebo/omp atm awpmd/cut beck beck/omp

...
morse/smooth/linear morse/smooth/linear/omp morse/soft
multi/lucy multi/lucy/rx multi/lucy/rx/kk
multi/lucy/rx/kk/device multi/lucy/rx/kk/host mything
nb3b/harmonic nm/cut nm/cut/coul/cut nm/cut/coul/cut/omp
nm/cut/coul/long nm/cut/coul/long/omp nm/cut/omp

And I can run the also attached modified version of the LJ benchmark input with the new executable where it shows the name of the pair style in the neighbor list summary:

...
Neighbor list info ...
update every 20 steps, delay 0 steps, check no
max neighbors/atom: 2000, page size: 100000
master list distance cutoff = 2.8
ghost atom cutoff = 2.8
binsize = 1.4, bins = 24 24 24
1 neighbor lists, perpetual/occasional/extra = 1 0 0
(1) pair mything, perpetual
attributes: half, newton on
pair build: half/bin/atomonly/newton
stencil: half/bin/3d
bin: standard

...

So, either you are not doing one of these steps correctly, or your source code is not correct, or you are not using the correct executable.
You can check with the attached files that I used for this demonstration…

pair_mything.h (2.1 KB)

pair_mything.cpp (20.1 KB)

in.mything (471 Bytes)

here is the same demo for the conventional make. first the recompile/relink without the new files:

$ make serial
make[1]: Entering directory '/home/akohlmey/compile/lammps/src/STUBS'
make[1]: 'libmpi_stubs.a' is up to date.
make[1]: Leaving directory '/home/akohlmey/compile/lammps/src/STUBS'
Gathering installed package information (may take a little while)
make[1]: Entering directory '/home/akohlmey/compile/lammps/src'
Gathering git version information
make[1]: Leaving directory '/home/akohlmey/compile/lammps/src'
Compiling LAMMPS for machine serial
make[1]: Entering directory '/home/akohlmey/compile/lammps/src/Obj_serial'
make[1]: Leaving directory '/home/akohlmey/compile/lammps/src/Obj_serial'
make[1]: Entering directory '/home/akohlmey/compile/lammps/src/Obj_serial'
g++ -g -O main.o -L../STUBS -L. -llammps_serial -lmpi_stubs -o ../lmp_serial
size ../lmp_serial
text data bss dec hex filename
4828905 11752 944 4841601 49e081 ../lmp_serial
make[1]: Leaving directory '/home/akohlmey/compile/lammps/src/Obj_serial'

And there the screen output after adding the new files and typing “make serial” again:

$ make serial
make[1]: Entering directory '/home/akohlmey/compile/lammps/src/STUBS'
make[1]: 'libmpi_stubs.a' is up to date.
make[1]: Leaving directory '/home/akohlmey/compile/lammps/src/STUBS'
Gathering installed package information (may take a little while)
make[1]: Entering directory '/home/akohlmey/compile/lammps/src'
Gathering git version information
make[1]: Leaving directory '/home/akohlmey/compile/lammps/src'
Compiling LAMMPS for machine serial
make[1]: Entering directory '/home/akohlmey/compile/lammps/src/Obj_serial'
make[1]: Leaving directory '/home/akohlmey/compile/lammps/src/Obj_serial'
make[1]: Entering directory '/home/akohlmey/compile/lammps/src/Obj_serial'
g++ -g -O3 -DLAMMPS_GZIP -DLAMMPS_MEMALIGN=64 -I../STUBS -c ../force.cpp
g++ -g -O3 -DLAMMPS_GZIP -DLAMMPS_MEMALIGN=64 -I../STUBS -c ../lammps.cpp
g++ -g -O3 -DLAMMPS_GZIP -DLAMMPS_MEMALIGN=64 -I../STUBS -c ../pair_mything.cpp
ar -rc ../liblammps_serial.a angle.o angle_deprecated.o angle_hybrid.o angle_zero.o arg_info.o atom.o atom_map.o atom_vec_atomic.o atom_vec_body.o atom_vec_charge.o atom_vec.o atom_vec_ellipsoid.o atom_vec_hybrid.o atom_vec_line.o atom_vec_sphere.o atom_vec_tri.o balance.o body.o bond.o bond_deprecated.o bond_hybrid.o bond_zero.o change_box.o citeme.o comm_brick.o comm.o comm_tiled.o compute_aggregate_atom.o compute_angle.o compute_angle_local.o compute_angmom_chunk.o compute_bond.o compute_bond_local.o compute_centro_atom.o compute_centroid_stress_atom.o compute_chunk_atom.o compute_chunk_spread_atom.o compute_cluster_atom.o compute_cna_atom.o compute_com_chunk.o compute_com.o compute_coord_atom.o compute.o compute_deprecated.o compute_dihedral.o compute_dihedral_local.o compute_dipole_chunk.o compute_dipole.o compute_displace_atom.o compute_erotate_sphere_atom.o compute_erotate_sphere.o compute_fragment_atom.o compute_global_atom.o compute_group_group.o compute_gyration_chunk.o compute_gyration.o compute_heat_flux.o compute_improper.o compute_improper_local.o compute_inertia_chunk.o compute_ke_atom.o compute_ke.o compute_msd_chunk.o compute_msd.o compute_omega_chunk.o compute_orientorder_atom.o compute_pair.o compute_pair_local.o compute_pe_atom.o compute_pe.o compute_pressure.o compute_property_atom.o compute_property_chunk.o compute_property_local.o compute_rdf.o compute_reduce_chunk.o compute_reduce.o compute_reduce_region.o compute_slice.o compute_stress_atom.o compute_temp_chunk.o compute_temp_com.o compute_temp.o compute_temp_deform.o compute_temp_partial.o compute_temp_profile.o compute_temp_ramp.o compute_temp_region.o compute_temp_sphere.o compute_torque_chunk.o compute_vacf.o compute_vcm_chunk.o create_atoms.o create_bonds.o create_box.o delete_atoms.o delete_bonds.o deprecated.o dihedral.o dihedral_deprecated.o dihedral_hybrid.o dihedral_zero.o displace_atoms.o domain.o dump_atom.o dump_cfg.o dump.o dump_custom.o dump_deprecated.o dump_image.o dump_local.o dump_movie.o dump_xyz.o error.o finish.o fix_adapt.o fix_addforce.o fix_ave_atom.o fix_ave_chunk.o fix_ave_correlate.o fix_aveforce.o fix_ave_histo.o fix_ave_histo_weight.o fix_ave_time.o fix_balance.o fix_box_relax.o fix.o fix_deform.o fix_deposit.o fix_deprecated.o fix_dt_reset.o fix_dummy.o fix_efield.o fix_enforce2d.o fix_evaporate.o fix_external.o fix_gravity.o fix_group.o fix_halt.o fix_heat.o fix_indent.o fix_langevin.o fix_lineforce.o fix_minimize.o fix_momentum.o fix_move.o fix_neigh_history.o fix_nh.o fix_nh_sphere.o fix_nph.o fix_nph_sphere.o fix_npt.o fix_npt_sphere.o fix_nve.o fix_nve_limit.o fix_nve_noforce.o fix_nve_sphere.o fix_nvt.o fix_nvt_sllod.o fix_nvt_sphere.o fix_planeforce.o fix_press_berendsen.o fix_print.o fix_property_atom.o fix_read_restart.o fix_recenter.o fix_respa.o fix_restrain.o fix_setforce.o fix_spring_chunk.o fix_spring.o fix_spring_self.o fix_store.o fix_store_force.o fix_store_state.o fix_temp_berendsen.o fix_temp_rescale.o fix_thermal_conductivity.o fix_vector.o fix_viscous.o fix_wall.o fix_wall_harmonic.o fix_wall_lj1043.o fix_wall_lj126.o fix_wall_lj93.o fix_wall_morse.o fix_wall_reflect.o fix_wall_region.o fmtlib_format.o fmtlib_os.o force.o group.o hashlittle.o image.o imbalance.o imbalance_group.o imbalance_neigh.o imbalance_store.o imbalance_time.o imbalance_var.o improper.o improper_deprecated.o improper_hybrid.o improper_zero.o info.o input.o integrate.o irregular.o kspace.o kspace_deprecated.o lammps.o lattice.o library.o lmppython.o math_eigen.o math_extra.o math_special.o memory.o min_cg.o min.o min_fire.o min_fire_old.o min_hftn.o minimize.o min_linesearch.o min_quickmin.o min_sd.o modify.o molecule.o my_page.o my_pool_chunk.o nbin.o nbin_multi.o nbin_standard.o neighbor.o neigh_list.o neigh_request.o npair_copy.o npair.o npair_full_bin_atomonly.o npair_full_bin.o npair_full_bin_ghost.o npair_full_multi.o npair_full_multi_old.o npair_full_nsq.o npair_full_nsq_ghost.o npair_half_bin_atomonly_newton.o npair_half_bin_newtoff.o npair_half_bin_newtoff_ghost.o npair_half_bin_newton.o npair_half_bin_newton_tri.o npair_halffull_newtoff.o npair_halffull_newton.o npair_half_multi_newtoff.o npair_half_multi_newton.o npair_half_multi_newton_tri.o npair_half_multi_old_newtoff.o npair_half_multi_old_newton.o npair_half_multi_old_newton_tri.o npair_half_nsq_newtoff.o npair_half_nsq_newtoff_ghost.o npair_half_nsq_newton.o npair_half_respa_bin_newtoff.o npair_half_respa_bin_newton.o npair_half_respa_bin_newton_tri.o npair_half_respa_nsq_newtoff.o npair_half_respa_nsq_newton.o npair_half_size_bin_newtoff.o npair_half_size_bin_newton.o npair_half_size_bin_newton_tri.o npair_half_size_multi_newtoff.o npair_half_size_multi_newton.o npair_half_size_multi_newton_tri.o npair_half_size_multi_old_newtoff.o npair_half_size_multi_old_newton.o npair_half_size_multi_old_newton_tri.o npair_half_size_nsq_newtoff.o npair_half_size_nsq_newton.o npair_skip.o npair_skip_respa.o npair_skip_size.o npair_skip_size_off2on.o npair_skip_size_off2on_oneside.o nstencil.o nstencil_full_bin_2d.o nstencil_full_bin_3d.o nstencil_full_ghost_bin_2d.o nstencil_full_ghost_bin_3d.o nstencil_full_multi_2d.o nstencil_full_multi_3d.o nstencil_full_multi_old_2d.o nstencil_full_multi_old_3d.o nstencil_half_bin_2d.o nstencil_half_bin_2d_tri.o nstencil_half_bin_3d.o nstencil_half_bin_3d_tri.o nstencil_half_multi_2d.o nstencil_half_multi_2d_tri.o nstencil_half_multi_3d.o nstencil_half_multi_3d_tri.o nstencil_half_multi_old_2d.o nstencil_half_multi_old_2d_tri.o nstencil_half_multi_old_3d.o nstencil_half_multi_old_3d_tri.o ntopo_angle_all.o ntopo_angle_partial.o ntopo_angle_template.o ntopo_bond_all.o ntopo_bond_partial.o ntopo_bond_template.o ntopo.o ntopo_dihedral_all.o ntopo_dihedral_partial.o ntopo_dihedral_template.o ntopo_improper_all.o ntopo_improper_partial.o ntopo_improper_template.o output.o pair_born.o pair_buck_coul_cut.o pair_buck.o pair_coul_cut.o pair_coul_debye.o pair_coul_dsf.o pair_coul_wolf.o pair.o pair_deprecated.o pair_hybrid.o pair_hybrid_overlay.o pair_hybrid_scaled.o pair_lj_cut_coul_cut.o pair_lj_cut.o pair_lj_expand.o pair_morse.o pair_mything.o pair_soft.o pair_table.o pair_yukawa.o pair_zbl.o pair_zero.o potential_file_reader.o procmap.o random_mars.o random_park.o rcb.o read_data.o read_dump.o reader.o reader_native.o reader_xyz.o read_restart.o region_block.o region_cone.o region.o region_cylinder.o region_deprecated.o region_intersect.o region_plane.o region_prism.o region_sphere.o region_union.o replicate.o rerun.o reset_atom_ids.o reset_mol_ids.o respa.o run.o set.o special.o table_file_reader.o tabular_function.o text_file_reader.o thermo.o timer.o tokenizer.o universe.o update.o utils.o variable.o velocity.o verlet.o write_coeff.o write_data.o write_dump.o write_restart.o
g++ -g -O main.o -L../STUBS -L. -llammps_serial -lmpi_stubs -o ../lmp_serial
size ../lmp_serial
text data bss dec hex filename
4846309 11752 944 4859005 4a247d ../lmp_serial
make[1]: Leaving directory '/home/akohlmey/compile/lammps/src/Obj_serial'

$ ./lmp_serial -h
...
* Pair styles:

born buck buck/coul/cut coul/cut coul/debye
coul/dsf coul/wolf reax hybrid hybrid/overlay
hybrid/scaled lj/cut lj/cut/coul/cut lj/expand morse
mything soft table yukawa zbl
zero

Dear Axel and Steve,
Thank you for your time and email. I have tried with both make and cmake
If I add
#ifdef REGION_CLASS
RegionStyle(block,RegBlock);
#else
These lines to my region_triangle.h, it gives compilation error as:
mpicxx -g -O3 -DLAMMPS_GZIP -DLAMMPS_MEMALIGN=64 -DMPICH_SKIP_MPICXX -DOMPI_SKIP_MPICXX=1 -c …/domain.cpp
…/domain.cpp: In instantiation of ‘static LAMMPS_NS::Region* LAMMPS_NS::Domain::region_creator(LAMMPS_NS::LAMMPS*, int, char**) [with T = LAMMPS_NS::RegTriangle]’:
…/region_triangle.h:16:1: required from here
…/domain.cpp:1806:10: error: invalid new-expression of abstract class type ‘LAMMPS_NS::RegTriangle’
1806 | return new T(lmp, narg, arg);

^~~~~~~~~~~~~~~~~~~~~
In file included from …/style_region.h:9,
from …/domain.cpp:20:
…/region_triangle.h:27:7: note: because the following virtual functions are pure within ‘LAMMPS_NS::RegTriangle’:
27 | class RegTriangle : public Region {

If I don’t add these lines, both make and cmake compile it though, but without adding the required styles.
[ 50%] Building CXX object CMakeFiles/lammps.dir/mnt/c/lammps-30Jul2021/src/region_plane.cpp.o
[ 50%] Building CXX object CMakeFiles/lammps.dir/mnt/c/lammps-30Jul2021/src/region_prism.cpp.o
[ 50%] Building CXX object CMakeFiles/lammps.dir/mnt/c/lammps-30Jul2021/src/region_sphere.cpp.o
[ 50%] Building CXX object CMakeFiles/lammps.dir/mnt/c/lammps-30Jul2021/src/region_triangle.cpp.o
[ 51%] Building CXX object CMakeFiles/lammps.dir/mnt/c/lammps-30Jul2021/src/region_union.cpp.o
[ 51%] Building CXX object CMakeFiles/lammps.dir/mnt/c/lammps-30Jul2021/src/replicate.cpp.o

But

  • Region styles:

block cone cylinder intersect plane
prism sphere union

Thank you.

region_triangle.h (1.03 KB)

region_triangle.cpp (1.93 KB)

Just to confirm Steve’s statement. The source code from 2008 needs to be updated to work correctly. Just adding the files will compile them, but not integrate them in a way that LAMMPS can recognize them. However, adding the ifdefs and the RegionStyle() macro is not sufficient. The LAMMPS code has evolved over the 13 years since and additional required functions need to be added. After updating the header you will get a compile error like this:

[ 9%] Building CXX object CMakeFiles/lammps.dir/home/akohlmey/compile/lammps/src/domain.cpp.o
/home/akohlmey/compile/lammps/src/domain.cpp: In instantiation of ‘static LAMMPS_NS::Region* LAMMPS_NS::Domain::region_creator(LAMMPS_NS::LAMMPS*, int, char**) [with T = LAMMPS_NS::RegTriangle]’:
/home/akohlmey/compile/lammps/src/region_triangle.h:16:1: required from here
/home/akohlmey/compile/lammps/src/domain.cpp:1806:10: error: invalid new-expression of abstract class type ‘LAMMPS_NS::RegTriangle’
1806 | return new T(lmp, narg, arg);
> ^~~~~~~~~~~~~~~~~~~~~
In file included from /home/akohlmey/compile/lammps/build-test/styles/style_region.h:10,
from /home/akohlmey/compile/lammps/src/domain.cpp:20:
/home/akohlmey/compile/lammps/src/region_triangle.h:27:7: note: because the following virtual functions are pure within ‘LAMMPS_NS::RegTriangle’:
27 | class RegTriangle : public Region {
> ^~~~~~~~~~~
In file included from /home/akohlmey/compile/lammps/src/region_block.h:23,
from /home/akohlmey/compile/lammps/build-test/styles/style_region.h:1,
from /home/akohlmey/compile/lammps/src/domain.cpp:20:
/home/akohlmey/compile/lammps/src/region.h:91:15: note: ‘virtual int LAMMPS_NS::Region::inside(double, double, double)’
91 | virtual int inside(double, double, double) = 0;
> ^~~~~~
/home/akohlmey/compile/lammps/src/region.h:92:15: note: ‘virtual int LAMMPS_NS::Region::surface_interior(double*, double)’
92 | virtual int surface_interior(double *, double) = 0;
> ^~~~~~~~~~~~~~~~
/home/akohlmey/compile/lammps/src/region.h:93:15: note: ‘virtual int LAMMPS_NS::Region::surface_exterior(double*, double)’
93 | virtual int surface_exterior(double *, double) = 0;
> ^~~~~~~~~~~~~~~~
make[2]: *** [CMakeFiles/lammps.dir/build.make:1526: CMakeFiles/lammps.dir/home/akohlmey/compile/lammps/src/domain.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:1349: CMakeFiles/lammps.dir/all] Error 2
make: *** [Makefile:136: all] Error 2

Your change to the header is very obviously incorrect. Please see the explanations in the manual at: https://docs.lammps.org/Modify_overview.html

But as I already pointed out, you need to add multiple functions to the class that are now required for Region classes (all the functions where the compiler complains about “pure virtual functions” must be implemented in a Region class. Please see the manual for explanations:

https://docs.lammps.org/Modify_region.html

So without some C++ programming from your side, it will simply not possible, unless you will switch to a version of LAMMPS from the early C++ days of LAMMPS (which will require multiple manual steps to include new styles).

Hi Steve,
I tried with adding these 3 lines at the top of old files, but its giving error as:

Even though I just copy and pasted these lines from new files. I am using the July, 2021 version of lammps. It gives error at line 16, which is about RegionStyle(triangle, RegTriangle);
I dont understand why it is happening.
Thank you.

Dear Axel and Steve,
I did compile your code by adding 3 header lines and some virtual functions like interior_surface, exterior_surface etc. It got compiled without any error but the triangle command is not working and not taking arguments. I define the triangle region as x1 y1 x2 y2 x3 y3 vertices of the triangle. Your advice will be appreciated.

Thank you.

region_triangle.cpp (1.93 KB)

region_triangle.h (1.61 KB)