common

package
v0.0.0-...-a20b597 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 17, 2015 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

This package provides common functions used by all other mumax packages. Other source files will typically import this package anonymously:

import . "hotspin-core/common"

Index

Constants

View Source
const (
	ESC        = "\033["
	BOLD       = "\033[1m"
	BLACK      = "\033[30m"
	RED        = "\033[31m"
	GREEN      = "\033[32m"
	YELLOW     = "\033[33m"
	BLUE       = "\033[34m"
	MAGENTA    = "\033[35m"
	CYAN       = "\033[36m"
	WHITE      = "\033[37m"
	RESET      = "\033[0m" // No formatting
	ERASE      = "\033[K"  // Erase rest of line
	LINEUP     = "\033[1A"
	LINEDOWN   = "\033[1B"
	LINEBEGIN  = "\033[?0E"
	HIDECURSOR = "\033[?25l"
	SHOWCURSOR = "\033[?25h"
)

ANSI escape sequences

View Source
const (
	PI     = 3.14159265358979323846264338327950288
	Mu0    = 4.0 * PI * 1e-7         // Permeability of vacuum in J/Am2
	Gamma0 = 2.211E5                 // Gyromagnetic ratio in m/As (actually γ*µ0)
	Kb     = 1.38064881313131313E-23 // Boltzmann's constant in J/K
	MuB    = 9.27400968202020202E-24 // Bohr magneton in Am^2
	H_bar  = 1.05457172647474747E-34 // Reduced Planck's Constant in J*s
	Na     = 6.02214129272727272e23  //Avogadro constant
)

Physical constants

View Source
const (
	ERR_NONE  = iota // Successful exit, no error.
	ERR_IO           // IO error
	ERR_INPUT        // Illegal input
	ERR_CUDA         // CUDA error
	ERR_BUG          // Bug
	ERR_PANIC        // Unspecified panic
)

Exit error code

View Source
const (
	X = 0
	Y = 1
	Z = 2
)

Indices for vector components

View Source
const (
	XX = 0
	YY = 1
	ZZ = 2
	YZ = 3
	XZ = 4
	XY = 5
	ZY = 6
	ZX = 7
	YX = 8
)

Linear indices for matrix components. E.g.: matrix[Y]Z is stored as list[YZ]

View Source
const (
	LOG_DEBUG_COL  = GREEN
	LOG_WARN_COL   = RED
	LOG_ERR_COL    = BOLD + RED
	LOG_NORMAL_COL = RESET
)
View Source
const MSG_ASSERTIONFAILED = "Assertion failed: %v line %v"
View Source
const MSG_WARNING = "Warning:"
View Source
const (
	SIZEOF_FLOAT = 8
)

C data type size in bytes.

Variables

View Source
var DEBUG bool = true

Global debug flag. Typical use: if DEBUG {Debug(...)}

View Source
var ErrString []string = []string{"Success", "I/O error", "Illegal input", "CUDA error", "Bug", "Unexpected panic"}

Human readable description of exit codes

View Source
var FullTensorIdx [3][3]int = [3][3]int{
	[3]int{XX, XY, XZ},
	[3]int{YX, YY, YZ},
	[3]int{ZX, ZY, ZZ}}

Maps the 3x3 indices of a matrix (K_ij) onto linear indices (Kxx, Kyy, Kzz, Kyz, Kxz, Kxy, Kzy, Kzx, Kyx)

View Source
var SymmTensorIdx [3][3]int = [3][3]int{
	[3]int{XX, XY, XZ},
	[3]int{XY, YY, YZ},
	[3]int{XZ, YZ, ZZ}}

Maps the 3x3 indices of a symmetric matrix (K_ij) onto a length 6 array containing the upper triangular part: (Kxx, Kyy, Kzz, Kyz, Kxz, Kxy)

View Source
var TensorIndex map[string]int = map[string]int{"XX": XX, "YY": YY, "ZZ": ZZ, "YZ": YZ, "XZ": XZ, "XY": XY, "ZY": ZY, "ZX": ZX, "YX": YX}

Maps string to tensor index

View Source
var TensorIndexStr []string = []string{"XX", "YY", "ZZ", "YZ", "XZ", "XY", "ZY", "ZX", "YX"}

Maps tensor index to string

View Source
var VectorIndex map[string]int = map[string]int{"X": X, "Y": Y, "Z": Z}

Maps sting to vector index

View Source
var VectorIndexStr []string = []string{"X", "Y", "Z"}

Maps vector index to string

Functions

func Abs32

func Abs32(x float64) float64

Absolute value.

func ArrayOffset

func ArrayOffset(array uintptr, index int) uintptr

Go equivalent of &array[index] (for a float array).

func Assert

func Assert(test bool)

Panics if test is false

func AssertEqual

func AssertEqual(a, b []int)

Panics if the slice are not equal. Used to check for equal tensor sizes.

func AssertMsg

func AssertMsg(test bool, msg string)

Panics if test is false, printing the message. NOTE: msg is string, not ...inteface{} to avoid spurious allocations

func Atob

func Atob(str string) bool

Safe strconv.Atob

func Atof32

func Atof32(s string) float64

Safe strconv.Atof32

func Atof64

func Atof64(str string) float64

Safe strconv.Atof64

func Atoi

func Atoi(s string) int

Safe strconv.Atoi

func Atoi64

func Atoi64(str string) int64

Safe strconv.Atoi64

func Buffer

func Buffer(out io.Writer) *bufio.Writer

Makes sure the Writer is buffered. If it is already a bufio.Writer, just return it. Otherwise, wrap it into a bufio.Writer

func CheckBug

func CheckBug(err error)

Raises Bug if err != nil

func CheckErr

func CheckErr(err error, code int)

Exits with the exit code if the error is not nil.

func CheckIO

func CheckIO(err error)

Raises IOErr if err != nil

func CheckInput

func CheckInput(err error)

Raises InputErr if err != nil

func CheckSize

func CheckSize(a, b []int)

Check if array sizes are equal. Panics if arrays a and b are different.

func CheckSize3

func CheckSize3(a, b, c []int)

Check if array sizes are equal. Panics if arrays a and b are different.

func DashExit

func DashExit()

Resume normal printing after previous Dashboard() calls.

func Dashboard

func Dashboard(msg ...interface{})

Used for showing "live" progress in the terminal. Prints the message but does not move the cursor down. The next call will replace the previous line. To resume normal printing, call DashExit() once.

func Debug

func Debug(msg ...interface{})

Log a debug message.

func DivUp

func DivUp(x, y int) int

Integer division rounded up

func EnableTimers

func EnableTimers(enable bool)

enable/disable global timers

func EqualSize

func EqualSize(a, b []int) bool

True if a and b are equal. Used to check for equal array sizes.

func Err

func Err(msg ...interface{})

Log an Error.

func ErrCat

func ErrCat(a, b error) error

Combines two Errors into one. If a and b are nil, the returned error is nil. If either is not nil, it is returned. If both are not nil, the first one is returned.

func FileExists

func FileExists(file string) bool

Checks if the file exists. TODO: use Stat() instead of Open()

func GetExecDir

func GetExecDir() string

Gets the directory where the executable is located.

func GetTime

func GetTime(tag string) float64

Get the total run time (in seconds) of a global timer

func IdxToIJ

func IdxToIJ(idx int) (i, j int)

Maps a linear index onto matrix indices i,j

func InitLogger

func InitLogger(logfile string, options ...LogOption)

Initiates the logger and sets the log file. logfile == "" disables logging to file.

func IsFinite

func IsFinite(f float64) bool

True if not infinite, not NaN and not zero

func IsInf

func IsInf(f float64) bool

True if f is (positive or negative) infinity

func IsReal

func IsReal(f float64) bool

True if not infinite and not NaN

func Log

func Log(msg ...interface{})

Log normal output.

func LogFile

func LogFile(msg ...interface{})

Log to the log file only.

func Mkdir

func Mkdir(filename string)

Makes a directory. The permission is the same as the parent directory.

func Nanoseconds

func Nanoseconds() int64

Substitute for the old time.Nanoseconds(). Does not start from Epoch so only suited to measure durations.

func OpenRDONLY

func OpenRDONLY(filename string) *os.File

Opens the file, panics on error

func OpenWRAPPENDONLY

func OpenWRAPPENDONLY(filename string) *os.File

Opens the file, panics on error

func OpenWRONLY

func OpenWRONLY(filename string) *os.File

Opens the file, panics on error

func Parent

func Parent(filename string) string

returns the parent directory of a file

func PrintProgress

func PrintProgress(prog, total int, msg string)

func PrintTimers

func PrintTimers()

Print names and runtime of all global timers

func Prod

func Prod(a []int) int

Product of ints.

func Progress

func Progress(progress, total int, msg string)

Set progress bar to progress/total and display msg if GUI is up and running.

func Readlink(name string) string

Safe os.Readlink

func ReplaceExt

func ReplaceExt(filename, newext string) string

Replaces the extension of filename by a new one.

func ResetTimer

func ResetTimer(tag string)

Re-set the timer to its initial state

func SetDebugHook

func SetDebugHook(f func())

DEBUG: calls f before and after every Debug()

func SetProgress

func SetProgress(f func(int, int, string))

Sets the function to be used internally by Progress. Avoids cyclic dependency on engine.

func ShortPrint

func ShortPrint(a interface{}) string

Like fmt.Sprint with a maximum length. Used to limit the length of error messages that contain, e.g., a large array.

func Sqrt32

func Sqrt32(x float64) float64

Square root.

func Start

func Start(tag string)

Start a global timer with tag name

func Stop

func Stop(tag string)

Stop a global timer with tag name

func SwapIndex

func SwapIndex(index, dim int) int

Transforms the index between user and program space, unless it is a scalar:

X  <-> Z
Y  <-> Y
Z  <-> X
XX <-> ZZ
YY <-> YY
ZZ <-> XX
YZ <-> XY
XZ <-> XZ
XY <-> YZ

func SwapXYZ

func SwapXYZ(array []float64)

Swaps the X-Z values of the array. This transforms from user to program space and vice-versa.

func Warn

func Warn(msg ...interface{})

Log a warning.

Types

type Bug

type Bug string

An unexpected error occurred which should be reported

func BugF

func BugF(msg ...interface{}) Bug

Shorthand for Bug(fmt.Sprint(msg...))

func (*Bug) Bug

func (e *Bug) Bug()

Empty function implements interface{Bug()}

func (Bug) String

func (e Bug) String() string

type IOErr

type IOErr string

A file could not be read/written

func IOErrF

func IOErrF(msg ...interface{}) IOErr

Shorthand for IOErr(fmt.Sprint(msg...))

func (*IOErr) IOErr

func (e *IOErr) IOErr()

Empty function implements interface{IOErr()}

func (IOErr) String

func (e IOErr) String() string

type InputErr

type InputErr string

The input file contains illegal input

func InputErrF

func InputErrF(msg ...interface{}) InputErr

Shorthand for InputErr(fmt.Sprint(msg...))

func (*InputErr) InputErr

func (e *InputErr) InputErr()

Empty function implements interface{InputErr()}

func (InputErr) String

func (e InputErr) String() string

type LogOption

type LogOption int
const (
	LOG_NOSTDOUT LogOption = 1 << iota
	LOG_NODEBUG  LogOption = 1 << iota
	LOG_NOWARN   LogOption = 1 << iota
)

type Logger

type Logger struct {
	ShowDebug   bool        // Include debug messages in stderr output?
	ShowWarn    bool        // Include warnings in stderr output?
	ShowPrint   bool        // Include normal output in stderr output?
	Screen      *log.Logger // Logs to the screen (stderr), usually prints only limited output
	File        *log.Logger // Logs to a log file, usually prints all output (including debug)
	Initialized bool        // If the logger is not initialized, dump output to stderr.
}

INTERNAL

func (*Logger) Init

func (l *Logger) Init(logfile string, options ...LogOption)

INTERNAL Initiates the logger and sets a log file.

type Size

type Size []int

fmt.Stringer for slices representing a size.

func (Size) String

func (s Size) String() string

Human readable size "Z x Y x X"

type Timer

type Timer struct {
	StartNanos, TotalNanos int64 // StartTime == 0: not running
	Count                  int
}

Non-thread safe timer for debugging. The zero value is usable without initialization.

func (*Timer) Average

func (t *Timer) Average() float64

Average number of seconds per call.

func (*Timer) Seconds

func (t *Timer) Seconds() float64

Returns the total number of seconds this timer has been running. Correct even if the timer is running when this function is called.

func (*Timer) Start

func (t *Timer) Start()

Start the timer

func (*Timer) Stop

func (t *Timer) Stop()

Stop the timer

func (*Timer) String

func (t *Timer) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL