PeriDyno 1.0.0
Loading...
Searching...
No Matches
EnergyAnalysis.cu
Go to the documentation of this file.
1#include "EnergyAnalysis.h"
2#include <sstream>
3#include <iostream>
4#include <fstream>
5
6
7namespace dyno {
8
9 template<typename TDataType>
10 EnergyAnalysis<TDataType>::EnergyAnalysis()
11 : ConstraintModule()
12 {
13 };
14
15 template<typename TDataType>
16 EnergyAnalysis<TDataType>::~EnergyAnalysis() {
17
18 m_Count.clear();
19 m_Energy.clear();
20 };
21
22 template <typename Real, typename Coord>
23 __global__ void EAM_EnergyAnalysis(
24 DArray<Real> Energy,
25 DArray<Coord> posArr,
26 DArray<Coord> velArr,
27 Real mass
28 )
29 {
30 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
31 if (pId >= posArr.size()) return;
32
33 Energy[pId] = 0.5 * mass * velArr[pId].norm() * velArr[pId].norm();
34 }
35
36
37 template <typename Real, typename Coord>
38 __global__ void EAM_NeighborCount(
39 DArray<Real> count,
40 DArray<Coord> pos,
41 DArrayList<int> neighbors,
42 Real radius
43 )
44 {
45 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
46 if (pId >= pos.size()) return;
47
48 List<int>& list_i = neighbors[pId];
49 count[pId] = (Real)(list_i.size());
50 }
51
52 template<typename TDataType>
53 void EnergyAnalysis<TDataType>::constrain() {
54
55 if (initial)
56 {
57 this->initializeImpl();
58 initial = false;
59 }
60
61 int num = this->inPosition()->getData().size();
62
63 if (m_Energy.size() != num)
64 {
65 m_Energy.resize(num);
66 m_Count.resize(num);
67 m_reduce = Reduction<float>::Create(num);
68 m_arithmetic = Arithmetic<float>::Create(num);
69 m_reduce_real = Reduction<float>::Create(num);
70
71 }
72
73 cuExecute(num, EAM_EnergyAnalysis,
74 m_Energy,
75 this->inPosition()->getData(),
76 this->inVelocity()->getData(),
77 1.0f);
78
79
80 cuExecute(num, EAM_NeighborCount,
81 m_Count,
82 this->inPosition()->getData(),
83 this->inNeighborIds()->getData(),
84 0.0125f
85 );
86
87 Real total_energy = m_arithmetic->Dot(m_Energy, m_Energy);
88 auto average_count = m_reduce_real->average(m_Count.begin(), m_Count.size());
89 std::cout << "*** average_count :" << average_count << std::endl;
90 if (counter % 8 == 0)
91 {
92 *m_output << average_count << std::endl;
93 }
94 counter++;
95
96
97 };
98
99
100
101 template<typename TDataType>
102 bool EnergyAnalysis<TDataType>::initializeImpl() {
103 std::cout << "EnergyAnalysis initializeImpl " << std::endl;
104
105 std::string filename = mOutpuPath + mOutputPrefix + std::string(".txt");
106
107 m_output.reset(new std::fstream(filename.c_str(), std::ios::out));
108
109 return true;
110 };
111
112
113
114 template<typename TDataType>
115 void EnergyAnalysis<TDataType>::setNamePrefix(std::string prefix)
116 {
117 mOutputPrefix = prefix;
118 }
119
120 template<typename TDataType>
121 void EnergyAnalysis<TDataType>::setOutputPath(std::string path)
122 {
123 mOutpuPath = path;
124 }
125
126
127
128 DEFINE_CLASS(EnergyAnalysis);
129}