Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7731959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:36:42+00:00 2026-06-01T06:36:42+00:00

I’m trying to construct a nearest neighbor list in python that stores the nearest

  • 0

I’m trying to construct a nearest neighbor list in python that stores the nearest neighbors of a node in a finite 3d hexagonal-closest packing (HCP) lattice. I’ve done this already with a 2d square lattice defining the structure like so. I don’t want coordinates, but just a quick way to create a nearest neighbor list for an HCP out of a list of integers. Below is the sample code of how I did this task with a square lattice.

N = int #number of nodes
L = side # a 32x32 graph, L would be 32

for i in range(N):

    nearNeighbor[i][0] = (i + 1 ) % N
    nearNeighbor[i][1] = (i + (N - 1)) % N
    nearNeighbor[i][2] = (i + L) % N
    nearNeighbor[i][3] = (i + N - L) % N

    if (i-L < 0):
         nearNeighbor[i][3] = -2
    if (i+L >= N):
         nearNeighbor[i][2] = -2
    if (i%L) == 0:
         nearNeighbor[i][1] = -2     
    if ((i+1)%L) == 0:
         nearN[eighbori][0] = -2

That’s it. Now an HCP lattice, when visualized, resembles a giant cube of spheres closely packed together. Each node should have at most 12 nearest neighbors and they should come out to make something like a cube. I guess largely I want to know how to use integers and modular arithmetic to represent the HCP lattice like I did with the square lattice. Can you help me stack?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:36:43+00:00Added an answer on June 1, 2026 at 6:36 am

    The answer to this question depends on how one chooses to truncate the HCP lattice and index it. One choice is

    Indexed HCP lattice
    Indexed HCP lattice part 2

    With this choice, the following code will return a list of neighbors of a given site.

    def neighbors(i, W, H, D):
      A = W * H
    
      plane = i / A
      plane_index = i % A
      row = plane_index / W
      col = plane_index % W
    
      r = -1 if row % 2 else 1   # (-1)**row
      p = -1 if plane % 2 else 1 # (-1)**plane
    
      nbors = []
    
      # first include neighbors in same plane
      if col != W-1: nbors.append(i+1)
      if col != 0:   nbors.append(i-1)
      if row != H-1: nbors.append(i+W)
      if row != 0:   nbors.append(i-W)
      if (col != 0 or r > 0) and (col != W-1 or r < 0):
        if row != H-1: nbors.append(i+W+r)
        if row != 0:   nbors.append(i-W+r)
    
      # now add neighbors from other planes
      if plane != D-1: nbors.append(i+A)
      if plane != 0:   nbors.append(i-A)
    
      if (col != 0 or p < 0) and (col != W-1 or p > 0):
        if plane != D-1: nbors.append(i+A-p)
        if plane != 0:   nbors.append(i-A-p)
    
      if ((col != W - 1 or p > 0 or r < 0) and
          (col != 0 or p < 0 or r > 0) and
          (row != H-1 or p < 0) and
          (row != 0 or p > 0)):
        if plane != D-1:
          nbors.append(i + A + p*W + (r-p)/2) #10
        if plane != 0:
          nbors.append(i - A + p*W + (r-p)/2) #11
    
      return nbors
    

    To make sure I got the logic correct, I used the following test while writing the above function

    def test_neighbors():
      n = lambda i: set(neighbors(i, 5, 5, 5))
    
      # test bottom layer
      assert n(0) == set([1,5,6,25,30])
      assert n(2) == set([1,3,7,8,26,27,32])
      assert n(4) == set([3,9,28,29,34])
      assert n(5) == set([0,6,10,30])
      assert n(9) == set([3,4,8,13,14,33,34,38])
      assert n(20) == set([15,16,21,45])
      assert n(21) == set([16,17,20,22,45,46])
      assert n(24) == set([19,23,48,49])
    
      # test second layer
      assert n(25) == set([0,1,26,30,31,50,51])
      assert n(34) == set([4,9,28,29,33,38,39,54,59])
      assert n(36) == set([7,11,12,31,32,35,37,41,42,57,61,62])
      assert n(49) == set([24,44,48,74])
    

    Note that the test doesn’t cover all of the unique types of sites, so, there may still be a corner case somewhere that is wrong.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.