A computational model of ferroelectric domains. Part II: grain boundaries and defect pinning