Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

import numpy as np 

from scipy.optimize import minimize 

from lsst.sims.utils import _angularSeparation 

 

__all__ = ['thetaphi2xyz', 'even_points', 'elec_potential', 'ang_potential', 'fib_sphere_grid', 

'iterate_potential_random', 'iterate_potential_smart', 'even_points_xyz', 'elec_potential_xyz', 

'xyz2thetaphi'] 

 

 

def thetaphi2xyz(theta, phi): 

x = np.sin(phi)*np.cos(theta) 

y = np.sin(phi)*np.sin(theta) 

z = np.cos(phi) 

return x, y, z 

 

 

def xyz2thetaphi(x, y, z): 

phi = np.arccos(z) 

theta = np.arctan2(y, x) 

return theta, phi 

 

 

def elec_potential(x0): 

""" 

Compute the potential energy for electrons on a sphere 

 

Parameters 

---------- 

x0 : array 

First half of x0 or theta values, secnd half phi 

 

Returns 

------- 

Potential energy 

""" 

 

theta = x0[0:x0.size/2] 

phi = x0[x0.size/2:] 

 

x, y, z = thetaphi2xyz(theta, phi) 

# Distance squared 

dsq = 0. 

 

indices = np.triu_indices(x.size, k=1) 

 

for coord in [x, y, z]: 

coord_i = np.tile(coord, (coord.size, 1)) 

coord_j = coord_i.T 

d = (coord_i[indices]-coord_j[indices])**2 

dsq += d 

 

U = np.sum(1./np.sqrt(dsq)) 

return U 

 

 

def potential_single(coord0, x, y, z): 

""" 

Find the potential contribution from a single point. 

""" 

 

x0 = coord0[0] 

y0 = coord0[1] 

z0 = coord0[2] 

# Enforce point has to be on a sphere 

rsq = x0**2+y0**2 + z0**2 

r = np.sqrt(rsq) 

x0 = x0/r 

y0 = y0/r 

z0 = z0/r 

 

dsq = (x-x0)**2+(y-y0)**2+(z-z0)**2 

U = np.sum(1./np.sqrt(dsq)) 

return U 

 

def xyz2U(x, y, z): 

""" 

compute the potential 

""" 

dsq = 0. 

 

indices = np.triu_indices(x.size, k=1) 

 

for coord in [x, y, z]: 

coord_i = np.tile(coord, (coord.size, 1)) 

coord_j = coord_i.T 

dsq += (coord_i[indices]-coord_j[indices])**2 

 

d = np.sqrt(dsq) 

U = np.sum(1./d) 

return U 

 

def iterate_potential_smart(x0, stepfrac=0.1): 

""" 

Calculate the change in potential by shifting points in theta and phi directions 

# wow, that sure didn't work at all. 

""" 

 

theta = x0[0:x0.size/2] 

phi = x0[x0.size/2:] 

x, y, z = thetaphi2xyz(theta, phi) 

 

U_input = xyz2U(x, y, z) 

 

# Now to loop over each point, and find where it's potenital minimum would be, and move it  

# half-way there. 

xyz_new = np.zeros((x.size, 3), dtype=float) 

mask = np.ones(x.size, dtype=bool) 

for i in np.arange(x.size): 

mask[i] = 0 

fit = minimize(potential_single, [x[i], y[i], z[i]], args=(x[mask], y[mask], z[mask])) 

mask[i] = 1 

xyz_new[i] = fit.x/np.sqrt(np.sum(fit.x**2)) 

 

xyz_input = np.array((x, y, z)).T 

diff = xyz_input - xyz_new 

 

# Move half way in x-y-z space 

xyz_out = xyz_input + stepfrac*diff 

# Project back onto sphere 

xyz_out = xyz_out.T/np.sqrt(np.sum(xyz_out**2, axis=1)) 

U_new = xyz2U(xyz_out[0, :], xyz_out[1, :], xyz_out[2, :]) 

theta, phi = xyz2thetaphi(xyz_out[0, :], xyz_out[1, :], xyz_out[2, :]) 

return np.concatenate((theta, phi)), U_new 

 

 

def iterate_potential_random(x0, stepsize=.05): 

""" 

Given a bunch of theta,phi values, shift things around to minimize potential 

""" 

 

theta = x0[0:x0.size/2] 

phi = x0[x0.size/2:] 

 

x, y, z = thetaphi2xyz(theta, phi) 

# Distance squared 

dsq = 0. 

 

indices = np.triu_indices(x.size, k=1) 

 

for coord in [x, y, z]: 

coord_i = np.tile(coord, (coord.size, 1)) 

coord_j = coord_i.T 

d = (coord_i[indices]-coord_j[indices])**2 

dsq += d 

 

d = np.sqrt(dsq) 

 

U_input = 1./d 

 

# offset everything by a random ammount 

x_new = x + np.random.random(theta.size) * stepsize 

y_new = y + np.random.random(theta.size) * stepsize 

z_new = z + np.random.random(theta.size) * stepsize 

 

r = (x_new**2 + y_new**2 + z_new**2)**0.5 

# put back on the sphere 

x_new = x_new/r 

y_new = y_new/r 

z_new = z_new/r 

 

dsq_new = 0 

for coord, coord_new in zip([x, y, z], [x_new, y_new, z_new]): 

coord_i_new = np.tile(coord_new, (coord_new.size, 1)) 

coord_j = coord_i_new.T 

d_new = (coord_i_new[indices]-coord_j[indices])**2 

dsq_new += d_new 

U_new = 1./np.sqrt(dsq_new) 

 

U_diff = np.sum(U_new)-np.sum(U_input) 

if U_diff > 0: 

return x0, 0. 

else: 

theta, phi = xyz2thetaphi(x_new, y_new, z_new) 

return np.concatenate((theta, phi)), U_diff 

 

 

def ang_potential(x0): 

""" 

If distance is computed along sphere rather than through 3-space. 

""" 

theta = x0[0:x0.size/2] 

phi = np.pi/2-x0[x0.size/2:] 

 

indices = np.triu_indices(theta.size, k=1) 

 

theta_i = np.tile(theta, (theta.size, 1)) 

theta_j = theta_i.T 

phi_i = np.tile(phi, (phi.size, 1)) 

phi_j = phi_i.T 

d = _angularSeparation(theta_i[indices], phi_i[indices], theta_j[indices], phi_j[indices]) 

U = np.sum(1./d) 

return U 

 

 

def fib_sphere_grid(npoints): 

""" 

Use a Fibonacci spiral to distribute points uniformly on a sphere. 

 

based on https://people.sc.fsu.edu/~jburkardt/py_src/sphere_fibonacci_grid/sphere_fibonacci_grid_points.py 

 

Returns theta and phi in radians 

""" 

 

phi = (1.0 + np.sqrt(5.0)) / 2.0 

 

i = np.arange(npoints, dtype=float) 

i2 = 2*i - (npoints-1) 

theta = (2.0*np.pi * i2/phi) % (2.*np.pi) 

sphi = i2/npoints 

phi = np.arccos(sphi) 

return theta, phi 

 

 

def even_points(npts, use_fib_init=True, method='CG', potential_func=elec_potential, maxiter=None): 

""" 

Distribute npts over a sphere and minimize their potential, making them 

"evenly" distributed 

 

Starting with the Fibonacci spiral speeds things up by ~factor of 2. 

""" 

 

if use_fib_init: 

# Start with fibonacci spiral guess 

theta, phi = fib_sphere_grid(npts) 

else: 

# Random on a sphere 

theta = np.random.rand(npts)*np.pi*2. 

phi = np.arccos(2.*np.random.rand(npts)-1.) 

 

x = np.concatenate((theta, phi)) 

# XXX--need to check if this is the best minimizer 

min_fit = minimize(potential_func, x, method='CG', options={'maxiter': maxiter}) 

 

x = min_fit.x 

theta = x[0:x.size/2] 

phi = x[x.size/2:] 

# Looks like I get the same energy values as https://en.wikipedia.org/wiki/Thomson_problem 

return theta, phi 

 

 

def elec_potential_xyz(x0): 

x0 = x0.reshape(3, x0.size/3) 

x = x0[0, :] 

y = x0[1, :] 

z = x0[2, :] 

dsq = 0. 

 

r = np.sqrt(x**2 + y**2 + z**2) 

x = x/r 

y = y/r 

z = z/r 

indices = np.triu_indices(x.size, k=1) 

 

for coord in [x, y, z]: 

coord_i = np.tile(coord, (coord.size, 1)) 

coord_j = coord_i.T 

d = (coord_i[indices]-coord_j[indices])**2 

dsq += d 

 

U = np.sum(1./np.sqrt(dsq)) 

return U 

 

 

def x02sphere(x0): 

x0 = x0.reshape(3, x0.size/3) 

x = x0[0, :] 

y = x0[1, :] 

z = x0[2, :] 

 

r = np.sqrt(x**2 + y**2 + z**2) 

x = x/r 

y = y/r 

z = z/r 

 

return np.concatenate((x, y, z)) 

 

 

def even_points_xyz(npts, use_fib_init=True, method='CG', potential_func=elec_potential_xyz, maxiter=None, 

callback=None): 

""" 

Distribute npts over a sphere and minimize their potential, making them 

"evenly" distributed 

 

Starting with the Fibonacci spiral speeds things up by ~factor of 2. 

""" 

 

if use_fib_init: 

# Start with fibonacci spiral guess 

theta, phi = fib_sphere_grid(npts) 

else: 

# Random on a sphere 

theta = np.random.rand(npts)*np.pi*2. 

phi = np.arccos(2.*np.random.rand(npts)-1.) 

 

x = np.concatenate(thetaphi2xyz(theta, phi)) 

# XXX--need to check if this is the best minimizer 

min_fit = minimize(potential_func, x, method='CG', options={'maxiter': maxiter}, callback=callback) 

 

x = x02sphere(min_fit.x) 

 

# Looks like I get the same energy values as https://en.wikipedia.org/wiki/Thomson_problem 

return x