double HeliumSimpleAnalytical::waveFunction(const mat &r, VMCSolver *solver) { double alpha = solver->getAlpha(); vec rpos(solver->getNParticles()); for(int i = 0; i < solver->getNParticles(); i++) { double rSingleParticle = 0; for(int j = 0; j < solver->getNDimensions(); j++) { rSingleParticle += r(i,j) * r(i,j); } rpos[i] = sqrt(rSingleParticle); } // cout << "Starting new run" << endl << endl; // cout << "detUp is" << solver->determinant()->detUp << endl; // cout << "detDown is" << solver->determinant()->detDown << endl; // cout << "SlaterDeterminant is " << SD << endl; // cout << "Correct wavefunction is: " << exp(-accu(rpos) * alpha) << endl << endl; return SD;//exp(-accu(rpos) * alpha); }

void level::insertLifeUpgrade() { double px = ((rand() % 2000)-1000)*(DIMENSION-0.25)/1000.; double py = ((rand() % 2000)-1000)*(DIMENSION-0.25)/1000.; vector2D rpos(px,py,0,0); lifeUpgradeList.push_back(new lifeUpgrade(rpos)); }

double DynNewtonian::getSquareCellCollision2(const Particle& part, const Vector & origin, const Vector & width) const { Vector rpos(part.getPosition() - origin); Vector vel(part.getVelocity()); Sim->BCs->applyBC(rpos, vel); for (size_t iDim = 0; iDim < NDIM; ++iDim) if ((vel[iDim] == 0) && (std::signbit(vel[iDim]))) vel[iDim] = 0; double retVal; if (vel[0] < 0) retVal = -rpos[0] / vel[0]; else retVal = (width[0]-rpos[0]) / vel[0]; for (size_t iDim = 1; iDim < NDIM; ++iDim) { double tmpdt((vel[iDim] < 0) ? -rpos[iDim]/vel[iDim] : (width[iDim]-rpos[iDim]) / vel[iDim]); if (tmpdt < retVal) retVal = tmpdt; } return retVal; }

int DynNewtonian::getSquareCellCollision3(const Particle& part, const Vector & origin, const Vector & width) const { Vector rpos(part.getPosition() - origin); Vector vel(part.getVelocity()); Sim->BCs->applyBC(rpos, vel); int retVal(0); double time(HUGE_VAL); for (size_t iDim = 0; iDim < NDIM; ++iDim) if ((vel[iDim] == 0) && (std::signbit(vel[iDim]))) vel[iDim] = 0; for (size_t iDim = 0; iDim < NDIM; ++iDim) { double tmpdt = ((vel[iDim] < 0) ? -rpos[iDim]/vel[iDim] : (width[iDim]-rpos[iDim]) / vel[iDim]); if (tmpdt < time) { time = tmpdt; retVal = (vel[iDim] < 0) ? -(iDim+1) : (iDim+1); } } if (((retVal < 0) && (vel[abs(retVal)-1] > 0)) || ((retVal > 0) && (vel[abs(retVal)-1] < 0))) M_throw() << "Found an error! retVal " << retVal << " vel is " << vel[abs(retVal)-1]; return retVal; }

void LineBuilder::strip_add_arc(Vector2 center, float angle_delta, Orientation orientation) { // Take the two last vertices and extrude an arc made of triangles // that all share one of the initial vertices Orientation opposite_orientation = orientation == UP ? DOWN : UP; Vector2 vbegin = vertices[_last_index[opposite_orientation]] - center; float radius = vbegin.length(); float angle_step = Math_PI / static_cast<float>(round_precision); float steps = Math::abs(angle_delta) / angle_step; if (angle_delta < 0.f) angle_step = -angle_step; float t = vbegin.angle_to(Vector2(1, 0)); float end_angle = t + angle_delta; Vector2 rpos(0, 0); // Arc vertices for (int ti = 0; ti < steps; ++ti, t += angle_step) { rpos = center + Vector2(Math::cos(t), Math::sin(t)) * radius; strip_add_tri(rpos, orientation); } // Last arc vertice rpos = center + Vector2(Math::cos(end_angle), Math::sin(end_angle)) * radius; strip_add_tri(rpos, orientation); }

double HeliumJastrowAnalytical::waveFunction(const mat &r, VMCSolver *solver) { double nParticles = solver->getNParticles(); double nDimensions = solver->getNDimensions(); double alpha = solver->getAlpha(); double beta = solver->getBeta(); //double r12; vec rpos(nParticles); for(int i = 0; i < nParticles; i++) { double rSingleParticle = 0; for(int j = 0; j < nDimensions; j++) { rSingleParticle += r(i,j) * r(i,j); } rpos[i] = sqrt(rSingleParticle); } // assuming 2 particles // (ta fra elektron-elektron pot.) //r12 = abs(rpos[0] - rpos[1]); double r12 = 0; for(int i = 0; i < nParticles; i++) { for(int j = i + 1; j < nParticles; j++) { r12 = 0; for(int k = 0; k < nDimensions; k++) { r12 += (r(i,k) - r(j,k)) * (r(i,k) - r(j,k)); } r12 = sqrt(r12); } } // cout << exp(r12 / (2.0*(1 + beta * r12))) << endl; return exp(-accu(rpos) * alpha) * exp(r12 / (2.0*(1 + beta * r12))) ; }

void ZCompressor::Deflate(uint8 level) { if( _iscompressed || (!size()) || level>9 ) return; char *buf; buf=new char[size()+8]; uint32 newsize=size(),oldsize=size(); reserve(size()+8); _compress((void*)buf, &newsize, (void*)contents(),size(),level); if(!newsize) return; resize(newsize); rpos(0); wpos(0); append(buf,newsize); delete [] buf; _iscompressed=true; _real_size=oldsize; }

double DynNewtonian::getSquareCellCollision2(const Particle& part, const Vector & origin, const Vector & width) const { Vector rpos(part.getPosition() - origin); Vector vel(part.getVelocity()); Sim->BCs->applyBC(rpos, vel); #ifdef DYNAMO_DEBUG for (size_t iDim = 0; iDim < NDIM; ++iDim) if ((vel[iDim] == 0) && (std::signbit(vel[iDim]))) M_throw() << "You have negative zero velocities, don't use them."; #endif double retVal; if (vel[0] < 0) retVal = -rpos[0] / vel[0]; else retVal = (width[0]-rpos[0]) / vel[0]; for (size_t iDim = 1; iDim < NDIM; ++iDim) { double tmpdt((vel[iDim] < 0) ? -rpos[iDim]/vel[iDim] : (width[iDim]-rpos[iDim]) / vel[iDim]); if (tmpdt < retVal) retVal = tmpdt; } return retVal; }

LuaPacket::LuaPacket(lua_State* L) : ByteBuffer(0), changed_(false) { uint32_t opcode = static_cast<uint32_t>(luaL_checknumber(L, 1)); ByteBuffer::SetOpcode(opcode); rpos(sizeof(uint32_t)); wpos(sizeof(uint32_t)); }

void level::insertFireUpgrade() { double px = ((rand() % 2000)-1000)*(DIMENSION-0.25)/1000.; double py = ((rand() % 2000)-1000)*(DIMENSION-0.25)/1000.; vector2D rpos(px,py,0,0); int wtype = rand() % WEAPONS; fireUpgradeList.push_back(new fireUpgrade(rpos,wtype)); }

//------------------------------------------------------------------------------------- int UDPPacket::recvFromEndPoint(EndPoint & ep, Address* pAddr) { int len = ep.recvfrom(data(), PACKET_MAX_SIZE_UDP, (u_int16_t*)&pAddr->port, (u_int32_t*)&pAddr->ip); KBE_ASSERT(rpos() == 0); wpos(len); return len; }

void level::insertSpiralShip() { double px = ((rand() % 2000)-1000)*(DIMENSION-0.4)/1000.; double py = ((rand() % 2000)-1000)*(DIMENSION-0.4)/1000.; vector2D rpos(px,py,0,0); double sx = 0.001; double sy = 0.001; vector2D resp(sx,sy,0,0); enemyList.push_back(new spiralShip(rpos,resp)); }

//------------------------------------------------------------------------------------- int TCPPacket::recvFromEndPoint(EndPoint & ep, Address* pAddr) { //KBE_ASSERT(MessageHandlers::pMainMessageHandlers != NULL && "Must set up a MainMessageHandlers!\n"); int len = ep.recv(data(), PACKET_MAX_SIZE_TCP * 4); KBE_ASSERT(rpos() == 0); wpos(len); // DEBUG_MSG("TCPPacket::recvFromEndPoint: datasize=%d, wpos=%d.\n", len, wpos()); return len; }

void level::insertDummyShip() { double px = ((rand() % 2000)-1000)*(DIMENSION-0.25)/1000.; double py = ((rand() % 2000)-1000)*(DIMENSION-0.25)/1000.; vector2D rpos(px,py,0,0); double sx = ((rand() % 2000)-1000)/100000.; double sy = ((rand() % 2000)-1000)/100000.; vector2D resp(sx,sy,0,0); enemyList.push_back(new dummyship(rpos,resp)); }

void MemoryStream::print_storage() { FString fbuffer; uint32 trpos = rpos_; DEBUG_MSG("MemoryStream::print_storage(): STORAGE_SIZE: %lu, rpos=%lu.", (unsigned long)wpos(), (unsigned long)rpos()); for (uint32 i = rpos(); i < wpos(); ++i) { fbuffer.AppendInt((int)read<uint8>(i)); fbuffer += TEXT(" "); } DEBUG_MSG("%s", *fbuffer); rpos_ = trpos; }

int DynNewtonian::getSquareCellCollision3(const Particle& part, const Vector & origin, const Vector & width) const { Vector rpos(part.getPosition() - origin); Vector vel(part.getVelocity()); Sim->BCs->applyBC(rpos, vel); int retVal(0); double time(HUGE_VAL); #ifdef DYNAMO_DEBUG for (size_t iDim = 0; iDim < NDIM; ++iDim) if ((vel[iDim] == 0) && (std::signbit(vel[iDim]))) M_throw() << "You have negative zero velocities, dont use them." << "\nPlease think of the neighbour lists."; #endif for (size_t iDim = 0; iDim < NDIM; ++iDim) { double tmpdt = ((vel[iDim] < 0) ? -rpos[iDim]/vel[iDim] : (width[iDim]-rpos[iDim]) / vel[iDim]); if (tmpdt < time) { time = tmpdt; retVal = (vel[iDim] < 0) ? -(iDim+1) : (iDim+1); } } if (((retVal < 0) && (vel[abs(retVal)-1] > 0)) || ((retVal > 0) && (vel[abs(retVal)-1] < 0))) M_throw() << "Found an error! retVal " << retVal << " vel is " << vel[abs(retVal)-1]; return retVal; }

void ZCompressor::Inflate(void) { if( (!_iscompressed) || (!_real_size) || (!size())) return; uLongf origsize=_real_size; int8 result; uint8 *target=new uint8[_real_size]; wpos(0); rpos(0); result = uncompress(target, &origsize, (uint8*)contents(), size()); if( result!=Z_OK || origsize!=_real_size) { logerror("ZCompressor: Inflate error! result=%d cursize=%u origsize=%u realsize=%u\n",result,size(),origsize,_real_size); delete [] target; return; } clear(); append(target,origsize); delete [] target; _real_size=0; _iscompressed=false; }

LuaPacket::LuaPacket(CDataStore* data) : changed_(false) { append(data->Buffer, data->Length); rpos(sizeof(uint32_t)); wpos(sizeof(uint32_t)); }

int DynGravity::getSquareCellCollision3(const Particle& part, const Vector & origin, const Vector & width) const { Vector rpos(part.getPosition() - origin); Vector vel(part.getVelocity()); Sim->BCs->applyBC(rpos, vel); int retVal(0); double time(std::numeric_limits<float>::infinity()); #ifdef DYNAMO_DEBUG for (size_t iDim = 0; iDim < NDIM; ++iDim) if ((vel[iDim] == 0) && (std::signbit(vel[iDim]))) M_throw() << "You have negative zero velocities, dont use them." << "\nPlease think of the neighbour lists."; #endif for (size_t iDim = 0; iDim < NDIM; ++iDim) if ((g[iDim] != 0) && part.testState(Particle::DYNAMIC)) { //First check the "upper" boundary that may have no roots double rdot = (g[iDim] < 0) ? rpos[iDim] - width[iDim]: rpos[iDim]; double arg = vel[iDim] * vel[iDim] - 2 * rdot * g[iDim]; double upperRoot1(std::numeric_limits<float>::infinity()), upperRoot2(std::numeric_limits<float>::infinity()); if (arg >= 0) { double t = -(vel[iDim] + ((vel[iDim]<0) ? -1: 1) * std::sqrt(arg)); upperRoot1 = t / g[iDim]; upperRoot2 = 2 * rdot / t; if (upperRoot2 < upperRoot1) std::swap(upperRoot2, upperRoot1); } //Now the lower boundary which always has roots rdot = (g[iDim] < 0) ? rpos[iDim] : rpos[iDim]-width[iDim]; arg = vel[iDim] * vel[iDim] - 2 * rdot * g[iDim]; double lowerRoot1(std::numeric_limits<float>::infinity()), lowerRoot2(std::numeric_limits<float>::infinity()); if (arg >= 0) { double t = -(vel[iDim] + ((vel[iDim]<0) ? -1: 1) * std::sqrt(arg)); lowerRoot1 = t / g[iDim]; lowerRoot2 = 2 * rdot / t; if (lowerRoot2 < lowerRoot1) std::swap(lowerRoot2, lowerRoot1); } //Now, if the velocity is "up", and the upper roots exist, //then pick the shortest one if (!((g[iDim] < 0) - (vel[iDim] > 0))) if (upperRoot1 < time) { time = upperRoot1; retVal = (g[iDim] < 0) ? (iDim + 1) : -(iDim + 1); } //Otherwise its usually the latest lowerRoot if (lowerRoot2 < time) { time = lowerRoot2; retVal = (g[iDim] < 0) ? - (iDim + 1) : (iDim + 1); } } else { double tmpdt = ((vel[iDim] < 0) ? -rpos[iDim] / vel[iDim] : (width[iDim] - rpos[iDim]) / vel[iDim]); if (tmpdt < time) { time = tmpdt; retVal = (vel[iDim] < 0) ? - (iDim + 1) : iDim + 1; } } return retVal; }

int LuaPacket::Dump(lua_State* L) { // Save old rpos and seek to the beginning (without the opcode) size_t oldrpos = rpos(); rpos(sizeof(uint32_t)); uint32_t pushed = 0; std::string value = lua_tostring(L, 1); std::vector<std::string> parts = Utils::Split(value, ' '); if (parts.size() < 1) return luaL_argerror(L, 1, "Invalid dump format"); for (uint32_t i = 0; i < parts.size(); i++) { if (parts[i] == "int8") { ReadInt8(L); pushed++; } else if (parts[i] == "int16") { ReadInt16(L); pushed++; } else if (parts[i] == "int32") { ReadInt32(L); pushed++; } else if (parts[i] == "int64") { ReadInt64(L); pushed++; } else if (parts[i] == "int128") { ReadInt128(L); pushed++; } else if (parts[i] == "float") { ReadFloat(L); pushed++; } else if (parts[i] == "double") { ReadDouble(L); pushed++; } else if (parts[i] == "string") { ReadString(L); pushed++; } else if (parts[i] == "bit") { ReadBit(L); pushed++; } } // Restore rpos rpos(oldrpos); return pushed; }

int LuaPacket::SetReadPosition(lua_State* L) { uint32_t position = static_cast<uint32_t>(luaL_checknumber(L, 1)); rpos(position); return 0; }

void LineBuilder::new_arc(Vector2 center, Vector2 vbegin, float angle_delta, Color color, Rect2 uv_rect) { // Make a standalone arc that doesn't use existing vertices, // with undistorted UVs from within a square section float radius = vbegin.length(); float angle_step = Math_PI / static_cast<float>(round_precision); float steps = Math::abs(angle_delta) / angle_step; if (angle_delta < 0.f) angle_step = -angle_step; float t = vbegin.angle_to(Vector2(1, 0)); float end_angle = t + angle_delta; Vector2 rpos(0, 0); float tt_begin = -Math_PI / 2.f; float tt = tt_begin; // Center vertice int vi = vertices.size(); vertices.push_back(center); if (_interpolate_color) colors.push_back(color); if (texture_mode != LINE_TEXTURE_NONE) uvs.push_back(interpolate(uv_rect, Vector2(0.5f, 0.5f))); // Arc vertices for (int ti = 0; ti < steps; ++ti, t += angle_step) { Vector2 sc = Vector2(Math::cos(t), Math::sin(t)); rpos = center + sc * radius; vertices.push_back(rpos); if (_interpolate_color) colors.push_back(color); if (texture_mode != LINE_TEXTURE_NONE) { Vector2 tsc = Vector2(Math::cos(tt), Math::sin(tt)); uvs.push_back(interpolate(uv_rect, 0.5f * (tsc + Vector2(1.f, 1.f)))); tt += angle_step; } } // Last arc vertice Vector2 sc = Vector2(Math::cos(end_angle), Math::sin(end_angle)); rpos = center + sc * radius; vertices.push_back(rpos); if (_interpolate_color) colors.push_back(color); if (texture_mode != LINE_TEXTURE_NONE) { tt = tt_begin + angle_delta; Vector2 tsc = Vector2(Math::cos(tt), Math::sin(tt)); uvs.push_back(interpolate(uv_rect, 0.5f * (tsc + Vector2(1.f, 1.f)))); } // Make up triangles int vi0 = vi; for (int ti = 0; ti < steps; ++ti) { indices.push_back(vi0); indices.push_back(++vi); indices.push_back(vi + 1); } }

int LuaPacket::GetReadPosition(lua_State* L) { lua_pushnumber(L, rpos()); return 1; }

int LuaPacket::SyncWritePosition(lua_State* L) { wpos(rpos()); return 0; }

int LuaPacket::SyncReadPosition(lua_State* L) { rpos(wpos()); return 0; }

void resetPacket(void) { wpos(0); rpos(0); // memset(data(), 0, size()); };

virtual size_t totalSize() const { return wpos() - rpos(); }

double DynGravity::getSquareCellCollision2(const Particle& part, const Vector & origin, const Vector & width) const { Vector rpos(part.getPosition() - origin); Vector vel(part.getVelocity()); Sim->BCs->applyBC(rpos, vel); #ifdef DYNAMO_DEBUG for (size_t iDim = 0; iDim < NDIM; ++iDim) if ((vel[iDim] == 0) && (std::signbit(vel[iDim]))) M_throw() << "You have negative zero velocities, dont use them." << "\nPlease think of the neighbour lists."; #endif double retVal = HUGE_VAL; for (size_t iDim = 0; iDim < NDIM; ++iDim) if ((g[iDim] != 0) && part.testState(Particle::DYNAMIC)) { //First check the "upper" boundary that may have no roots double r = (g[iDim] < 0) ? rpos[iDim] - width[iDim] : rpos[iDim]; double arg = vel[iDim] * vel[iDim] - 2 * r * g[iDim]; double upperRoot1(HUGE_VAL), upperRoot2(HUGE_VAL); if (arg >= 0) { double t = -(vel[iDim] + ((vel[iDim]<0) ? -1: 1) * std::sqrt(arg)); upperRoot1 = t / g[iDim]; upperRoot2 = 2 * r / t; if (upperRoot2 < upperRoot1) std::swap(upperRoot2, upperRoot1); } //Now the lower boundary which always has roots r = (g[iDim] < 0) ? rpos[iDim] : rpos[iDim] - width[iDim]; arg = vel[iDim] * vel[iDim] - 2 * r * g[iDim]; double lowerRoot1(HUGE_VAL), lowerRoot2(HUGE_VAL); if (arg >= 0) { double t = -(vel[iDim] + ((vel[iDim]<0) ? -1: 1) * std::sqrt(arg)); lowerRoot1 = t / g[iDim]; lowerRoot2 = 2 * r / t; if (lowerRoot2 < lowerRoot1) std::swap(lowerRoot2, lowerRoot1); } double root = HUGE_VAL; //Now, if the velocity is "up", and the upper roots exist, //then pick the shortest one if (!((g[iDim] < 0) - (vel[iDim] > 0)) && (upperRoot1 != HUGE_VAL)) root = upperRoot1; //Otherwise its usually the latest lowerRoot if (root == HUGE_VAL) root = lowerRoot2; if (root < retVal) retVal = root; } else { double tmpdt((vel[iDim] < 0) ? -rpos[iDim]/vel[iDim] : (width[iDim]-rpos[iDim]) / vel[iDim]); if (tmpdt < retVal) retVal = tmpdt; } return retVal; }