We formulated a technique which combines the first-principles, micromagnetic and microstructural calculations and allows us to study the nature of hysteretic phenomena in hard magnets. Two distinct sources of coercivity in polytwinned CoPt type magnets, domain wall pinning at antiphase boundaries and splitting at twin boundaries, are illustrated for a realistic microstructure. Methodology of multiscale modelling of hysteretic phenomena in nanoscale magnets is discussed.