example.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#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;
}
Back to wk06_STL_1