This paper presents a numerical procedure for achieving desired features of a melt undergoing solidification by applying an external magnetic field whose intensity and spatial distribution are obtained by the use of a hybrid optimization algorithm. The intensities of the magnets along the boundaries of the container are described as B-splines. The inverse problem is then formulated as to find the magnetic boundary conditions (the coefficients of the B-splines) in such a way that the gradients of temperature along the gravity direction are minimized. For this task, a hybrid optimization code was used that incorporates several of the most popular optimization modules; the Davidon-Fletcher-Powell (DFP) gradient method, a genetic algorithm (GA), the Nelder-Mead (NM) simplex method, quasi-Newton algorithm of Pshenichny-Danilin (LM), differential evolution (DE), and sequential quadratic programming (SQP). Transient Navier-Stokes and Maxwell equations were discretized using finite volume method in a generalized curvilinear non-orthogonal coordinate system. For the phase change problems, an enthalpy formulation was used. The code was validated against analytical and numerical benchmark results with very good agreements in both cases.