- First, you need to import your trajectory data into MATLAB. If your file is in a text format, you can use the ‘readtable’ function or similar to load the data. For binary files, ‘fread’ might be necessary.
- Once you've loaded the data, you'll want to calculate the density distribution. This can be done by dividing the space into a grid and counting the number of molecules in each grid cell. MATLAB’s ‘histcounts2’ function is handy for this purpose.
- With the density data ready, you can use the ‘contourf’ function to create a 2-D contour plot. This will visually represent the density distribution of your molecules.
2-D contour plot of a trajectory file obtained from lammps
8 views (last 30 days)
Show older comments
I have done a simulation of a mixture of water and ethanol on a graphite surface. Now i have a trajectory file for visualisation. What I want to do is plot a 2-D contour of the mixture to show the density distribution of one of the molecule. can any body help me how to do it in Matlab. How can i basically call the trajectory file which i have in Matlab.
0 Comments
Answers (1)
Raag
on 20 Feb 2025
Hi Dani,
I am assuming that you are looking to visualize the density distribution of your simulation data using a 2-D contour plot in MATLAB.
Here are steps, to achieve the above-mentioned workflow:
Here is an example code for the steps mentioned above:
% Load your trajectory data (replace 'trajectory.txt' with your file)
data = readtable('trajectory.txt'); % Adjust this line based on your file format
% Extract x, y coordinates (assuming your file contains these columns)
x = data.x;
y = data.y;
% Define the grid for the contour plot
xEdges = linspace(min(x), max(x), 100); % Adjust the number of bins as needed
yEdges = linspace(min(y), max(y), 100);
% Calculate 2D histogram for density
[N, xEdges, yEdges] = histcounts2(x, y, xEdges, yEdges);
% Plot the contour
contourf(xEdges(1:end-1), yEdges(1:end-1), N', 'LineColor', 'none');
colorbar;
xlabel('X');
ylabel('Y');
title('2-D Contour Plot of Density Distribution');
Additionally if your data file format is specific (e.g., LAMMPS dump), you may need a custom parser.
For more information on ‘countourf’ function, use this command to open the documentation:
>>web(fullfile(docroot, "techdoc/ref/contourf.html"))
0 Comments
See Also
Categories
Find more on Thermal Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!