This paper mainly studies to explore a three-dimensional geometric model including three modules of a sandcastle foundation with optimal stability. Firstly, based on the knowledge of streamline structure, structural mechanics, and fluid mechanics, the most stable sand pile foundation model under the action of tidal current and wave is established. Secondly, by limiting the degree of allowable aggregation, the discrete global optimization algorithm based on the continuous descent method is adopted to find out the optimal water-sand ratio. Finally, we apply the above results to verify the reliability of the optimal model by comprehensively considering the influence of rainfall factors on sandcastles.