The ex ante environmental evaluation of farming systems is increasingly demanded when proposing new developments of animal farming systems. Modelling is a promising approach to reduce the cost and the delay in studying the relationship between farming management and risky emissions. The simulation of decision is essential to better analyze ex ante changes in farm management, but is rarely considered in environmental models. MELODIE simulates the flows of carbon, nitrogen, phosphorus, copper, zinc and water within the whole pig and dairy farm over the long term. MELODIE upscales dynamic models developed at the field or animal scale by considering the management of the whole farm system coherently with the livestock farming system. The model is structured according to an ontology of agricultural production systems to represent the interactions between the biotechnical system and the decision system. The biotechnical module simulates the nutrient flows at a daily time step for each entity of the sub-models (soil/crop, animal and manure processes). MELODIE represents decisions at two time scales: every year, for drawing annual activity plans and every day for the context-dependent application of this plan. Thanks to the interactions between the biotechnical system and the decision system at different time scales, MELODIE is able to run consistently under different long-term climate series. The goal is to study the emerging properties of the system. Besides, because the nutrient flows within the farm are dynamically simulated, it is possible to study both the spatial and temporal heterogeneity of the environmental risks. This approach enables a better understanding of variability in the farming system according to climate. MELODIE is intended for use in research, not as a decision support system for farm management. It is a framework for virtual experimentation on animal farming systems, and could be extended to deal with other issues than nutrient flows.