1__global__
void Prepare_LambdaChange(
Atoms* d_a,
Atoms Mol,
Simulations Sims,
ForceField FF,
size_t start_position,
size_t SelectedComponent,
bool* device_flag)
3 size_t i = blockIdx.x * blockDim.x + threadIdx.x;
4 size_t real_pos = start_position + i;
6 const double3 pos = d_a[SelectedComponent].pos[real_pos];
7 const double scale = d_a[SelectedComponent].scale[real_pos];
8 const double charge = d_a[SelectedComponent].charge[real_pos];
9 const double scaleCoul = d_a[SelectedComponent].scaleCoul[real_pos];
10 const size_t Type = d_a[SelectedComponent].Type[real_pos];
11 const size_t MolID = d_a[SelectedComponent].MolID[real_pos];
16 Mol.scale[i] = scale; Mol.charge[i] = charge; Mol.scaleCoul[i] = scaleCoul; Mol.Type[i] = Type; Mol.MolID[i] = MolID;
17 device_flag[i] =
false;
20static inline MoveEnergy CBCF_LambdaChange(
Components& SystemComponents,
Simulations& Sims,
ForceField& FF,
RandomNumber& Random,
WidomStruct& Widom,
size_t SelectedMolInComponent,
size_t SelectedComponent, double2 oldScale, double2 newScale,
size_t& start_position,
int MoveType,
bool& SuccessConstruction)
25 for(
size_t ijk = 0; ijk < SystemComponents.Total_Components; ijk++)
27 Atomsize += SystemComponents.Moleculesize[ijk] * SystemComponents.NumberOfMolecule_for_Component[ijk];
31 Molsize = SystemComponents.Moleculesize[SelectedComponent];
34 throw std::runtime_error(
"Molecule size is greater than allocated size, Why so big?\n");
36 start_position = SelectedMolInComponent*SystemComponents.Moleculesize[SelectedComponent];
37 Prepare_LambdaChange<<<1, Molsize>>>(Sims.d_a, Sims.Old, Sims, FF, start_position, SelectedComponent, Sims.device_flag);
40 size_t NHostAtom = 0;
size_t NGuestAtom = 0;
41 for(
size_t i = 0; i < SystemComponents.NComponents.y; i++)
42 NHostAtom += SystemComponents.Moleculesize[i] * SystemComponents.NumberOfMolecule_for_Component[i];
43 for(
size_t i = SystemComponents.NComponents.y; i < SystemComponents.NComponents.x; i++)
44 NGuestAtom+= SystemComponents.Moleculesize[i] * SystemComponents.NumberOfMolecule_for_Component[i];
47 size_t HH_Nthread=0;
size_t HH_Nblock=0; Setup_threadblock(NHostAtom * Molsize, &HH_Nblock, &HH_Nthread);
48 size_t HG_Nthread=0;
size_t HG_Nblock=0; Setup_threadblock(NHostAtom * Molsize, &HG_Nblock, &HG_Nthread);
49 size_t GG_Nthread=0;
size_t GG_Nblock=0; Setup_threadblock(NGuestAtom * Molsize, &GG_Nblock, &GG_Nthread);
51 size_t CrossTypeNthread = 0;
52 if(SelectedComponent < SystemComponents.NComponents.y)
53 {GG_Nthread = 0; GG_Nblock = 0; CrossTypeNthread = HH_Nthread; }
55 {HH_Nthread = 0; HH_Nblock = 0; CrossTypeNthread = GG_Nthread; }
57 size_t Nthread = std::max(CrossTypeNthread, HG_Nthread);
58 size_t Total_Nblock = HH_Nblock + HG_Nblock + GG_Nblock;
60 int3 NBlocks = {(int) HH_Nblock, (
int) HG_Nblock, (int) GG_Nblock};
61 bool Do_New =
true;
bool Do_Old =
true;
63 Calculate_Single_Body_Energy_SEPARATE_HostGuest_VDWReal_LambdaChange<<<Total_Nblock, Nthread, Nthread * 2 *
sizeof(double)>>>(Sims.Box, Sims.d_a, Sims.Old, Sims.New, FF, Sims.Blocksum, SelectedComponent, Atomsize, Molsize, Sims.device_flag, NBlocks, Do_New, Do_Old, SystemComponents.NComponents, newScale);
65 cudaMemcpy(SystemComponents.flag, Sims.device_flag,
sizeof(
bool), cudaMemcpyDeviceToHost);
67 if(!SystemComponents.flag[0])
69 SuccessConstruction =
true;
70 double BlockResult[Total_Nblock + Total_Nblock];
71 cudaMemcpy(BlockResult, Sims.Blocksum, 2 * Total_Nblock *
sizeof(
double), cudaMemcpyDeviceToHost);
74 for(
size_t i = 0; i < HH_Nblock; i++)
76 tot.HHVDW += BlockResult[i];
77 tot.HHReal+= BlockResult[i + Total_Nblock];
80 for(
size_t i = HH_Nblock; i < HH_Nblock + HG_Nblock; i++)
82 tot.HGVDW += BlockResult[i];
83 tot.HGReal+= BlockResult[i + Total_Nblock];
86 for(
size_t i = HH_Nblock + HG_Nblock; i < Total_Nblock; i++)
88 tot.GGVDW += BlockResult[i];
89 tot.GGReal+= BlockResult[i + Total_Nblock];
93 if(!FF.noCharges && SystemComponents.hasPartialCharge[SelectedComponent])
96 double2 EwaldE = GPU_EwaldDifference_LambdaChange(Sims.Box, Sims.d_a, Sims.Old, FF, Sims.Blocksum, SystemComponents, SelectedComponent, oldScale, newScale, MoveType);
99 tot.GGEwaldE = EwaldE.x;
100 tot.HGEwaldE = EwaldE.y;
104 tot.HHEwaldE = EwaldE.x;
105 tot.HGEwaldE = EwaldE.y;
110 tot.TailE = TailCorrectionDifference(SystemComponents, SelectedComponent, FF.size, Sims.Box.Volume, MoveType);
114 SuccessConstruction =
false;
120__global__
void Revert_CBCF_Insertion(
Atoms* d_a,
size_t SelectedComponent,
size_t start_position, double2 oldScale)
122 size_t i = blockIdx.x * blockDim.x + threadIdx.x;
123 d_a[SelectedComponent].scale[start_position+i] = oldScale.x;
124 d_a[SelectedComponent].scaleCoul[start_position+i] = oldScale.y;
129__global__
void Update_deletion_data_fractional(
Atoms* d_a,
size_t SelectedComponent,
size_t UpdateLocation,
int Moleculesize,
size_t LastLocation)
131 size_t i = blockIdx.x * blockDim.x + threadIdx.x;
138 if(UpdateLocation != LastLocation)
140 for(
size_t i = 0; i < Moleculesize; i++)
142 double3 pos = d_a[SelectedComponent].pos[UpdateLocation+i];
143 double scale = d_a[SelectedComponent].scale[UpdateLocation+i];
144 double charge = d_a[SelectedComponent].charge[UpdateLocation+i];
145 double scaleCoul = d_a[SelectedComponent].scaleCoul[UpdateLocation+i];
double Type = d_a[SelectedComponent].Type[UpdateLocation+i];
146 d_a[SelectedComponent].pos[UpdateLocation+i] = d_a[SelectedComponent].pos[LastLocation+i];
147 d_a[SelectedComponent].scale[UpdateLocation+i] = d_a[SelectedComponent].scale[LastLocation+i];
148 d_a[SelectedComponent].charge[UpdateLocation+i] = d_a[SelectedComponent].charge[LastLocation+i];
149 d_a[SelectedComponent].scaleCoul[UpdateLocation+i] = d_a[SelectedComponent].scaleCoul[LastLocation+i];
150 d_a[SelectedComponent].Type[UpdateLocation+i] = d_a[SelectedComponent].Type[LastLocation+i];
152 d_a[SelectedComponent].pos[LastLocation+i] = pos;
153 d_a[SelectedComponent].scale[LastLocation+i] = scale;
154 d_a[SelectedComponent].charge[LastLocation+i] = charge; d_a[SelectedComponent].scaleCoul[LastLocation+i] = scaleCoul;
155 d_a[SelectedComponent].Type[LastLocation+i] = Type;
163 d_a[SelectedComponent].size -= Moleculesize;
167__global__
void Revert_CBCF_Deletion(
Atoms* d_a,
Atoms NewMol,
size_t SelectedComponent,
size_t UpdateLocation,
int Moleculesize,
size_t LastLocation)
175 if(UpdateLocation != LastLocation)
177 for(
size_t i = 0; i < Moleculesize; i++)
179 double3 pos = d_a[SelectedComponent].pos[UpdateLocation+i];
180 double scale = d_a[SelectedComponent].scale[UpdateLocation+i];
181 double charge = d_a[SelectedComponent].charge[UpdateLocation+i];
182 double scaleCoul = d_a[SelectedComponent].scaleCoul[UpdateLocation+i];
double Type = d_a[SelectedComponent].Type[UpdateLocation+i];
183 d_a[SelectedComponent].pos[UpdateLocation+i] = d_a[SelectedComponent].pos[LastLocation+i];
184 d_a[SelectedComponent].scale[UpdateLocation+i] = d_a[SelectedComponent].scale[LastLocation+i];
185 d_a[SelectedComponent].charge[UpdateLocation+i] = d_a[SelectedComponent].charge[LastLocation+i];
186 d_a[SelectedComponent].scaleCoul[UpdateLocation+i] = d_a[SelectedComponent].scaleCoul[LastLocation+i];
187 d_a[SelectedComponent].Type[UpdateLocation+i] = d_a[SelectedComponent].Type[LastLocation+i];
189 d_a[SelectedComponent].pos[LastLocation+i] = pos;
190 d_a[SelectedComponent].scale[LastLocation+i] = scale;
191 d_a[SelectedComponent].charge[LastLocation+i] = charge; d_a[SelectedComponent].scaleCoul[LastLocation+i] = scaleCoul;
192 d_a[SelectedComponent].Type[LastLocation+i] = Type;
198 size_t i = blockIdx.x * blockDim.x + threadIdx.x;
201 d_a[SelectedComponent].size += Moleculesize;
205__global__
void update_CBCF_scale(
Atoms* d_a,
size_t start_position,
size_t SelectedComponent, double2 newScale)
207 size_t i = blockIdx.x * blockDim.x + threadIdx.x;
208 d_a[SelectedComponent].scale[start_position+i] = newScale.x;
209 d_a[SelectedComponent].scaleCoul[start_position+i] = newScale.y;
219 double NMol = SystemComponents.NumberOfMolecule_for_Component[SelectedComponent];
220 if(SystemComponents.hasfractionalMolecule[SelectedComponent]) NMol--;
223 double TMMCPacc = 0.0;
225 SystemComponents.Moves[SelectedComponent].CBCFTotal ++;
227 LAMBDA lambda = SystemComponents.Lambda[SelectedComponent];
229 size_t oldBin = lambda.currentBin;
230 size_t nbin = lambda.binsize;
231 double delta = lambda.delta;
232 double oldLambda = delta *
static_cast<double>(oldBin);
238 int SelectednewBin = selectNewBinTMMC(SystemComponents.Lambda[SelectedComponent]);
243 while(SelectednewBin == oldBin || (SelectednewBin < 0 && SystemComponents.NumberOfMolecule_for_Component[SelectedComponent] == 1))
245 if(SystemComponents.Tmmc[SelectedComponent].DoTMMC)
250 SelectednewBin = selectNewBinTMMC(SystemComponents.Lambda[SelectedComponent]);
255 SelectednewBin = selectNewBin(SystemComponents.Lambda[SelectedComponent]);
258 Binchange = SelectednewBin - oldBin;
263 if(SelectednewBin >
static_cast<int>(nbin))
265 MoveType = CBCF_INSERTION;
268 SystemComponents.Moves[SelectedComponent].CBCFInsertionTotal ++;
269 int newBin = SelectednewBin -
static_cast<int>(nbin);
270 double newLambda = delta *
static_cast<double>(newBin);
272 double2 oldScale = SystemComponents.Lambda[SelectedComponent].SET_SCALE(oldLambda);
273 double2 InterScale= SystemComponents.Lambda[SelectedComponent].SET_SCALE(1.0);
274 double2 newScale = SystemComponents.Lambda[SelectedComponent].SET_SCALE(newLambda);
278 size_t start_position = 1;
279 bool SuccessConstruction =
false;
280 energy = CBCF_LambdaChange(SystemComponents, Sims, FF, Random, Widom, SelectedMolInComponent, SelectedComponent, oldScale, InterScale, start_position, CBCF_INSERTION, SuccessConstruction);
281 if(!SuccessConstruction)
284 SystemComponents.Tmmc[SelectedComponent].UpdateCBCF(0.0, NMol, Binchange);
290 update_CBCF_scale<<<1,SystemComponents.Moleculesize[SelectedComponent]>>>(Sims.d_a, start_position, SelectedComponent, InterScale);
291 SuccessConstruction =
false;
292 double Rosenbluth = 0.0;
293 size_t SelectedTrial = 0;
294 double preFactor = 0.0;
295 bool Accepted =
false;
299 MoveEnergy second_step_energy = Insertion_Body(SystemComponents, Sims, FF, Random, Widom, SelectedMolInComponent, SelectedComponent, Rosenbluth, SuccessConstruction, SelectedTrial, preFactor,
true, newScale);
300 if(SuccessConstruction)
302 energy += second_step_energy;
304 double biasTerm = SystemComponents.Lambda[SelectedComponent].biasFactor[newBin] - SystemComponents.Lambda[SelectedComponent].biasFactor[oldBin];
305 preFactor *= std::exp(-SystemComponents.Beta * first_step_energy.total() + biasTerm);
306 double IdealRosen = SystemComponents.IdealRosenbluthWeight[SelectedComponent];
307 TMMCPacc = preFactor * Rosenbluth / IdealRosen;
309 SystemComponents.Tmmc[SelectedComponent].ApplyWLBiasCBCF(preFactor, NMol, Binchange);
310 SystemComponents.Tmmc[SelectedComponent].ApplyTMBiasCBCF(preFactor, NMol, Binchange);
312 if(Get_Uniform_Random() < preFactor * Rosenbluth / IdealRosen) Accepted =
true;
313 SystemComponents.Tmmc[SelectedComponent].TreatAccOutofBoundCBCF(Accepted, NMol, Binchange);
319 SystemComponents.Moves[SelectedComponent].CBCFInsertionAccepted ++;
320 SystemComponents.Moves[SelectedComponent].CBCFAccepted ++;
321 size_t UpdateLocation = SystemComponents.Moleculesize[SelectedComponent] * SystemComponents.NumberOfMolecule_for_Component[SelectedComponent];
323 Update_insertion_data<<<1,1>>>(Sims.d_a, Sims.Old, Sims.New, SelectedTrial, SelectedComponent, UpdateLocation, (int) SystemComponents.Moleculesize[SelectedComponent]);
324 Update_NumberOfMolecules(SystemComponents, Sims.d_a, SelectedComponent, CBCF_INSERTION);
326 SystemComponents.Lambda[SelectedComponent].FractionalMoleculeID = SystemComponents.NumberOfMolecule_for_Component[SelectedComponent] - 1;
327 SystemComponents.Lambda[SelectedComponent].currentBin = newBin;
328 if(SystemComponents.Tmmc[SelectedComponent].DoTMMC)
329 SystemComponents.Tmmc[SelectedComponent].currentBin = newBin;
330 if(!FF.noCharges && SystemComponents.hasPartialCharge[SelectedComponent])
332 Update_Ewald_Vector(Sims.Box,
false, SystemComponents, SelectedComponent);
334 final_energy = energy;
337 if(!Accepted) Revert_CBCF_Insertion<<<1, SystemComponents.Moleculesize[SelectedComponent]>>>(Sims.d_a, SelectedComponent, start_position, oldScale);
339 else if(SelectednewBin < 0)
341 MoveType = CBCF_DELETION;
343 SystemComponents.Moves[SelectedComponent].CBCFDeletionTotal ++;
344 int newBin = SelectednewBin + nbin;
345 double newLambda = delta *
static_cast<double>(newBin);
347 double2 oldScale = SystemComponents.Lambda[SelectedComponent].SET_SCALE(oldLambda);
348 double2 InterScale= SystemComponents.Lambda[SelectedComponent].SET_SCALE(1.0);
349 double2 newScale = SystemComponents.Lambda[SelectedComponent].SET_SCALE(newLambda);
353 bool SuccessConstruction =
false;
355 double Rosenbluth = 0.0;
356 double preFactor = 0.0;
357 size_t UpdateLocation = SelectedMolInComponent * SystemComponents.Moleculesize[SelectedComponent];
358 energy = Deletion_Body(SystemComponents, Sims, FF, Random, Widom, SelectedMolInComponent, SelectedComponent, UpdateLocation, Rosenbluth, SuccessConstruction, preFactor, oldScale);
364 size_t LastMolecule = SystemComponents.NumberOfMolecule_for_Component[SelectedComponent]-1;
365 size_t LastLocation = LastMolecule*SystemComponents.Moleculesize[SelectedComponent];
366 Update_deletion_data_fractional<<<1,1>>>(Sims.d_a, SelectedComponent, UpdateLocation, (int) SystemComponents.Moleculesize[SelectedComponent], LastLocation);
368 size_t OldFracMolID = SystemComponents.Lambda[SelectedComponent].FractionalMoleculeID;
369 Update_NumberOfMolecules(SystemComponents, Sims.d_a, SelectedComponent, CBCF_DELETION);
373 SelectedMolInComponent =
static_cast<size_t>(Get_Uniform_Random() *
static_cast<double>(SystemComponents.NumberOfMolecule_for_Component[SelectedComponent]));
374 SystemComponents.Lambda[SelectedComponent].FractionalMoleculeID = SelectedMolInComponent;
376 bool Accepted =
false;
377 if(SuccessConstruction)
379 size_t start_position = 1;
382 SuccessConstruction =
false;
383 second_step_energy = CBCF_LambdaChange(SystemComponents, Sims, FF, Random, Widom, SelectedMolInComponent, SelectedComponent, InterScale, newScale, start_position, CBCF_DELETION, SuccessConstruction);
386 double biasTerm = SystemComponents.Lambda[SelectedComponent].biasFactor[newBin] - SystemComponents.Lambda[SelectedComponent].biasFactor[oldBin];
387 preFactor *= std::exp(-SystemComponents.Beta * second_step_energy.total() + biasTerm);
388 double IdealRosen = SystemComponents.IdealRosenbluthWeight[SelectedComponent];
389 TMMCPacc = preFactor * IdealRosen / Rosenbluth;
392 SystemComponents.Tmmc[SelectedComponent].ApplyTMBiasCBCF(preFactor, NMol, Binchange);
394 if(Get_Uniform_Random() < preFactor * IdealRosen / Rosenbluth) Accepted =
true;
395 SystemComponents.Tmmc[SelectedComponent].TreatAccOutofBoundCBCF(Accepted, NMol, Binchange);
399 SystemComponents.Moves[SelectedComponent].CBCFDeletionAccepted ++;
400 SystemComponents.Moves[SelectedComponent].CBCFAccepted ++;
402 update_CBCF_scale<<<1,SystemComponents.Moleculesize[SelectedComponent]>>>(Sims.d_a, start_position, SelectedComponent, newScale);
403 SystemComponents.Lambda[SelectedComponent].currentBin = newBin;
404 if(SystemComponents.Tmmc[SelectedComponent].DoTMMC)
405 SystemComponents.Tmmc[SelectedComponent].currentBin = newBin;
406 if(!FF.noCharges && SystemComponents.hasPartialCharge[SelectedComponent])
408 Update_Ewald_Vector(Sims.Box,
false, SystemComponents, SelectedComponent);
410 energy.take_negative();
411 energy += second_step_energy;
412 final_energy = energy;
417 Revert_CBCF_Deletion<<<1,1>>>(Sims.d_a, Sims.New, SelectedComponent, UpdateLocation, (int) SystemComponents.Moleculesize[SelectedComponent], LastLocation);
418 SystemComponents.Lambda[SelectedComponent].FractionalMoleculeID = OldFracMolID;
419 SystemComponents.TotalNumberOfMolecules ++;
420 SystemComponents.NumberOfMolecule_for_Component[SelectedComponent] ++;
421 SystemComponents.UpdatePseudoAtoms(CBCF_INSERTION, SelectedComponent);
427 MoveType = CBCF_LAMBDACHANGE;
428 SystemComponents.Moves[SelectedComponent].CBCFLambdaTotal ++;
429 int newBin =
static_cast<size_t>(SelectednewBin);
430 double newLambda = delta *
static_cast<double>(newBin);
432 double2 oldScale = SystemComponents.Lambda[SelectedComponent].SET_SCALE(oldLambda);
433 double2 newScale = SystemComponents.Lambda[SelectedComponent].SET_SCALE(newLambda);
434 size_t start_position = 1;
435 bool SuccessConstruction =
false;
436 energy = CBCF_LambdaChange(SystemComponents, Sims, FF, Random, Widom, SelectedMolInComponent, SelectedComponent, oldScale, newScale, start_position, CBCF_LAMBDACHANGE, SuccessConstruction);
437 if(!SuccessConstruction)
440 SystemComponents.Tmmc[SelectedComponent].UpdateCBCF(0.0, NMol, Binchange);
445 double biasTerm = SystemComponents.Lambda[SelectedComponent].biasFactor[newBin] - SystemComponents.Lambda[SelectedComponent].biasFactor[oldBin];
446 TMMCPacc = std::exp(-SystemComponents.Beta * energy.total() + biasTerm);
447 double preFactor = 1.0;
448 SystemComponents.Tmmc[SelectedComponent].ApplyTMBiasCBCF(preFactor, NMol, Binchange);
450 bool Accepted =
false;
451 if (Get_Uniform_Random() < preFactor * std::exp(-SystemComponents.Beta * energy.total() + biasTerm)) Accepted =
true;
452 SystemComponents.Tmmc[SelectedComponent].TreatAccOutofBoundCBCF(Accepted, NMol, Binchange);
456 SystemComponents.Moves[SelectedComponent].CBCFLambdaAccepted ++;
458 update_CBCF_scale<<<1,SystemComponents.Moleculesize[SelectedComponent]>>>(Sims.d_a, start_position, SelectedComponent, newScale);
459 SystemComponents.Moves[SelectedComponent].CBCFAccepted ++;
460 SystemComponents.Lambda[SelectedComponent].currentBin = newBin;
461 if(SystemComponents.Tmmc[SelectedComponent].DoTMMC)
462 SystemComponents.Tmmc[SelectedComponent].currentBin = newBin;
463 if(!FF.noCharges && SystemComponents.hasPartialCharge[SelectedComponent])
465 Update_Ewald_Vector(Sims.Box,
false, SystemComponents, SelectedComponent);
467 final_energy = energy;
470 SystemComponents.Tmmc[SelectedComponent].UpdateCBCF(TMMCPacc, NMol, Binchange);
Definition data_struct.h:746
Definition data_struct.h:843
Definition data_struct.h:794
Definition data_struct.h:73
Definition data_struct.h:615
Definition data_struct.h:1053
Definition data_struct.h:1027
Definition data_struct.h:1044