Search in sources :

Example 31 with NumberFormat

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

the class AnsysWriter method writeNodeFile.

public static void writeNodeFile(FemModel3d fem, PrintWriter nodeWriter) {
    NumberFormat fmt1 = new NumberFormat("%8i");
    NumberFormat fmt2 = new NumberFormat("%19.13G");
    int offset = (fem.getByNumber(0) == null) ? 0 : 1;
    for (FemNode3d n : fem.getNodes()) {
        nodeWriter.print(fmt1.format(n.getNumber() + offset));
        nodeWriter.println(" " + fmt2.format(n.getPosition().x) + " " + fmt2.format(n.getPosition().y) + " " + fmt2.format(n.getPosition().z));
    }
    nodeWriter.close();
}
Also used : NumberFormat(maspack.util.NumberFormat)

Example 32 with NumberFormat

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

the class AnsysWriter method writeMarkerFile.

public static void writeMarkerFile(FemModel3d fem, String fileName) {
    NumberFormat fmt1 = new NumberFormat("%8i");
    NumberFormat fmt2 = new NumberFormat("%20.13G");
    try {
        PrintWriter pw = new PrintWriter(new File(fileName));
        HashMap<Integer, Point3d> markerMap = new HashMap<Integer, Point3d>();
        for (FemMarker m : fem.markers()) {
            markerMap.put(m.getNumber(), m.getPosition());
        }
        // for (FemElement3d e : fem.getElements ()) {
        // for (FemMarker m : fem.getMarkersInElement (e)) {
        // markerMap.put (m.getNumber (), m.getPosition ());
        // }
        // }
        SortedSet<Integer> sortedIds = new TreeSet<Integer>(markerMap.keySet());
        Iterator<Integer> it = sortedIds.iterator();
        while (it.hasNext()) {
            int markerId = it.next();
            Point3d pos = markerMap.get(markerId);
            pw.println(fmt1.format(markerId) + " " + pos.toString(fmt2));
        }
        pw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) Point3d(maspack.matrix.Point3d) TreeSet(java.util.TreeSet) File(java.io.File) NumberFormat(maspack.util.NumberFormat) PrintWriter(java.io.PrintWriter)

Example 33 with NumberFormat

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

the class MechSystemSolver method KKTFactorAndSolve.

/**
 * Solves a KKT system in which the Jacobian augmented M matrix and
 * and force vectors are given by
 * <pre>
 * M' = M + a0 df/dv + a1 df/dx
 *
 * bf' = bf + (a2 df/dv + a3 df/dx) vel0
 * </pre>
 * It is assumed that a0 and a1 are both non-zero. It is also assumed that
 * the a0 = -alpha h, where h is the step size and alpha indicates the
 * propertion of implicitness for the solve; i.e., for regular backward
 * euler, alpha=1, while for trapezoidal solves, alpha = 0.5;
 *
 * When used to solve for velocities in an implicit integrator, then
 * on input, bf is assumed to be given by
 * <pre>
 * bf = M vel0 + h f
 * </pre>
 * where h is the time step and f is the generalized forces, while
 * on output bf is modified to include the Jacobian terms described
 * above.
 *
 * @param vel returns the computed velocity
 * @param fpar if useFictitousJacobianForces is true, returns fictitious
 * Jacobian forces for parametric components
 * @param bf right side offset
 * @param btmp temporary vector
 * @param vel0 right side velocity
 * @param h interval time step - used to scale constraint offsets and
 * impulses
 * @param a0 left side df/dv coefficient
 * @param a1 left side df/dx coefficient
 * @param a2 right side df/dv coefficient
 * @param a3 right side df/dx coefficient
 */
public void KKTFactorAndSolve(VectorNd vel, VectorNd fpar, VectorNd bf, VectorNd btmp, VectorNd vel0, double h, double a0, double a1, double a2, double a3) {
    // assumes that updateMassMatrix() has been called
    updateStateSizes();
    int velSize = myActiveVelSize;
    boolean analyze = false;
    updateSolveMatrixStructure();
    if (myKKTSolveMatrixVersion != mySolveMatrixVersion) {
        myKKTSolveMatrixVersion = mySolveMatrixVersion;
        analyze = true;
    }
    SparseNumberedBlockMatrix S = mySolveMatrix;
    S.setZero();
    myC.setSize(S.rowSize());
    myC.setZero();
    mySys.addVelJacobian(S, myC, a0);
    // System.out.println ("myC=" + myC);
    if (useFictitousJacobianForces) {
        bf.scaledAdd(-a0, myC);
        if (fpar != null && myParametricVelSize > 0) {
            setSubVector(fpar, myC, velSize, myParametricVelSize);
        }
    }
    if (vel0 != null) {
        double alpha = a2 / a0 - a3 / a1;
        S.mul(btmp, vel0, velSize, velSize);
        bf.scaledAdd(alpha, btmp);
    }
    myC.setZero();
    mySys.addPosJacobian(S, myC, a1);
    if (useFictitousJacobianForces) {
        bf.scaledAdd(-a0, myC);
        if (fpar != null && myParametricVelSize > 0) {
            addSubVector(fpar, myC, velSize, myParametricVelSize);
        }
    }
    if (vel0 != null && a3 != 0) {
        double beta = a3 / a1;
        S.mul(btmp, vel0, velSize, velSize);
        bf.scaledAdd(beta, btmp);
    }
    addActiveMassMatrix(mySys, S);
    if (velSize > 0 && myParametricVelSize > 0) {
        S.mulTranspose(btmp, myUpar, 0, velSize, velSize, myParametricVelSize);
        bf.sub(btmp);
    }
    if (myKKTSolver == null) {
        myKKTSolver = new KKTSolver();
    }
    updateBilateralConstraints();
    if (myKKTGTVersion != myGTVersion) {
        analyze = true;
        myKKTGTVersion = myGTVersion;
    }
    if (myGsize > 0 && myParametricVelSize > 0) {
        myGT.mulTranspose(myBg, myUpar, 0, myGsize, velSize, myParametricVelSize);
        // move to rhs
        myBg.negate();
    } else {
        myBg.setZero();
    }
    // a0 is assumed to be negative, which moves myGdot over to the rhs
    // myBg.scaledAdd (a0, myGdot);
    // -a0);
    setBilateralOffsets(h, -a0);
    updateUnilateralConstraints();
    if (myNsize > 0 && myParametricVelSize > 0) {
        myNT.mulTranspose(myBn, myUpar, 0, myNsize, velSize, myParametricVelSize);
        // move to rhs
        myBn.negate();
    } else {
        myBn.setZero();
    }
    // a0 is assumed to be negative, which moves myNdot over to the rhs
    // -a0);
    setUnilateralOffsets(h, -a0);
    // 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) {
        if (vel0 != null) {
            // set vel to vel0 in case the solver needs a warm start
            vel.set(vel0);
        }
        if (analyze) {
            myKKTSolver.analyze(S, velSize, myGT, myRg, mySys.getSolveMatrixType());
        }
        if (myHybridSolveP && !analyze && myNT.colSize() == 0) {
            if (profileKKTSolveTime) {
                timerStart();
            }
            myKKTSolver.factorAndSolve(S, velSize, myGT, myRg, vel, myLam, bf, myBg, myHybridSolveTol);
            if (profileKKTSolveTime) {
                timerStop("KKTsolve(hybrid)");
            }
        } else {
            if (profileKKTSolveTime) {
                timerStart();
            }
            myKKTSolver.factor(S, velSize, myGT, myRg, myNT, myRn);
            myKKTSolver.solve(vel, myLam, myThe, bf, myBg, myBn);
            if (profileKKTSolveTime) {
                timerStop("KKTsolve");
            }
        }
        if (computeKKTResidual) {
            double res = myKKTSolver.residual(S, velSize, myGT, myRg, myNT, myRn, vel, 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);
                myKKTSolver.printLinearProblem(crsWriter, bf, myBg, "%g", crsOmitDiag);
            } catch (Exception e) {
                e.printStackTrace();
                crsWriter = null;
                crsFileName = null;
            }
        }
    }
    mySys.setBilateralImpulses(myLam, h);
    mySys.setUnilateralImpulses(myThe, h);
    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.SYMMETRIC_CRS, velSize, velSize);
            myLogWriter.println("];");
            myLogWriter.println("GT(" + velSize + "x" + myGT.colSize() + ")=[");
            myGT.write(myLogWriter, fmt, Matrix.WriteFormat.CRS, velSize, myGT.colSize());
            myLogWriter.println("];");
            myLogWriter.println("NT(" + velSize + "x" + myNT.colSize() + ")=[");
            myNT.write(myLogWriter, fmt, Matrix.WriteFormat.CRS, 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("vel=[");
            vel.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 : 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 34 with NumberFormat

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

the class ContactConstraint method toString.

public String toString(String fmtStr) {
    NumberFormat fmt = new NumberFormat(fmtStr);
    String str = "[ pnt0=" + myCpnt0.toString(fmtStr);
    str += " pnt1=" + myCpnt1.toString(fmtStr);
    str += " dist=" + fmt.format(myDistance) + " ]";
    return str;
}
Also used : NumberFormat(maspack.util.NumberFormat)

Example 35 with NumberFormat

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

the class TextTest method build.

@Override
public void build(String[] args) throws IOException {
    super.build(args);
    final HashMap<String, Font> fontMap = new HashMap<>();
    for (Font font : GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts()) {
        fontMap.put(font.getName(), font);
    }
    TextComponent3d text = new TextComponent3d("hello world");
    text.setFont(new Font(Font.SANS_SERIF, 0, 54));
    text.setText("Hello world! And goodnight to all.");
    text.setPosition(new Point3d(0.4f, 0.3f, 0.3f));
    text.setTextSize(0.25);
    text.setTextColor(Color.WHITE);
    text.setFollowEye(false);
    RenderProps.setFaceStyle(text, FaceStyle.FRONT_AND_BACK);
    addRenderable(text);
    text = new TextComponent3d("alphabet");
    text.setFont(new Font(Font.SANS_SERIF, 0, 54));
    text.setText("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
    text.setPosition(new Point3d(-2f, 0f, 0f));
    text.setTextSize(0.25);
    text.setTextColor(Color.ORANGE);
    text.setFollowEye(false);
    RenderProps.setFaceStyle(text, FaceStyle.FRONT_AND_BACK);
    addRenderable(text);
    Font comic = fontMap.get("Comic Sans MS");
    if (comic == null) {
        comic = new Font(Font.MONOSPACED, Font.BOLD, 32);
    } else {
        comic = comic.deriveFont(Font.BOLD, 32);
    }
    text = new TextComponent3d("mikey");
    text.setFont(new Font(Font.SANS_SERIF, 0, 54));
    text.setText("Cowabunga");
    text.setPosition(new Point3d(0, 0.5f, 0f));
    text.setTextSize(0.25);
    text.setTextColor(Color.CYAN);
    text.setHorizontalAlignment(HorizontalAlignment.CENTRE);
    text.setFollowEye(false);
    RenderProps.setFaceStyle(text, FaceStyle.FRONT_AND_BACK);
    RenderProps.setShading(text, Shading.NONE);
    addRenderable(text);
    text = new TextComponent3d("flip");
    text.setFont(new Font(Font.SERIF, 0, 64));
    text.setText("Flip me!");
    text.setPosition(new Point3d(0, -0.44f, 0f));
    text.setOrientation(new AxisAngle(0, 1, 0, Math.toRadians(30)));
    text.setTextSize(0.3);
    text.setTextColor(Color.MAGENTA);
    text.setHorizontalAlignment(HorizontalAlignment.CENTRE);
    text.setFollowEye(false);
    RenderProps.setFaceStyle(text, FaceStyle.FRONT);
    addRenderable(text);
    text = new TextComponent3d("flip2");
    text.setFont(new Font(Font.SERIF, 0, 64));
    text.setText("Flip me!");
    text.setPosition(new Point3d(0, -0.44f, 0f));
    text.setOrientation(new AxisAngle(0, 1, 0, Math.toRadians(210)));
    text.setTextSize(0.3);
    text.setTextColor(Color.MAGENTA);
    text.setHorizontalAlignment(HorizontalAlignment.CENTRE);
    text.setFollowEye(false);
    RenderProps.setFaceStyle(text, FaceStyle.FRONT);
    addRenderable(text);
    PrintWriter writer = new PrintWriter(ArtisynthPath.getTempDir().getAbsolutePath() + "/texttest.txt");
    write(writer, new NumberFormat("%g"), null);
    writer.close();
    ArrayDeque<ScanToken> tokens = new ArrayDeque<>();
    ReaderTokenizer rtok = new ReaderTokenizer(new FileReader(ArtisynthPath.getTempDir().getAbsolutePath() + "/texttest.txt"));
    scan(rtok, tokens);
    rtok.close();
}
Also used : HashMap(java.util.HashMap) Font(java.awt.Font) ArrayDeque(java.util.ArrayDeque) TextComponent3d(artisynth.core.renderables.TextComponent3d) AxisAngle(maspack.matrix.AxisAngle) ScanToken(artisynth.core.util.ScanToken) Point3d(maspack.matrix.Point3d) ReaderTokenizer(maspack.util.ReaderTokenizer) FileReader(java.io.FileReader) 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