#include <iostream>
#include <vector>
#include <array>
#include <unordered_map>
#include <cmath>
#include <functional> // Include this for std::hash
// Define a 3D vector using std::array
using Vector3D = std::array<double, 3>;
// Hash function for std::pair<int, int>
struct pair_hash {
template <class T1, class T2>
std::size_t operator()(const std::pair<T1, T2>& p) const {
auto h1 = std::hash<T1>{}(p.first);
auto h2 = std::hash<T2>{}(p.second);
return h1 ^ (h2 << 1); // or use boost::hash_combine
}
};
// Particle class for molecular dynamics simulation
class Particle {
public:
Particle(const Vector3D& pos, const Vector3D& vel) : position(pos), velocity(vel) {}
void updatePosition(double dt) {
for (int i = 0; i < 3; ++i) {
position[i] += velocity[i] * dt;
}
}
void print() const {
std::cout << "Position: (" << position[0] << ", " << position[1] << ", " << position[2] << ") "
<< "Velocity: (" << velocity[0] << ", " << velocity[1] << ", " << velocity[2] << ")\n";
}
private:
Vector3D position;
Vector3D velocity;
};
// Heat distribution simulation using std::unordered_map
class HeatDistribution {
public:
void setTemperature(int x, int y, double temp) {
grid[std::make_pair(x, y)] = temp;
}
double getTemperature(int x, int y) const {
auto it = grid.find(std::make_pair(x, y));
return (it != grid.end()) ? it->second : 0.0;
}
void print() const {
for (const auto& entry : grid) {
std::cout << "Grid point (" << entry.first.first << ", " << entry.first.second
<< "): Temperature = " << entry.second << "\n";
}
}
private:
std::unordered_map<std::pair<int, int>, double, pair_hash> grid;
};
int main() {
// Molecular dynamics simulation using std::vector
std::vector<Particle> particles;
particles.emplace_back(Vector3D{0.0, 0.0, 0.0}, Vector3D{1.0, 1.0, 1.0});
particles.emplace_back(Vector3D{1.0, 1.0, 1.0}, Vector3D{-1.0, 0.5, 0.5});
particles.emplace_back(Vector3D{-1.0, 0.5, 0.5}, Vector3D{0.5, -1.0, 0.5});
std::cout << "Initial particle states:\n";
for (const auto& p : particles) {
p.print();
}
// Update particle positions
double dt = 0.1;
for (auto& p : particles) {
p.updatePosition(dt);
}
std::cout << "\nParticle states after update:\n";
for (const auto& p : particles) {
p.print();
}
// Heat distribution simulation
HeatDistribution heatGrid;
heatGrid.setTemperature(0, 0, 100.0);
heatGrid.setTemperature(0, 1, 75.0);
heatGrid.setTemperature(1, 0, 50.0);
heatGrid.setTemperature(1, 1, 25.0);
std::cout << "\nHeat distribution grid:\n";
heatGrid.print();
return 0;
}