Search in sources :

Example 21 with NumberFormat

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

the class EditorUtils method saveComponent.

public static void saveComponent(ModelComponent comp) {
    Main main = Main.getMain();
    JFrame frame = main.getMainFrame();
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(main.getModelDirectory());
    int retVal = chooser.showSaveDialog(frame);
    if (retVal == JFileChooser.APPROVE_OPTION) {
        File file = chooser.getSelectedFile();
        try {
            ModelComponent ancestor = ComponentUtils.nearestEncapsulatingAncestor(comp);
            if (ancestor == null) {
                ancestor = comp;
            }
            ComponentUtils.saveComponent(file, comp, new NumberFormat("%.8g"), ancestor);
        } catch (Exception ex) {
            ex.printStackTrace();
            showError(frame, "Error saving file: " + ex.getMessage());
        }
        main.setModelDirectory(chooser.getCurrentDirectory());
    }
}
Also used : Main(artisynth.core.driver.Main) NumberFormat(maspack.util.NumberFormat)

Example 22 with NumberFormat

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

the class MechSystemSolver method KKTStaticFactorAndSolve.

/**
 * Solves a static KKT system of the form
 * <pre>{@code
 * -df/dx*Delta(x) -G^T*lambda - N^T*theta = f
 * G*Delta(x) + g = 0, N*Delta(x) + n >= 0
 * }</pre>
 *
 * @param u returned displacement Delta(x)
 * @param bf right-hand side net force
 * @param beta scale factor for any additional forces such as fictitious forces
 * @param btmp temporary vector
 */
public void KKTStaticFactorAndSolve(VectorNd u, VectorNd bf, double beta, VectorNd btmp) {
    updateStateSizes();
    int velSize = myActiveVelSize;
    boolean analyze = false;
    updateSolveMatrixStructure();
    if (myStaticKKTVersion != mySolveMatrixVersion) {
        myStaticKKTVersion = mySolveMatrixVersion;
        analyze = true;
    }
    SparseNumberedBlockMatrix S = mySolveMatrix;
    S.setZero();
    // add tikhonov regularization factor
    if (myStaticTikhonov > 0) {
        // identity
        for (int i = 0; i < S.numBlockRows(); ++i) {
            MatrixBlock bi = S.getBlock(i, i);
            for (int j = 0; j < bi.rowSize(); ++j) {
                bi.set(j, j, myStaticTikhonov);
            }
        }
    }
    myC.setZero();
    mySys.addPosJacobian(S, myC, -1);
    if (useFictitousJacobianForces && beta != 0) {
        bf.scaledAdd(beta, myC);
    }
    if (myStaticSolver == null) {
        myStaticSolver = new KKTSolver();
    }
    updateBilateralConstraints();
    if (myKKTGTVersion != myGTVersion) {
        analyze = true;
        myKKTGTVersion = myGTVersion;
    }
    // bilateral offsets
    // setBilateralOffsets (h, -a0); // -a0);
    // myVel.setSize (velSize);
    getBilateralDeviation(myBg);
    myRg.setZero();
    updateUnilateralConstraints();
    getUnilateralDeviation(myBn);
    // if (myStaticTikhonov < 0) {
    // double fn2 = S.frobeniusNormSquared();
    // if (myGsize > 0) {
    // fn2 += myGT.frobeniusNormSquared();
    // }
    // if (myNsize > 0) {
    // fn2 += myNT.frobeniusNormSquared();
    // }
    // double eps = Math.sqrt(0.1*Math.sqrt(fn2/velSize));
    // // add scaled identity
    // for (int i=0; i<S.numBlockRows(); ++i) {
    // MatrixBlock bi = S.getBlock(i, i);
    // for (int j=0; j<bi.rowSize(); ++j) {
    // bi.set(j,j, bi.get(j, j)+eps);
    // }
    // }
    // System.out.println("Tikhonov: " + eps);
    // }
    // get these in case we are doing hybrid solves and they are needed to
    // help with a warm start
    mySys.getBilateralImpulses(myLam);
    mySys.getUnilateralImpulses(myThe);
    if (!solveModePrinted) {
        String msg = (myHybridSolveP ? "hybrid solves" : "direct solves");
        if (mySys.getSolveMatrixType() == Matrix.INDEFINITE) {
            msg += ", unsymmetric matrix";
        } else {
            msg += ", symmetric matrix";
        }
        System.out.println(msg);
        solveModePrinted = true;
    }
    if (crsWriter == null && crsFileName != null) {
        try {
            crsWriter = ArtisynthIO.newIndentingPrintWriter(crsFileName);
        } catch (Exception e) {
            crsFileName = null;
        }
    }
    if (velSize != 0) {
        u.setZero();
        if (analyze) {
            myStaticSolver.analyze(S, velSize, myGT, myRg, mySys.getSolveMatrixType());
        }
        if (myHybridSolveP && !analyze && myNT.colSize() == 0) {
            if (profileKKTSolveTime) {
                timerStart();
            }
            myStaticSolver.factorAndSolve(S, velSize, myGT, myRg, u, myLam, bf, myBg, myHybridSolveTol);
            if (profileKKTSolveTime) {
                timerStop("KKTsolve(hybrid)");
            }
        } else {
            if (profileKKTSolveTime) {
                timerStart();
            }
            myStaticSolver.factor(S, velSize, myGT, myRg, myNT, myRn);
            // int nperturbed = myStaticSolver.getNumNonZerosInFactors();
            myStaticSolver.solve(u, myLam, myThe, bf, myBg, myBn);
            if (profileKKTSolveTime) {
                timerStop("KKTsolve");
            }
        }
        if (computeKKTResidual) {
            double res = myStaticSolver.residual(S, velSize, myGT, myRg, myNT, myRn, u, myLam, myThe, bf, myBg, myBn);
            System.out.println("vel residual (" + velSize + "," + myGT.colSize() + "," + myNT.colSize() + "): " + res);
        }
        if (crsWriter != null) {
            String msg = "# KKTsolve M=" + velSize + " G=" + myGT.colSize() + " N=" + myNT.colSize() + (analyze ? " ANALYZE" : "");
            System.out.println(msg);
            try {
                crsWriter.println(msg);
                myStaticSolver.printLinearProblem(crsWriter, bf, myBg, "%g", crsOmitDiag);
            } catch (Exception e) {
                e.printStackTrace();
                crsWriter = null;
                crsFileName = null;
            }
        }
    }
    mySys.setBilateralImpulses(myLam, 1);
    mySys.setUnilateralImpulses(myThe, 1);
    if (myUpdateForcesAtStepEnd) {
        if (myGsize > 0) {
            myGT.mulAdd(myFcon, myLam, velSize, myGsize);
        }
        if (myNsize > 0) {
            myNT.mulAdd(myFcon, myThe, velSize, myNsize);
        }
    }
    if (myLogWriter != null) {
        try {
            NumberFormat fmt = new NumberFormat("%g");
            myLogWriter.println("M(" + velSize + "x" + velSize + ")=[");
            S.write(myLogWriter, fmt, Matrix.WriteFormat.Dense, velSize, velSize);
            myLogWriter.println("];");
            myLogWriter.println("GT(" + velSize + "x" + myGT.colSize() + ")=[");
            myGT.write(myLogWriter, fmt, Matrix.WriteFormat.Dense, velSize, myGT.colSize());
            myLogWriter.println("];");
            myLogWriter.println("NT(" + velSize + "x" + myNT.colSize() + ")=[");
            myNT.write(myLogWriter, fmt, Matrix.WriteFormat.Dense, velSize, myNT.colSize());
            myLogWriter.println("];");
            myLogWriter.println("bf=[");
            bf.write(myLogWriter, fmt);
            myLogWriter.println("];");
            myLogWriter.println("myBg=[");
            myBg.write(myLogWriter, fmt);
            myLogWriter.println("];");
            myLogWriter.println("myBn=[");
            myBn.write(myLogWriter, fmt);
            myLogWriter.println("];");
            myLogWriter.println("u=[");
            u.write(myLogWriter, fmt);
            myLogWriter.println("];");
            myLogWriter.println("myLam=[");
            myLam.write(myLogWriter, fmt);
            myLogWriter.println("];");
            myLogWriter.println("myThe=[");
            myThe.write(myLogWriter, fmt);
            myLogWriter.println("];");
            myLogWriter.println("");
            myLogWriter.flush();
            System.out.println("logging");
        } catch (IOException e) {
            e.printStackTrace();
            myLogWriter = null;
        }
    }
}
Also used : MatrixBlock(maspack.matrix.MatrixBlock) KKTSolver(maspack.solvers.KKTSolver) IOException(java.io.IOException) SparseNumberedBlockMatrix(maspack.matrix.SparseNumberedBlockMatrix) InternalErrorException(maspack.util.InternalErrorException) IOException(java.io.IOException) NumberFormat(maspack.util.NumberFormat)

Example 23 with NumberFormat

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

the class MechSystemBase method writeBilateralConstraintMatrix.

public void writeBilateralConstraintMatrix(String fileName, Matrix.WriteFormat matfmt) throws IOException {
    PrintWriter pw = ArtisynthIO.newIndentingPrintWriter(fileName);
    SparseBlockMatrix GT = mySolver.createActiveBilateralMatrix(0);
    NumberFormat fmt = new NumberFormat("%g");
    GT.write(pw, fmt, matfmt);
    pw.close();
}
Also used : SparseBlockMatrix(maspack.matrix.SparseBlockMatrix) PrintWriter(java.io.PrintWriter) NumberFormat(maspack.util.NumberFormat)

Example 24 with NumberFormat

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

the class MechSystemBase method writeMassMatrix.

public void writeMassMatrix(String fileName, Matrix.WriteFormat matfmt) throws IOException {
    updatePosState();
    PrintWriter pw = ArtisynthIO.newIndentingPrintWriter(fileName);
    SparseBlockMatrix M = mySolver.createActiveMassMatrix(0);
    NumberFormat fmt = new NumberFormat("%g");
    M.write(pw, fmt, matfmt);
    pw.close();
}
Also used : SparseBlockMatrix(maspack.matrix.SparseBlockMatrix) PrintWriter(java.io.PrintWriter) NumberFormat(maspack.util.NumberFormat)

Example 25 with NumberFormat

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

the class MechSystemBase method writeStiffnessMatrix.

public void writeStiffnessMatrix(String fileName, double h, Matrix.WriteFormat matfmt) throws IOException {
    updatePosState();
    SparseBlockMatrix K = mySolver.createActiveStiffnessMatrix(h);
    PrintWriter pw = ArtisynthIO.newIndentingPrintWriter(fileName);
    int size = getActiveVelStateSize();
    NumberFormat fmt = new NumberFormat("%g");
    K.write(pw, fmt, matfmt, size, size);
    pw.close();
}
Also used : SparseBlockMatrix(maspack.matrix.SparseBlockMatrix) PrintWriter(java.io.PrintWriter) NumberFormat(maspack.util.NumberFormat)

Aggregations

NumberFormat (maspack.util.NumberFormat)55 PrintWriter (java.io.PrintWriter)15 IOException (java.io.IOException)8 Point3d (maspack.matrix.Point3d)6 BufferedWriter (java.io.BufferedWriter)5 File (java.io.File)5 PolygonalMesh (maspack.geometry.PolygonalMesh)5 Point (artisynth.core.mechmodels.Point)4 FileWriter (java.io.FileWriter)4 ArrayList (java.util.ArrayList)4 SparseBlockMatrix (maspack.matrix.SparseBlockMatrix)4 ReaderTokenizer (maspack.util.ReaderTokenizer)4 TestException (maspack.util.TestException)4 Face (maspack.geometry.Face)3 VectorNd (maspack.matrix.VectorNd)3 FunctionTimer (maspack.util.FunctionTimer)3 InternalErrorException (maspack.util.InternalErrorException)3 ContactPoint (artisynth.core.mechmodels.ContactPoint)2 PointParticleAttachment (artisynth.core.mechmodels.PointParticleAttachment)2 Font (java.awt.Font)2