The rapid development of information technologies in today's conditions makes it possible to control and automate processes and enterprises, institutions to ensure the correct and efficient operation of various energy systems. The mathematical apparatus used in the software for such objects and systems makes it possible to manage their states in various regular conditions. Sometimes unpredictable factors arise in the operation of energy facilities, which can lead to global catastrophes not only for a particular region, but also for all of humanity is the deliberate damage to the logic of the software that controls all the processes of the power system is one of these factors, for the purpose of terror or other malicious purposes. Such factors require the construction of models with which it is possible to predict the scale of risk and extent of damage, as well as to obtain a general estimate of the costs of protecting power system software against such malicious actions. An optimization mathematical model and a corresponding description of the implementation of a complex software tool for modeling the spread of malicious software (malware) in modern energy facilities and systems is the result of the work. The developed optimization mathematical model is based on the use of methods of optimization of functions and functionals with restrictions in the form of systems of ordinary differential equations with given corresponding initial conditions. To develop process simulation software modules based on the PSIDR mathematical model, stochastic population methods, models and algorithms were used to determine the control parameter at each time step. The use of such optimization methods and algorithms makes it possible to solve more complex tasks. It requires a procedure for predicting the spread of processes of various origins in general. The developed mathematical model consists in the minimization of costs for the purchase of antiviruses for the protection of relevant systems in energy facilities and systems. Keywords: malware, prediction, optimization, stochastic model, deterministic model, cellular automaton, energy objects