Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Molecular Gas Dynamics – AE747

Computational Project - 02

Sampling and Post-processing of Data from


Particle based DSMC and MD Methods

G.Kalyan
210400
% Use uigetfile to manually select the DSMC data file
[dsmc_filename, dsmc_path] = uigetfile('*.txt', 'Select DSMC Data
File');
if dsmc_filename == 0
error('No file selected.');
end
dsmc_data_file = fullfile(dsmc_path, dsmc_filename);
% Use uigetfile to manually select the MD data file
[md_filename, md_path] = uigetfile('*.txt', 'Select MD Data File');
if md_filename == 0
error('No file selected.');
end
md_data_file = fullfile(md_path, md_filename);

% Verify the existence of the DSMC file


if exist(dsmc_data_file, 'file') ~= 2
error('The DSMC file %s does not exist or is not accessible.',
dsmc_data_file);
end

% Access the DSMC file for reading


fid_dsmc = fopen(dsmc_data_file, 'r');

% Ignore header lines until reaching the line containing numeric data
while true
line = fgetl(fid_dsmc);
if ~ischar(line)
error('No numeric data found in the DSMC file.');
elseif startsWith(line, 'ITEM: ATOMS')
break; % Exit the loop when numeric data is reached
end
end

% Extract numeric data using textscan


dsmc_data = textscan(fid_dsmc, '%f %f %f %f %f %f %f');

% Terminate access to the DSMC file


fclose(fid_dsmc);

% Transform DSMC data from cell array to matrix


dsmc_data = cell2mat(dsmc_data);

% Retrieve velocity components from DSMC data


dsmc_velocities = dsmc_data(:, 5:7);
% Verify the presence of the MD file

if exist(md_data_file, 'file') ~= 2
error('The MD file %s does not exist or is not accessible.',
md_data_file);
end
% Access the MD file for reading
fid_md = fopen(md_data_file, 'r');

% Ignore header lines until reaching the line containing numeric data
while true
line = fgetl(fid_md);
if ~ischar(line)
error('No numeric data found in the MD file.');
elseif startsWith(line, 'ITEM: ATOMS')
break;
% Terminate the loop upon reaching numeric data
end
end
% Parse numeric data using textscan
md_data = textscan(fid_md, '%f %f %f %f %f %f %f');
% Terminate access to the MD file
fclose(fid_md);
% Transform MD data from cell array to matrix format
md_data = cell2mat(md_data);
% Retrieve velocity components from MD dataset

md_velocities = md_data(:, 5:7);


% Merge velocities from both simulations
all_velocities = [dsmc_velocities; md_velocities];
% Compute Bulk Velocity
bulk_velocity = mean(all_velocities);
% Constants
k_B = 1.380649e-23; % Boltzmann's constant in Joules per Kelvin
temperature = 300; % Temperature in Kelvin
box_length = 5e-8; % Side length of the cubic box in meters
% Total number of particles
N_particles_MD = size(md_velocities, 1); % Particles count from MD
simulation
N_particles_DSMC = size(dsmc_velocities, 1); % Particles count from
DSMC simulation
N_particles_statmech = size(all_velocities, 1); % Particles count for
statistical analysis
% Volume of the system
V_system = box_length^3;
% Compute bulk velocity
bulk_velocity_DSMC = mean(dsmc_velocities);
bulk_velocity_MD = mean(md_velocities);
% Calculate particle speeds for statistical analysis
speed_statmech = sqrt(sum(all_velocities.^2, 2));
% Define bins for histogram
num_bins = 222; % Adjust the number of bins as necessary
bin_edges = linspace(min(speed_statmech), max(speed_statmech),
num_bins+1);
bin_centers = (bin_edges(1:end-1) + bin_edges(2:end)) / 2;
% Bin particles based on speed for statistical analysis
speed_hist_statmech = histcounts(speed_statmech, bin_edges);
% Normalize histogram to obtain Probability Density Function for
statistical calculation
speed_pdf_statmech = speed_hist_statmech / (sum(speed_hist_statmech) *
(bin_edges(2) - bin_edges(1)));
% Constants for theoretical Maxwellian distribution
molar_mass = 39.948e-3; % Molar mass of Argon in kilograms per mole
Avogadro_constant = 6.022e23; % Avogadro's constant
particle_mass = molar_mass / Avogadro_constant; % Mass of a single
particle in kiograms
m = particle_mass; % For the sake of brevity
% Theoretical Maxwellian PDF
speed_pdf_maxwellian = 4 * pi * (m / (2 * pi * k_B *
temperature))^(3/2) * (4 * pi * (bin_centers .^ 2) .* exp(-(m *
(bin_centers .^ 2))) / (2 * k_B * temperature));
% Plot PDF for particle speed for statistical analysis
% Ensure proper normalization of theoretical curve
integral_maxwellian = trapz(bin_centers, speed_pdf_maxwellian);
speed_pdf_maxwellian_normalized = speed_pdf_maxwellian /
integral_maxwellian;
% Plot PDF particle speed for statistical analysis
figure;
bar(bin_centers, speed_pdf_statmech, 'hist');
hold on;
plot(bin_centers, speed_pdf_maxwellian_normalized, 'r--', 'LineWidth',
2); % Employ the normalized Maxwellian curve
hold off;
xlabel('Speed (m/s)');
ylabel('Probability Density');
title('Probability Density Function (PDF) for Speed (Statistical
Calculation)');
legend('Statistical Calculation', 'Theoretical (Maxwellian)');
grid on;
% figure;
% bar(bin_centers, speed_pdf_statmech, 'hist');
% hold on;
% plot(bin_centers, speed_pdf_maxwellian, 'r--', 'LineWidth', 2);
% hold off;
% xlabel('Speed (m/s)');
% ylabel('Probability Density');
% title('Probability Density Function (PDF) for Speed (Statistical
Calculation)');
% legend('Statistical Calculation', 'Theoretical (Maxwellian)');
% grid on;
% Calculate pressure using the ideal gas law for statistical analysis
pressure_statmech = N_particles_statmech / V_system * k_B *
temperature;
% Compute pressure from DSMC data
pressure_DSMC = N_particles_DSMC / V_system * k_B * temperature;
% Compute pressure from MD data
pressure_MD = N_particles_MD / V_system * k_B * temperature;
% Compute kinetic energy per particle for statistical analysis
kinetic_energy_per_particle_statmech = 1.5 * k_B * temperature;
% Compute kinetic energy from DSMC data
kinetic_energy_DSMC = sum(0.5 * particle_mass *
sum(dsmc_velocities.^2, 2)) / N_particles_DSMC;
% Compute kinetic energy from MD data
kinetic_energy_MD = sum(0.5 * particle_mass * sum(md_velocities.^2,
2)) / N_particles_MD;
% Errors
pressure_error_statmech_DSMC = abs(pressure_statmech - pressure_DSMC);
pressure_error_statmech_MD = abs(pressure_statmech - pressure_MD);
kinetic_energy_error_statmech_DSMC =
abs(kinetic_energy_per_particle_statmech - kinetic_energy_DSMC);
kinetic_energy_error_statmech_MD =
abs(kinetic_energy_per_particle_statmech - kinetic_energy_MD);
bulk_velocity_error_DSMC = abs(bulk_velocity_DSMC - bulk_velocity);
bulk_velocity_error_MD = abs(bulk_velocity_MD - bulk_velocity);
% Display results
disp(['Bulk Velocity: ', num2str(bulk_velocity)]);
disp(['Bulk Velocity DSMC: ', num2str(bulk_velocity_DSMC)]);
disp(['Bulk Velocity MD: ', num2str(bulk_velocity_MD)]);
disp(['Pressure DSMC: ', num2str(pressure_DSMC)]);
disp(['Pressure MD: ', num2str(pressure_MD)]);
disp(['Kinetic Energy DSMC: ', num2str(kinetic_energy_DSMC)]);
disp(['Kinetic Energy MD: ', num2str(kinetic_energy_MD)]);
disp(['Pressure Statmech: ', num2str(pressure_statmech)]);
disp(['Kinetic Energy per Particle Statmech: ',
num2str(kinetic_energy_per_particle_statmech)]);
disp(['Error in Pressure (Statistical Mechanics vs DSMC): ',
num2str(pressure_error_statmech_DSMC), ' Pa']);
disp(['Error in Pressure (Statistical Mechanics vs MD): ',
num2str(pressure_error_statmech_MD), ' Pa']);
disp(['Error in Kinetic Energy per Particle (Statistical Mechanics vs
DSMC): ',num2str(kinetic_energy_error_statmech_DSMC), ' J']);
disp(['Error in Kinetic Energy per Particle (Statistical Mechanics vs
MD): ',num2str(kinetic_energy_error_statmech_MD), ' J']);
disp(['Error in Bulk Velocity (Statistical Mechanics vs DSMC): ',
num2str(bulk_velocity_error_DSMC), ' m/s']);
disp(['Error in Bulk Velocity (Statistical Mechanics vs MD): ',
num2str(bulk_velocity_error_MD), ' m/s']);

Results obtained:

Bulk Velocity: 5.1647e-09 9.1814e-09 -2.818e-08

Bulk Velocity DSMC: -1.5968e-08 4.232e-08 -2.6775e-08

Bulk Velocity MD: 2.6297e-08 -2.3957e-08 -2.9586e-08

Pressure DSMC: 132542.304

Pressure MD: 132542.304

Kinetic Energy DSMC: 6.2112e-25

Kinetic Energy MD: 6.2112e-25

Pressure Statmech: 265084.608

Kinetic Energy per Particle Statmech: 6.2129e-21

Error in Pressure (Statistical Mechanics vs DSMC): 132542.304 Pa

Error in Pressure (Statistical Mechanics vs MD): 132542.304 Pa

Error in Kinetic Energy per Particle (Statistical Mechanics vs DSMC): 6.2123e-21 J


Error in Kinetic Energy per Particle (Statistical Mechanics vs MD): 6.2123e-21 J

Error in Bulk Velocity (Statistical Mechanics vs DSMC): 2.1133e-08 3.3138e-08 1.4052e-09


m/s

Graph Obtained:

You might also like