Search in sources :

Example 11 with ReaderTokenizer

use of maspack.util.ReaderTokenizer in project artisynth_core by artisynth.

the class IncompleteLUDecompositionTest method main.

public static void main(String[] args) {
    SparseMatrixNd A = new SparseMatrixNd(1, 1);
    Random random = new Random(51);
    try {
        A.scan(new ReaderTokenizer(new FileReader("/ubc/ece/home/hct/other/elliote/A.matrix")));
        System.out.println("read matrix " + A.rowSize() + " " + A.colSize());
    } catch (IOException e) {
        System.out.println(e.getMessage());
        System.exit(0);
    }
    int n = A.rowSize();
    System.out.println("loaded matrix");
    // int n = 400;
    // A.setSize(n, n);
    // SVDecomposition svd;
    // do
    // {
    // A.setRandom(1, 2, n*2, random);
    // for(int i = 0; i < n; i++)
    // A.set(i, i, random.nextDouble() + 1);
    // 
    // svd = new SVDecomposition(A);
    // }
    // while(svd.determinant() < 1e-10);
    // A.mulTranspose(A);
    // System.out.println("created spd matrix");
    // waitforuser();
    IncompleteLUDecomposition ilud = new IncompleteLUDecomposition();
    ilud.factor(A, 0.00001);
    if (ilud.L.containsNaN() || ilud.U.containsNaN()) {
        System.out.println("LU contains NaN");
    }
    System.out.println("factored matrix");
    // waitforuser();
    SparseMatrixNd tmp = new SparseMatrixNd(n, n);
    tmp.mul(ilud.L, ilud.U);
    // System.out.println(new MatrixNd(tmp));
    tmp.sub(A);
    System.out.println("residual matrix one norm " + tmp.oneNorm());
    VectorNd x = new VectorNd(n);
    VectorNd b = new VectorNd(n);
    VectorNd xc = new VectorNd(n);
    try {
        System.out.println("writing solve matrix and incomplete cholesky decomposition");
        PrintWriter fwr;
        fwr = new PrintWriter("/ubc/ece/home/hct/other/elliote/LUA.matrix");
        // fwr.append("[ ");
        A.write(fwr, new NumberFormat(), WriteFormat.Dense);
        // fwr.append(" ]");
        fwr.close();
        fwr = new PrintWriter("/ubc/ece/home/hct/other/elliote/L.matrix");
        // fwr.append("[ ");
        ilud.L.write(fwr, new NumberFormat(), WriteFormat.Dense);
        // fwr.append(" ]");
        fwr.close();
        fwr = new PrintWriter("/ubc/ece/home/hct/other/elliote/U.matrix");
        // fwr.append("[ ");
        ilud.U.write(fwr, new NumberFormat(), WriteFormat.Dense);
        // fwr.append(" ]");
        fwr.close();
        System.out.println("finished writing");
    } catch (IOException e) {
        System.out.println(e.getMessage());
        System.exit(0);
    }
    // test backsolves
    System.out.println();
    CGSolver isolver = new CGSolver();
    DirectSolver dsolver = new UmfpackSolver();
    // dsolver.factor(A);
    b.setRandom(-1, 1, random);
    System.out.println("solving L * x = b");
    ilud.solveL(x, b);
    dsolver.analyzeAndFactor(ilud.L);
    dsolver.solve(xc, b);
    System.out.println("b " + b);
    System.out.println("x " + x);
    System.out.println("xc " + xc);
    if (!x.epsilonEquals(xc, 1e-6)) {
        System.out.println("backsolve failed");
    }
    System.out.println();
    System.out.println("solving U * x = b");
    ilud.solveU(x, b);
    dsolver.analyzeAndFactor(ilud.U);
    dsolver.solve(xc, b);
    System.out.println("b " + b);
    System.out.println("x " + x);
    System.out.println("xc " + xc);
    if (!x.epsilonEquals(xc, 1e-6)) {
        System.out.println("backsolve failed");
    }
    // test upcg solver
    System.out.println();
    System.out.println("solving A * x = b");
    double tol = 1e-2;
    int maxit = 1500;
    System.out.println("preconditioned solve untransformed");
    x.setZero();
    isolver.solve(x, A, b, tol, maxit, ilud);
    System.out.println("iterations " + isolver.getNumIterations());
    System.out.println("b " + b);
    System.out.println("x " + x);
    System.out.println();
    // System.out.println("preconditioned solve transformed");
    // x.setZero();
    // isolver.solveTransformed(x, A, b, tol, maxit, icd);
    // System.out.println("iterations " + isolver.getNumIterations());
    // System.out.println("b " + b);
    // System.out.println("x " + x);
    // 
    // System.out.println();
    System.out.println("unpreconditioned solve");
    x.setZero();
    isolver.solve(x, A, b, tol, maxit);
    System.out.println("iterations " + isolver.getNumIterations());
    System.out.println("b " + b);
    System.out.println("x " + x);
    System.out.println();
    System.out.println("direct solve");
    x.setZero();
    dsolver.analyzeAndFactor(A);
    dsolver.solve(x, b);
    System.out.println("b " + b);
    System.out.println("x " + x);
}
Also used : IOException(java.io.IOException) Random(java.util.Random) SparseMatrixNd(maspack.matrix.SparseMatrixNd) ReaderTokenizer(maspack.util.ReaderTokenizer) VectorNd(maspack.matrix.VectorNd) FileReader(java.io.FileReader) PrintWriter(java.io.PrintWriter) NumberFormat(maspack.util.NumberFormat)

Example 12 with ReaderTokenizer

use of maspack.util.ReaderTokenizer in project artisynth_core by artisynth.

the class GridResolutionField method textToValue.

public Object textToValue(String text, BooleanHolder corrected, StringHolder errMsg) {
    corrected.value = false;
    ReaderTokenizer rtok = new ReaderTokenizer(new StringReader(text));
    double cellSize = 1;
    int numDivisions = 1;
    text = text.trim();
    if (text.equals("*")) {
        // return a resolution with 0 cellSize, indicating auto-sizing.
        return validValue(new GLGridResolution(0, 1), errMsg);
    }
    if (isBlank(text)) {
        return illegalValue("Void value not permited", errMsg);
    }
    try {
        if (rtok.nextToken() != ReaderTokenizer.TT_NUMBER) {
            return illegalValue("Improperly formed cell size", errMsg);
        }
        cellSize = rtok.nval;
        if (rtok.nextToken() == ReaderTokenizer.TT_EOF) {
            return validValue(new GLGridResolution(cellSize, 1), errMsg);
        } else if (rtok.ttype != '/') {
            return illegalValue("Expecting '/' after cell size", errMsg);
        }
        if (rtok.nextToken() != ReaderTokenizer.TT_NUMBER) {
            return illegalValue("Expecting number of cell divisions after '/'", errMsg);
        }
        if (rtok.nval < 1) {
            return illegalValue("number of cell divisions must be positive integer", errMsg);
        }
        numDivisions = (int) rtok.nval;
        if (numDivisions != rtok.nval) {
            corrected.value = true;
        }
        return validValue(new GLGridResolution(cellSize, numDivisions), errMsg);
    } catch (Exception e) {
        return illegalValue("Improperly formed resolution", errMsg);
    }
}
Also used : ReaderTokenizer(maspack.util.ReaderTokenizer) StringReader(java.io.StringReader) GLGridResolution(maspack.render.GL.GLGridResolution) IOException(java.io.IOException)

Example 13 with ReaderTokenizer

use of maspack.util.ReaderTokenizer in project artisynth_core by artisynth.

the class ScaleField method textToValue.

public Object textToValue(String text, BooleanHolder corrected, StringHolder errMsg) {
    if (isBlank(text)) {
        return setVoidIfPossible(errMsg);
    }
    ReaderTokenizer rtok = new ReaderTokenizer(new StringReader(text));
    double[] tmp = new double[3];
    try {
        if (rtok.nextToken() != ReaderTokenizer.TT_NUMBER) {
            return illegalValue("Missing or malformed number", errMsg);
        }
        tmp[0] = rtok.nval;
        if (rtok.nextToken() != ReaderTokenizer.TT_EOF) {
            rtok.pushBack();
            for (int i = 1; i < 3; i++) {
                if (rtok.nextToken() != ReaderTokenizer.TT_NUMBER) {
                    return illegalValue("Missing or malformed number for scale element " + i, errMsg);
                }
                tmp[i] = rtok.nval;
            }
            if (rtok.nextToken() != ReaderTokenizer.TT_EOF) {
                return illegalValue("Extra characters after third scale element", errMsg);
            }
            return validValue(new Vector3d(tmp[0], tmp[1], tmp[2]), errMsg);
        } else {
            return validValue(new Double(tmp[0]), errMsg);
        }
    } catch (IOException e) {
        return illegalValue("Improperly formed scale value(s)", errMsg);
    }
}
Also used : Vector3d(maspack.matrix.Vector3d) ReaderTokenizer(maspack.util.ReaderTokenizer) StringReader(java.io.StringReader) IOException(java.io.IOException)

Example 14 with ReaderTokenizer

use of maspack.util.ReaderTokenizer in project artisynth_core by artisynth.

the class LibraryInstaller method readLibs.

public void readLibs(File file) throws IOException {
    ReaderTokenizer rtok = null;
    if (file == null) {
        // open from standard input
        rtok = new ReaderTokenizer(new InputStreamReader(System.in));
    } else {
        rtok = ArtisynthIO.newReaderTokenizer(file);
    }
    try {
        rtok.wordChars("-");
        rtok.eolIsSignificant(true);
        while (rtok.nextToken() != ReaderTokenizer.TT_EOF) {
            rtok.pushBack();
            readLine(rtok);
        }
    } catch (Exception e) {
        if (file == null) {
            throw new IOException("Error reading input: " + e.getMessage(), e);
        } else {
            throw new IOException("Error reading file" + file + ": " + e.getMessage(), e);
        }
    } finally {
        if (rtok != null) {
            rtok.close();
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ReaderTokenizer(maspack.util.ReaderTokenizer) IOException(java.io.IOException) IOException(java.io.IOException) URIxSyntaxException(maspack.fileutil.uri.URIxSyntaxException)

Example 15 with ReaderTokenizer

use of maspack.util.ReaderTokenizer in project artisynth_core by artisynth.

the class Main method loadModelFile.

public boolean loadModelFile(File file) throws IOException {
    // the Jython console.
    if (myViewer != null && !SwingUtilities.isEventDispatchThread()) {
        LoadModelFileRunner runner = new LoadModelFileRunner(file);
        if (!runInSwing(runner)) {
            return false;
        } else {
            return runner.getStatus();
        }
    }
    RootModel newRoot = null;
    clearRootModel();
    ReaderTokenizer rtok = ArtisynthIO.newReaderTokenizer(file);
    if (rtok.nextToken() == ReaderTokenizer.TT_WORD) {
        try {
            newRoot = (RootModel) ClassAliases.newInstance(rtok.sval, RootModel.class);
            myModelFile = file;
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (newRoot == null) {
            throw new IOException("cannot create instance of " + rtok.sval);
        }
    } else {
        rtok.pushBack();
        newRoot = new RootModel();
    }
    // getWorkspace().getWayPoints().clear();
    long t0 = System.nanoTime();
    ScanWriteUtils.scanfull(rtok, newRoot, newRoot);
    long t1 = System.nanoTime();
    System.out.println("File scan time: " + ((t1 - t0) * 1e-9) + " sec");
    System.out.println("File size: " + file.length());
    // System.out.println ("queue size=" + ModelComponentBase.scanQueueSize());
    rtok.close();
    String modelName = newRoot.getName();
    if (modelName == null) {
        // use file name with extension stripped off
        modelName = file.getName();
        int dotIdx = modelName.indexOf('.');
        if (dotIdx != -1) {
            modelName = modelName.substring(0, dotIdx);
        }
    }
    setRootModel(newRoot, modelName, null);
    return true;
}
Also used : RootModel(artisynth.core.workspace.RootModel) ReaderTokenizer(maspack.util.ReaderTokenizer) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(maspack.util.InternalErrorException) WayPoint(artisynth.core.probes.WayPoint)

Aggregations

ReaderTokenizer (maspack.util.ReaderTokenizer)44 IOException (java.io.IOException)19 Point3d (maspack.matrix.Point3d)19 BufferedReader (java.io.BufferedReader)14 ArrayList (java.util.ArrayList)14 FileReader (java.io.FileReader)13 StringReader (java.io.StringReader)7 VectorNd (maspack.matrix.VectorNd)6 FileNotFoundException (java.io.FileNotFoundException)5 PolygonalMesh (maspack.geometry.PolygonalMesh)5 InputStreamReader (java.io.InputStreamReader)4 Vector3d (maspack.matrix.Vector3d)4 NumberFormat (maspack.util.NumberFormat)4 PrintStream (java.io.PrintStream)3 Date (java.util.Date)3 TestException (maspack.util.TestException)3 File (java.io.File)2 PrintWriter (java.io.PrintWriter)2 DateFormat (java.text.DateFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2