Skip to content

Commit

Permalink
start merging dot.go and customfield.go
Browse files Browse the repository at this point in the history
  • Loading branch information
barnex committed Sep 1, 2016
1 parent 9859625 commit 5dda379
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 62 deletions.
5 changes: 5 additions & 0 deletions cuda/madd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ func Mul(dst, a, b *data.Slice) {
}
}

// Add: dst = src1 + src2.
func Add(dst, src1, src2 *data.Slice) {
Madd2(dst, src1, src2, 1, 1)
}

// multiply-add: dst[i] = src1[i] * factor1 + src2[i] * factor2
func Madd2(dst, src1, src2 *data.Slice, factor1, factor2 float32) {
N := dst.Len()
Expand Down
2 changes: 1 addition & 1 deletion engine/anisotropy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var (
AnisU = NewVectorParam("anisU", "", "Uniaxial anisotropy direction")
AnisC1 = NewVectorParam("anisC1", "", "Cubic anisotropy direction #1")
AnisC2 = NewVectorParam("anisC2", "", "Cubic anisotorpy directon #2")
B_anis = NewVectorField("B_anis", "T", "Anisotropy filed", AddAnisotropyField)
B_anis = NewVectorField("B_anis", "T", "Anisotropy field", AddAnisotropyField)
Edens_anis = NewScalarField("Edens_anis", "J/m3", "Anisotropy energy density", AddAnisotropyEnergyDensity)
E_anis = NewScalarValue("E_anis", "J", "total anisotropy energy", GetAnisotropyEnergy)
)
Expand Down
104 changes: 104 additions & 0 deletions engine/customfield.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package engine

// Add arbitrary terms to H_eff

import (
"github.com/mumax/3/cuda"
"github.com/mumax/3/data"
)

var (
B_custom = NewVectorField("B_custom", "T", "User-defined field", AddCustomField)
Edens_custom = NewScalarField("Edens_custom", "J/m3", "Energy density of user-defined field.", AddCustomEnergyDensity)
E_custom = NewScalarValue("E_custom", "J", "total energy of user-defined field", GetCustomEnergy)
customTerms []VectorField
customEnergies []ScalarField
)

func init() {
DeclFunc("AddFieldTerm", AddFieldTerm, "Add an expression to B_eff.")
}

func AddFieldTerm(b outputField) {
customTerms = append(customTerms, AsVectorField(b))
}

// AddCustomField evaluates the user-defined custom field terms
// and adds the result to dst.
func AddCustomField(dst *data.Slice) {
for _, term := range customTerms {
buf, recycle := term.Slice()
cuda.Add(dst, dst, buf)
if recycle {
cuda.Recycle(buf)
}
}
}

// AddCustomField evaluates the user-defined custom energy density terms
// and adds the result to dst.
func AddCustomEnergyDensity(dst *data.Slice) {

}

func GetCustomEnergy() float64 {
buf := cuda.Buffer(1, Edens_custom.Mesh().Size())
defer cuda.Recycle(buf)
cuda.Zero(buf)
AddCustomEnergyDensity(buf)
return cellVolume() * float64(cuda.Sum(buf))
}

func init() {
DeclFunc("DotProduct", DotProduct, "Dot product of two vector quantities")
}

type dotProduct struct {
a, b outputField
}

// DotProduct creates a new quantity that is the dot product of
// quantities a and b. E.g.:
// DotProct(&M, &B_ext)
func DotProduct(a, b outputField) *dotProduct {
return &dotProduct{a, b}
}

func (d *dotProduct) Mesh() *data.Mesh {
return d.a.Mesh()
}

func (d *dotProduct) NComp() int {
return 1
}

func (d *dotProduct) Name() string {
return d.a.Name() + "_dot_" + d.b.Name()
}

func (d *dotProduct) Unit() string {
return d.a.Unit() + d.b.Unit()
}

func (d *dotProduct) Slice() (*data.Slice, bool) {
slice := cuda.Buffer(d.NComp(), d.Mesh().Size())
cuda.Zero(slice)
A, r := d.a.Slice()
if r {
defer cuda.Recycle(A)
}
B, r := d.b.Slice()
if r {
defer cuda.Recycle(B)
}
cuda.AddDotProduct(slice, 1, A, B)
return slice, true
}

func (d *dotProduct) average() []float64 {
return qAverageUniverse(d)
}

func (d *dotProduct) Average() float64 {
return d.average()[0]
}
60 changes: 0 additions & 60 deletions engine/dot.go

This file was deleted.

2 changes: 1 addition & 1 deletion engine/temperature.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func init() {
func (b *thermField) AddTo(dst *data.Slice) {
if !Temp.isZero() {
b.update()
cuda.Madd2(dst, dst, b.noise, 1, 1)
cuda.Add(dst, dst, b.noise)
}
}

Expand Down

0 comments on commit 5dda379

Please sign in to comment.