This paper addresses the problem of community detection (clustering) in self-organizing systems consisting of a large number of similar interacting elements. A three-dimensional neural network is chosen as a model example, where neurons act as elements and synaptic connections serve as edges of a weighted graph. An adaptation of the Louvain method, one of the most efficient algorithms for community detection in large graphs, to this class of systems is proposed. The mathematical foundations of the method are presented: definition of modularity considering the effective distance between elements (length of axons and dendrites), description of the two-phase iterative procedure, and formulas for modularity gain. Quality metrics specific to three-dimensional neural structures are discussed. The results can be used to analyze the functional organization of neuronal ensembles in neurophysiological studies.