Abstract Advanced control strategies for delivering heat to users in a district heating network have the potential to improve performance and reduce wasted energy. To enable the design of such controllers, this paper proposes an automated plant modeling framework that captures the relevant system dynamics, while being adaptable to any network configuration. Starting from the network topology and system parameters, the developed algorithm generates a state-space model of the system, relying on a graph-based technique to facilitate the combination of component models into a full network model. The accuracy of the approach is validated against experimental data collected from a laboratory-scale district heating network. The verification shows an acceptable average normalized root-mean-square error of 0.39 in the mass flow rates delivered to the buildings, and 0.15 in the network return temperature. Furthermore, the ability of the proposed modeling technique to rapidly generate models characterizing different network configurations is demonstrated through its application to topology optimization. The optimal design, obtained via a branch and bound algorithm, reduces network heat losses by 15% as compared to the conventional length-minimized topology.