In regions of intensive pig and dairy farming, nutrient losses to the environment at farm level are a source of concern for water and air quality. Dynamic models are useful tools to evaluate the effects of production strategies on nutrient flows and losses to the environment. This paper presents the development of a new whole-farm model upscaling dynamic models developed at the field or animal scale. The model, called MELODIE, is based on an original structure with interacting biotechnical and decisional modules. Indeed, it is supported by an ontology of production systems and the associated programming platform DIESE. The biotechnical module simulates the nutrient flows in the different animal, soil and crops and manure sub-models. The decision module relies on an annual optimization of cropping and spreading allocation plans, and on the flexible execution of activity plans for each simulated year. These plans are examined every day by an operational management sub-model and their application is context dependent. As a result, MELODIE dynamically simulates the flows of carbon, nitrogen, phosphorus, copper, zinc and water within the whole farm over the short and long-term considering both the farming system and its adaptation to climatic conditions. Therefore, it is possible to study both the spatial and temporal heterogeneity of the environmental risks, and to test changes of practices and innovative scenarios. This is illustrated with one example of simulation plan on dairy farms to interpret the Nitrogen farm-gate budget indicator. It shows that this indicator is able to reflect small differences in Nitrogen losses between different systems, but it can only be interpreted using a mobile average, not on a yearly basis. This example illustrates how MELODIE could be used to study the dynamic behaviour of the system and the dynamic of nutrient flows. Finally, MELODIE can also be used for comprehensive multi-criterion assessments, and it also constitutes a generic and evolving framework for virtual experimentation on animal farming systems.